Results 1 to 11 of 11

Thread: JComboBox

  1. #1
    Java_25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default JComboBox

    Hi All,

    Newbie here looking for a bit of advice!

    I find these things hard to explain so I'll just tell you what is on my GUI -

    At the top there are 2 combo boxes with 4 options each(say a,b,c,d in each one). There is also a button that says Calculate and a text field for the output to go in.

    My aim is for a number to be produced in the textfield when two options are selected in the combo boxes and the calculate button is clicked.

    So, for example, if a is in the first box and b is in the second, produce 4.5 in the text box.

    I'm very new to Java and understand it must be something to do with an ActionListener but I'm not sure how to go about this.

    Thanks for the help.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Java_25 View Post
    I'm very new to Java and understand it must be something to do with an ActionListener but I'm not sure how to go about this.
    Please have a look at the Swing tutorials on how to use buttons, action listeners and comboboxes as this will show you the tools that you'll need to solve this.

    Useful tutorial links:

  4. #4
    Java_25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the replies.

    Most of it goes straight over my head, I don't believe I have the foundation knowledge.

    The bit Im stuck on:

    This screen (with the 2 JComboBox and the JButton) is created as a new frame from the previous frame (main menu with button to this frame). So at the moment the code is all written under the first action listener (from the main menu). Is there a better way to do this (I have experience in VB and you could do this with procedures etc)?

    Sorry for the newbie questions!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Then start smaller- don't worry about that first JFrame for now. Create a small program that simply contains the JFrame with the JComboBoxes and the JButton. When you have that working, then worry about adding it to your existing code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Java_25 View Post
    Thanks for the replies.

    Most of it goes straight over my head, I don't believe I have the foundation knowledge.
    Keep going through the tutorials as with any skill the more you do this, the better you'll become at understanding them.


    The bit Im stuck on:

    This screen (with the 2 JComboBox and the JButton) is created as a new frame from the previous frame (main menu with button to this frame). So at the moment the code is all written under the first action listener (from the main menu). Is there a better way to do this (I have experience in VB and you could do this with procedures etc)?

    Sorry for the newbie questions!
    A bit hard to give specific recommendations given this information.

  7. #7
    Java_25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Keep going through the tutorials as with any skill the more you do this, the better you'll become at understanding them.




    A bit hard to give specific recommendations given this information.
    Hi,
    Thanks for the advice - currently attempting to do this separate form.

    And sorry I did describe it badly - I'll start again.

    Basically I have 2 ComboBoxes, each with 4 options ( say a,b,c,d).
    I also have a Button and a TextField.

    I want the user to select an option from ComboBox1 (say a), then select a different option from ComboBox2 (say b), then click the button and a number (say 2.5) will appear in the text field. This number will be different for each combination of letters.

    Is that a bit clearer?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, it's basically as you described it in your initial post, and still you are asking very general questions, questions that are best answered by your going through the tutorials as I've linked to above. They come with runnable sample code and will help you to do just what you want, please believe me. So go through them and give it your best shot. If you get stuck or have a specific answerable questions, then please show us your code and ask your question. Again, good luck!

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Guess the OP didn't like any of this advice.

    Crossposted: JComboBox
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    He'll go far in programming.

  11. #11
    Java_25 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Sorry guys, didn't know cross posting was bad - just wanted to get a broader view, won't happen again!

    I've had an attempt and this is what I've got so far, I tried simplifying it down (as suggested) to just one combo box and a button and a label for the output.. Here's my code so far:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test1 implements ActionListener {
    public static void main(String[] args){
    JComboBox box;
    JTextField text;
    JFrame frame;
    JPanel panel;
    JButton button;
    JLabel label;
    String[] locations = { "a" , "b" , "c" };

    box = new JComboBox(locations);
    box.setSelectedIndex(2);

    frame = new JFrame();

    panel = new JPanel();

    button = new JButton("Click");

    button.addActionListener(this);

    frame.add(panel);
    panel.add(box);
    panel.add(button);
    panel.add(label);
    frame.setVisible(true);
    frame.setSize(600,600);
    }
    public void actionPerformed(ActionEvent e) {
    if (box.getSelectedItem() = "a") {
    label.setText("4.5");
    }
    }
    }


    Where has this gone wrong?

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  2. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  3. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  4. JComboBox
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-10-2008, 01:22 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •