Java JComboBox Help.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Martyj
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Apr 23, 2009 11:23 am
Location: Ogden Utah
Contact:

Java JComboBox Help.

Post by Martyj »

I'm working on an applet something that works like this flash http://www.runehq.com/calculators/rs2/c ... me=Agility

The part that I'm having a hard time on is the JComboBoxes and how to create them with all the changing values.

Here is my applet that I have so far (Far from done)
Image

When you select on the top ComboBox it should load different ones in the second field. If everything works right the third one will get passed the correct value to show values there.

Here is my code that is causing the problem

Code: Select all

public void actionPerformed(ActionEvent e) {
         //This whole method needs redone. 
          Object source = e.getSource();
          if(source.equals(combo)){
            // Changes the Calculator
             calc = (String)combo.getSelectedItem();
             calc2 = (String)page.getItemAt(1);
             CalcBuild(calc, calc2);
          } 
          if(source.equals(page)){
              calc2 = (String)page.getSelectedItem();
              CalcBuild(calc,calc2);
          }
          else if(source.equals(search)){
             // Other code not pertaining to this
              }
              else{
              JOptionPane.showMessageDialog(this, "Error Code: GUI490", "Error, please enter a username.", JOptionPane.INFORMATION_MESSAGE);  
              }
          }  
    }
The right methods are getting called, it's just how I check the values of the ComboBoxes. If you want to view the whole source code you can download it here:

http://martyj.comuv.com/GUI.java

I'd really appreciate some help. Even if you don't know java you could give your ideas on it. Write it in pseudo code if you must :p.

Thanks
Marty
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Java JComboBox Help.

Post by wearymemory »

I didn't fully understand your question. I did, however, make an example in Java, which should hopefully clear up any questions you might have about changing the values of a JComboBox.

Code: Select all

package misc;

public class JComboBoxExample extends javax.swing.JFrame {
  private final String[] mainList = {"Item1", "Item2"};
  private final String[] item1List = {"Item1a", "Item1b"};
  private final String[] item2List = {"Item2a", "Item2b"};
  
  public JComboBoxExample() {
    initComponents();
  }
  
  private void initComponents() {
    jComboBox1 = new javax.swing.JComboBox(mainList);
    jComboBox2 = new javax.swing.JComboBox(item1List);
    
    setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
    getContentPane().add(jComboBox1, java.awt.BorderLayout.NORTH);
    
    jComboBox1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jComboBox1ActionPerformed(evt);
      }
    });
    
    getContentPane().add(jComboBox2, java.awt.BorderLayout.SOUTH);
    
    setLocationRelativeTo(null);
    pack();
  }
  
  private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
    if(jComboBox1.getSelectedItem().equals("Item1")) {
      jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(item1List));
    } else if(jComboBox1.getSelectedItem().equals("Item2")) {
      jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(item2List));
    }
  }
  
  public static void main(String[] argv) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new JComboBoxExample().setVisible(true);
      }
    });
  }
  
  private javax.swing.JComboBox jComboBox1;
  private javax.swing.JComboBox jComboBox2;
}
Martyj
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Apr 23, 2009 11:23 am
Location: Ogden Utah
Contact:

Re: Java JComboBox Help.

Post by Martyj »

right I didn't really show you guys what the problem was. When you click on the top combo box at like 530, 3 it is suppose to load the middle combo box with values depending on what is in the top combo box.

http://martyj.comuv.com/EvilChickenCalc.jar

There is the compiled version of it. Don't mind the JOptionBox I was using it to see what values are being passed through the action listener.

When you click Construction, in the top combo box, it should load up raw materials in the middle box and raw materials in the bottom box. If you click on garden in the 2nd tab once you are in construction in the top tab, it should load like 4 options in the bottom box.

I'll go through your posted code right now and see if that's the answer


EDIT:

That was extremely helpful. Thank you.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Java JComboBox Help.

Post by MarauderIIC »

Did that solve it? If so, I might ask you to change the topic to [SOLVED] Java JComboBox help
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply