2017-08-26 3 views
2

私はタクシー予約アプリケーションを作成しようとしています。さて、もし私が(例えば)モバイルアプリケーションでスケジュールを設定したいのであれば、ドライバ名でコンボボックスを有効にしたいのですが、誰かが電話でスケジュールしたら、ディスパッチャ名でコンボボックスを有効にしたいと思います。どうやって? JRadioButtonを与えるのItemListenerチェックのことを:私はあなただけ2のJRadioButtonと2 JComboBoxesをお持ちの場合は明らかに、それは、その後、解決策は単純ですJava - ラジオボタンに応じてコンボボックスを有効にする

public class OrderWindow extends JFrame { 

    private JLabel lblCustomerName; 
    private JTextField txtCustomerName; 
    private JLabel lblDateOrder; 
    private JPanel pnlDateOrder; 
    private JLabel lblDepartureAdress; 
    private JTextField txtADepartureAdress` 
    private JComboBox cbDriver; 
    private JRadioButton rbMobileApp; 
    private JRadioButton rbPhoneCall; 
    private ButtonGroup bgOrder; 

    public OrderWindow(){ 
     setTitle("Scheduling"); 
     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     setLocationRelativeTo(null); 
     setResizable(false); 
     initGUI(); 
     initActions(); 
    } 



    private void initActions() { 
     rbMobileApp.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource()==rbMobileApp) { 
        setEnabled(rbMobilnaAplikacija.isSelected()); 
       } 
      } 
     }); 
    } 

    private void initGUI() { 
     MigLayout mig = new MigLayout("wrap 2", "[][]", "[]10[][]10[]"); 
     setLayout(mig); 
     lblCustomerName = new JLabel("Name and Lastname"); 
     txtCustomerName = new JTextField(20); 

     lblDepartureAdress = new JLabel("Adress"); 
     txtDepartureAdress = new JTextField(20); 

     rbMobileApp = new JRadioButton("Application"); 
     rbPhoneCall = new JRadioButton("Call"); 
     bgPorudzbina = new ButtonGroup(); 
     add(lblCustomerName); 
     add(txtCustomerName); 
     add(lblDepartureAdress); 
     add(txtDepartureAdress); 
     add(rbMobileApp); 
     add(rbPhoneCall); 
     bgOrder = new ButtonGroup(); 
     bgOrder.add(rbMobileApp); 
     bgOrder.add(rbPhoneCall); 
    } 
} 

enter image description here

答えて

1

...働いていない、initActions()で何かをtryiedが、ラジオが選択されている場合は、対応するJComboBoxを選択します。あなたがのJRadioButton/JComboBoxの組み合わせの束を持っている場合例えば

radioBtn.addItemListener(evt -> { 
    combo.setEnabled(evt.getStateChange() == ItemEvent.SELECTED); 
}); 

は、その後、次の2つを接続するために、より堅牢な方法を必要とし、それは、HashMapのよう地図を使用してのいずれかによって達成することができます、または自分のクラスに両方のオブジェクトを置くことによって、例えばのようなもの:

enter image description here

import java.awt.event.ItemEvent; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 
import javax.swing.JRadioButton; 

public class RadioCombo<T> { 
    private JRadioButton radioBtn; 
    private JComboBox<T> combo; 

    public RadioCombo(String text, DefaultComboBoxModel<T> model) { 
     radioBtn = new JRadioButton(text); 
     combo = new JComboBox<>(model); 
     combo.setEnabled(false); 
     radioBtn.addItemListener(evt -> { 
      combo.setEnabled(evt.getStateChange() == ItemEvent.SELECTED); 
     }); 
    } 

    public JRadioButton getRadioBtn() { 
     return radioBtn; 
    } 

    public JComboBox<T> getCombo() { 
     return combo; 
    } 
} 

次にあなたがそうのようにそれを使用することができます。

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

@SuppressWarnings("serial") 
public class TestRadioCombo extends JPanel { 
    private static final String[] DATA = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 
    private static final String[] INNER_DATA = {"One", "Two", "Three", "Four", "Five"}; 
    private static final int GAP = 3; 
    public TestRadioCombo() { 
     ButtonGroup buttonGroup = new ButtonGroup(); 

     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     setLayout(new GridBagLayout()); 
     for (String datum : DATA) { 

      DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(); 
      for (String innerDatum : INNER_DATA) { 
       String item = datum + " - " + innerDatum; 
       model.addElement(item); 
      } 
      RadioCombo<String> radioCombo = new RadioCombo<>(datum, model); 
      buttonGroup.add(radioCombo.getRadioBtn()); 
      addRadioCombo(radioCombo); 
     } 
    } 

    private void addRadioCombo(RadioCombo<String> radioCombo) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = GridBagConstraints.RELATIVE; 
     gbc.anchor = GridBagConstraints.WEST; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.insets = new Insets(GAP, GAP, GAP, 2 * GAP); 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     add(radioCombo.getRadioBtn(), gbc); 

     gbc.gridx = 1; 
     gbc.anchor = GridBagConstraints.EAST; 
     gbc.insets = new Insets(GAP, GAP, GAP, GAP); 
     add(radioCombo.getCombo(), gbc); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new TestRadioCombo()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

別のオプションは、JRadioButtonを一束だけするJComboBoxを持って、そして、あなたのラジオボタン項目リスナーに、のJRadioButtonを選択したかに応じするJComboBoxのモデルを交換することです。

関連する問題