2017-01-03 2 views
0

Also mein Problem ist mit JComboBoxen und ActionListeners. Ich werde einen neuen vereinfachten Code erstellen, um das Problem anhand meines ursprünglichen Codes darzustellen. Ich möchte, dass eine JComboBox eine JComboBox hinzufügt, die dann eine dritte JComboBox hinzufügt und so weiter. Jedes Mal, wenn ich auf sie klicke, möchte ich, dass sie den Inhalt ändern, abhängig davon, was die vorherige JComboBox anzeigt.JComboBox fesselt andere JComboBoxen ActionListeners

Mein größtes Problem ist jetzt, dass wenn ich etwas in der ersten "Rennbox" von JComboBox auswähle. Es fügt dem Panel nicht nur "infantrybox" hinzu, es fügt auch alle anderen JComboBoxen hinzu, die ich habe, anstatt sie nur einmal hinzuzufügen, wenn ich etwas in der jeweiligen JComboBox auswähle.

Es ist wie wenn ich etwas in Racebox wähle es beginnt Code von jedem anderen actionPerformed zu lesen.

Eine seltsame Sache ist, dass die JComboBoxen nach dem Hinzufügen von "racebox" rückwärts hinzugefügt werden. Erstens: Racebox Zweitens: infantrynmrbox Drittens: infantrybox

... 
public void Attacker(){ 

    racebox = new JComboBox(array); 
    infantrybox = new JComboBox(); 
    infantrynmrbox = new JComboBox(); 

    panel.add(racebox); 
    panel.revalidate(); 
    panel.repaint(); 

    racebox.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      JComboBox cb = (JComboBox)e.getSource(); 
      race = (String)cb.getSelectedItem(); 

      infantrybox.removeAllItems(); 
      for(String s : otherarray){ 
       infantrybox.addItem(s); 
      } 

      panel.add(infantrybox); 
      panel.revalidate(); 
      panel.repaint(); 
     } 
    }); 

    infantrybox.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      JComboBox cb = (JComboBox)e.getSource(); 
      infantry = (String)cb.getSelectedItem(); 

      infantrynmrbox.removeAllItems(); 
      for(String s : nmr){ 
       infantrynmrbox.addItem(s); 
       System.out.println(s + " "); 
      } 

      panel.add(infantrynmrbox); 
      panel.revalidate(); 
      panel.repaint(); 
     } 
    }); 

    infantrynmrbox.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      JComboBox cb = (JComboBox)e.getSource(); 
      infantrynmr = Integer.parseInt((String)cb.getSelectedItem()); 
     } 
    }); 
    ... 
} 

Antwort

1

nicht Combo-Boxen an der Platte halten Sie hinzufügen.

Stattdessen ändern Sie einfach das Modell des vorhandenen Kombinationsfelds.

Zum Beispiel:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.*; 

public class ComboBoxTwo extends JPanel implements ActionListener 
{ 
    private JComboBox<String> mainComboBox; 
    private JComboBox<String> subComboBox; 
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>(); 

    public ComboBoxTwo() 
    { 
     String[] items = { "Select Item", "Color", "Shape", "Fruit" }; 
     mainComboBox = new JComboBox<String>(items); 
     mainComboBox.addActionListener(this); 

     // prevent action events from being fired when the up/down arrow keys are used 
     mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     add(mainComboBox); 

     // Create sub combo box with multiple models 

     subComboBox = new JComboBox<String>(); 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     add(subComboBox); 

     JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", ""); 
     Dimension d = arrow.getPreferredSize(); 
     System.out.println(arrow.getClass()); 
     System.out.println(d); 
     d.width = 100; 
     arrow.setPreferredSize(d); 

     String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; 
     subItems.put(items[1], subItems1); 

     String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; 
     subItems.put(items[2], subItems2); 

     String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; 
     subItems.put(items[3], subItems3); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     String item = (String)mainComboBox.getSelectedItem(); 
     Object o = subItems.get(item); 

     if (o == null) 
     { 
      subComboBox.setModel(new DefaultComboBoxModel()); 
     } 
     else 
     { 
      subComboBox.setModel(new DefaultComboBoxModel((String[])o)); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     try 
     { 
//   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) { } 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxTwo()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
+0

Vielen Dank für Ihre Antwort. Ein Teil dieses Codes verstehe ich überhaupt nicht. Entschuldigung, mein Code ist ein wenig unklar. Die Boxen werden nur einmal im Originalcode hinzugefügt. Aber das Problem ist, dass sie alle auf einmal hinzugefügt werden. Ich möchte, dass alle hinzugefügt werden, aber nicht alle gleichzeitig. – Richovic

+0

@Richovic, 'Aber das Problem ist, dass sie alle auf einmal hinzugefügt werden. - Fügen Sie den Aktionslistener dem Kombinationsfeld hinzu, nachdem Sie die Daten dem Kombinationsfeld hinzugefügt haben. Wenn Sie mehr Hilfe benötigen, dann veröffentlichen Sie eine ordnungsgemäße [mcve], die das Problem demonstriert. – camickr

+0

Nevermind, ich habe es jetzt. Ich habe das "setModel (new DefaultComboBoxModel());" in gewisser Weise und ich habe es funktioniert. Vielen Dank. – Richovic