2012-01-24 31 views
0

次のスニペットでオン/オフ機能が達成されています。
しかし、VISUALはラジオボタンを「選択済み」として表示しません。
私はあなたが選択を解除(下部にリスナーを参照してください)、一時停止前とコンボをクリア
選択した視覚的な表示で/方向助けを事前にradioButtonに選択されたステータスが表示されません

おかげ
エース

// 
// The idea is to have a combo box for device selection 
// and radio buttons to select on/off for the device 
// the buttons are NOT available until a combo selection is made 
// The button should show selected before the combo box is reset 
// 

import javax.swing.JFrame; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.JPanel; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.AbstractButton; 
import javax.swing.JTabbedPane; 
import javax.swing.ButtonGroup; 
import javax.swing.JComboBox; 
import javax.swing.JRadioButton; 

public class itsGUI extends JFrame 
{ 
private static final long serialVersionUID = 1L; 
static itsGUI  yeahItsGUI; 

// Main panel 
JPanel    jpMain; 
JTabbedPane   jtpMain; 
GridBagLayout  gblMain = new GridBagLayout(); 
GridBagConstraints gbcMain = new GridBagConstraints(); 

// Administrator tab 
JPanel    jpAdmin; 
GridBagLayout  gblAdmin = new GridBagLayout(); 
GridBagConstraints gbcAdmin = new GridBagConstraints(); 
ButtonGroup   bgAdmin; 
JComboBox   jcbDevice; 
JRadioButton  jrbOn; 
JRadioButton  jrbOff; 

// local 
int     DeviceIndex = 0; 
String    DeviceName = null; 


public static void main(String args[]) 
{ 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
    catch (ClassNotFoundException e) {} 
    catch (InstantiationException e) {} 
    catch (IllegalAccessException e) {} 
    catch (UnsupportedLookAndFeelException e) {} 

    yeahItsGUI = new itsGUI(); 
    } /* main */ 


public itsGUI() 
{ 
    super("Button Group"); 

    jpMain = new JPanel(); 
    jpMain.setLayout(gblMain); 
    jpMain.setPreferredSize(new Dimension(200, 100)); 

    jtpMain = new JTabbedPane(); 

    jpAdmin = new JPanel(); 
    bgAdmin = new ButtonGroup(); 
    jpAdmin.setLayout(gblAdmin); 

    String[] dataList = { "Choose a device", "Lamp", "Radio", "Toaster" } ; 
    jcbDevice = new JComboBox(dataList); 
    jcbDevice.addActionListener(listenerCombo); 
    gbcAdmin.gridx = 1; 
    gbcAdmin.gridy = 1; 
    gbcAdmin.gridwidth = 6; 
    gbcAdmin.gridheight = 3; 
    gbcAdmin.fill = GridBagConstraints.BOTH; 
    gbcAdmin.weightx = 1; 
    gbcAdmin.weighty = 0; 
    gbcAdmin.anchor = GridBagConstraints.NORTH; 
    gblAdmin.setConstraints(jcbDevice, gbcAdmin); 
    jpAdmin.add(jcbDevice); 

    jrbOn = new JRadioButton("ON"); 
    jrbOn.setActionCommand("ON");   
    jrbOn.addActionListener(listenerRadio); 
    jrbOn.setEnabled(false);  // start radioButton DISABLED 
    bgAdmin.add(jrbOn); 
    gbcAdmin.gridx = 8; 
    gbcAdmin.gridy = 2; 
    gbcAdmin.gridwidth = 3; 
    gbcAdmin.gridheight = 1; 
    gbcAdmin.fill = GridBagConstraints.BOTH; 
    gbcAdmin.weightx = 1; 
    gbcAdmin.weighty = 0; 
    gbcAdmin.anchor = GridBagConstraints.NORTH; 
    gblAdmin.setConstraints(jrbOn, gbcAdmin); 
    jpAdmin.add(jrbOn); 

    jrbOff = new JRadioButton("OFF"); 
    jrbOff.setActionCommand("OFF");   
    jrbOff.addActionListener(listenerRadio); 
    jrbOff.setEnabled(false);  // start radioButton DISABLED 
    bgAdmin.add(jrbOff); 
    gbcAdmin.gridx = 13; 
    gbcAdmin.gridy = 2; 
    gbcAdmin.gridwidth = 1; 
    gbcAdmin.gridheight = 1; 
    gbcAdmin.fill = GridBagConstraints.BOTH; 
    gbcAdmin.weightx = 1; 
    gbcAdmin.weighty = 0; 
    gbcAdmin.anchor = GridBagConstraints.NORTH; 
    gblAdmin.setConstraints(jrbOff, gbcAdmin); 
    jpAdmin.add(jrbOff); 

    jtpMain.addTab("Controller", jpAdmin); 

    gbcMain.gridx = 0; 
    gbcMain.gridy = 0; 
    gbcMain.gridwidth = 20; 
    gbcMain.gridheight = 5; 
    gbcMain.fill = GridBagConstraints.BOTH; 
    gbcMain.weightx = 1; 
    gbcMain.weighty = 1; 
    gbcMain.anchor = GridBagConstraints.NORTH; 
    gblMain.setConstraints(jtpMain, gbcMain); 
    jpMain.add(jtpMain); 

    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    setContentPane(jpMain); 
    pack(); 
    setVisible(true); 
    } /* itsGUI */ 



ActionListener listenerCombo = new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    JComboBox combo = (JComboBox) e.getSource(); 
    DeviceIndex = combo.getSelectedIndex(); 
    if (DeviceIndex > 0) { 
     // ENABLE the radioButtons 
     DeviceName = (String)combo.getSelectedItem(); 
     jrbOn.setEnabled(true);  
     jrbOff.setEnabled(true); 
     } 
    else { 
     // DISABLE the radioButtons 
     DeviceName = null ; 
     jrbOn.setEnabled(false);  
     jrbOff.setEnabled(false); 
     } 
    } 
}; 



ActionListener listenerRadio = new ActionListener() { 
    public void actionPerformed(ActionEvent actionEvent) { 
    AbstractButton aButton = (AbstractButton) actionEvent.getSource(); 
    if (aButton.getText().equals("ON")) { 
     // turn on radioButton selection ON indicator 
     // jrbOn.setSelected(true); 
     bgAdmin.setSelected(jrbOn.getModel(), true); 
     System.out.println("Turning ON the " + DeviceName); 
     } /* on */ 
    else if (aButton.getText().equals("OFF")) { 
     // turn on radioButton selection OFF indicator 
     // jrbOff.setSelected(true);    
     bgAdmin.setSelected(jrbOff.getModel(), true); 
     System.out.println("Turning OFF the " + DeviceName); 
     } /* off */ 
    try { Thread.sleep(1000L); } catch (InterruptedException e) {} // wait a second 
    bgAdmin.clearSelection();  // clear selection indicator of both radioButtons 
    jrbOn.setEnabled(false);  // DISABLE the radioButtons 
    jrbOff.setEnabled(false);  
    jcbDevice.setSelectedIndex(0); // Clear the device selection 
    } 
    }; 
} /* class */ 
+0

行を削除します。bgAdmin.clearSelection();それは動作します。 –

答えて

4

を探しています

bgAdmin.clearSelection(); 

達成しようとしているものの、ブロックしないことを確認してください。イベントディスパッチスレッド(EDT)w ith Thread.sleep()。続きを読むhere

+0

EDT&Thread.sleep()+1 – mKorbel

+0

+1、あまりにも真実、あなたは全体のことについて言った:-)よろしくご連絡ください –

+0

EDT上のポインタありがとうございます。コンボリスナにクリアを移動しました。今働いている。 – user1166392

関連する問題