Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Question in JCombobox

  1. #1
    Walaa is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Post Question in JCombobox

    I have JComboBox expressed on the Marital Status and there is also JRadioButton expressed about sex
    My question is how can I work the hidden box to insert the name of the wife and shows when I have a male and married and when they are female and married box appears to insert the name of husband

    Please I need your help

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

    Default Re: Question in JCombobox

    Please go into a bit more depth on what you're trying to do and what you've tried including code. The best would be to post an SSCCE that almost does what you want.
    Walaa likes this.

  3. #3
    Walaa is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Question in JCombobox

    This is the code ,There are JLabel and JTextField to insert the name of the wife but hidden and appears when the condition that the person is a male and married appears to insert wife's name
    However, if the condition of the person female and married JLabel and JTextField appears to insert the name of husband


    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    class opengui extends JFrame implements ActionListener{

    Container c;


    JLabel l;



    opengui(){
    super("Transformation Operation");
    c=this.getContentPane();
    c.setLayout(null);






    JLabel l1=new JLabel("Sno");
    l1.setBounds(30,90,50,50);
    c.add(l1);

    JTextField t1=new JTextField();
    t1.setBounds(70,90,70,30);
    c.add(t1);

    JLabel l2=new JLabel("Name");
    l2.setBounds(195,90,80,30);
    c.add(l2);

    JTextField t2=new JTextField();
    t2.setBounds(270,90,80,40);
    c.add(t2);


    JLabel l3=new JLabel("Birthday");
    l3.setBounds(50,170,70,40);
    c.add(l3);

    JTextField t3=new JTextField();
    t3.setBounds(140,170,50,30);
    c.add(t3);


    JLabel l4=new JLabel("Salary");
    l4.setBounds(320,170,70,40);
    c.add(l4);

    JTextField t4=new JTextField();
    t4.setBounds(370,170,60,30);
    c.add(t4);

    JLabel l5=new JLabel("Sex");
    l5.setBounds(50,220,70,40);
    c.add(l5);

    ButtonGroup bg =new ButtonGroup();

    JRadioButton male =new JRadioButton("Male");
    male.setSelected(true);
    male.setBounds(50,270,80,40);

    JRadioButton female =new JRadioButton("Female");
    female.setBounds(140,270,80,40);
    bg.add(male);
    bg.add(female);
    c.add(male);
    c.add(female);



    JLabel l6=new JLabel("Marital Status");
    l6.setBounds(50,370,100,40);
    c.add(l6);
    JComboBox combo= new JComboBox();
    combo.setBounds(155,370,100,50);
    c.add(combo);
    combo.addItem("Marid");
    combo.addItem("Single");
    combo.setSelectedIndex(-1);


    JLabel l7=new JLabel("name of husband");
    l7.setBounds(60,440,100,50);
    c.add(l7);
    l7.setEnabled(false);
    JTextField t7=new JTextField();
    t7.setBounds(170,440,100,50);
    c.add(t7);
    t7.setEnabled(false);


    JLabel l8=new JLabel("name of wife");
    l8.setBounds(280,440,100,50);
    c.add(l8);
    l8.setEnabled(false);

    JTextField t8=new JTextField();
    t8.setBounds(390,440,100,50);
    c.add(t8);
    t8.setEnabled(false);


    JButton b1=new JButton("OK");
    b1.setBounds(50,500,100,50);
    c.add(b1);

    JButton b2=new JButton("More");
    b2.setBounds(160,500,100,50);
    c.add(b2);
    setSize(600,600);
    setVisible(true);



    }
    public void actionPerformed(ActionEvent e)
    {




    }







    public static void main(String args[]){
    new opengui();
    } }

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    927
    Rep Power
    2

    Default Re: Question in JCombobox

    Your class implements ActionListener, but you don't use it. Add an ActionListener to variable "combo" so the actionPerformed(ActionEvent) is fired when you change the marital status. Inside actionPerformed you can start checking things. However, the names textfields and sex radio buttons are local in the constructor. You should move these to fields so you can use them in other methods as well.

    Oh, and while you're at it: in some countries gay marriage is allowed so a man that has marital status "Marid" [sic] doesn't always have a wife :). Depending on the country you're in, this might be a fun argument to hand to your teacher :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Question in JCombobox

    Thread moved from a staff-only section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Walaa is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Question in JCombobox

    Did you mean like this , the problem I face is that I do not know how I work previous comparison in actionlistener

    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    class opengui extends JFrame implements ActionListener{

    Container c;


    JLabel l;



    opengui(){
    super("Transformation Operation");
    c=this.getContentPane();
    c.setLayout(null);






    JLabel l1=new JLabel("Sno");
    l1.setBounds(30,90,50,50);
    c.add(l1);

    JTextField t1=new JTextField();
    t1.setBounds(70,90,70,30);
    c.add(t1);

    JLabel l2=new JLabel("Name");
    l2.setBounds(195,90,80,30);
    c.add(l2);

    JTextField t2=new JTextField();
    t2.setBounds(270,90,80,40);
    c.add(t2);


    JLabel l3=new JLabel("Birthday");
    l3.setBounds(50,170,70,40);
    c.add(l3);

    JTextField t3=new JTextField();
    t3.setBounds(140,170,50,30);
    c.add(t3);


    JLabel l4=new JLabel("Salary");
    l4.setBounds(320,170,70,40);
    c.add(l4);

    JTextField t4=new JTextField();
    t4.setBounds(370,170,60,30);
    c.add(t4);

    JLabel l5=new JLabel("Sex");
    l5.setBounds(50,220,70,40);
    c.add(l5);

    ButtonGroup bg =new ButtonGroup();

    JRadioButton male =new JRadioButton("Male");
    male.setSelected(true);
    male.setBounds(50,270,80,40);
    male.addActionListener(this);

    JRadioButton female =new JRadioButton("Female");
    female.setBounds(140,270,80,40);
    bg.add(male);
    bg.add(female);
    c.add(male);
    c.add(female);
    female.addActionListener(this);


    JLabel l6=new JLabel("Marital Status");
    l6.setBounds(50,370,100,40);
    c.add(l6);
    JComboBox combo= new JComboBox();
    combo.setBounds(155,370,100,50);
    c.add(combo);
    combo.addItem("Marid");
    combo.addItem("Single");
    combo.setSelectedIndex(-1);
    combo.addActionListener(this);

    JLabel l7=new JLabel("name of husband");
    l7.setBounds(60,440,100,50);
    c.add(l7);
    l7.setEnabled(false);
    JTextField t7=new JTextField();
    t7.setBounds(170,440,100,50);
    c.add(t7);
    t7.setEnabled(false);


    JLabel l8=new JLabel("name of wife");
    l8.setBounds(280,440,100,50);
    c.add(l8);
    l8.setEnabled(false);

    JTextField t8=new JTextField();
    t8.setBounds(390,440,100,50);
    c.add(t8);
    t8.setEnabled(false);

    JButton b1=new JButton("OK");
    b1.setBounds(50,500,100,50);
    c.add(b1);

    JButton b2=new JButton("More");
    b2.setBounds(160,500,100,50);
    c.add(b2);
    setSize(600,600);
    setVisible(true);



    }
    public void actionPerformed(ActionEvent e)
    {




    }







    public static void main(String args[]){
    new opengui();
    }
    }

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    927
    Rep Power
    2

    Default Re: Question in JCombobox

    Your radios and combo now have an ActionListener. Now the method actionPerformed(ActionEvent e) is triggered once there is a change in any of those. To check for which component triggered the event you can check the event's source: e.getSource(). You can compare that with any of your components. Problem is that the scope of the components is local to the constructor. You would have to move any component that is accessed through your class to a field. So in short it would look like this:

    Java Code:
    public class Opengui implements ActionListener {
        private JComboBox maritalStatus; //this field is now accessible in the methods of the class 
    
        public Opengui() {
            maritalStatus = new JComboBox();
            maritalStatus.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent e) {
            if ( e.getSource() == maritalStatus) {
                 //perform somethisng with another component
            }
        }
    I left lots of stuff out, but you hopefully get the idea.

    Good luck.

  8. #8
    Walaa is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Question in JCombobox

    Thank you for helping this is the code you mean, but what I want to do is when work Action on combo to achieve what I'm asking, which is to show the JLabel and JTextField but does not shown it I do not know why

    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    class opengui extends JFrame implements ActionListener{

    JComboBox maritalStatus;
    JRadioButton male ,female;

    Container c;
    JTextField t7,t8;

    JLabel l,l7,l8;



    opengui(){
    super("sewtergy");
    c=this.getContentPane();
    c.setLayout(null);






    JLabel l1=new JLabel("Sno");
    l1.setBounds(30,90,50,50);
    c.add(l1);

    JTextField t1=new JTextField();
    t1.setBounds(70,90,70,30);
    c.add(t1);

    JLabel l2=new JLabel("Name");
    l2.setBounds(195,90,80,30);
    c.add(l2);

    JTextField t2=new JTextField();
    t2.setBounds(270,90,80,40);
    c.add(t2);


    JLabel l3=new JLabel("Birthday");
    l3.setBounds(50,170,70,40);
    c.add(l3);

    JTextField t3=new JTextField();
    t3.setBounds(140,170,50,30);
    c.add(t3);


    JLabel l4=new JLabel("Salary");
    l4.setBounds(320,170,70,40);
    c.add(l4);

    JTextField t4=new JTextField();
    t4.setBounds(370,170,60,30);
    c.add(t4);

    JLabel l5=new JLabel("Sex");
    l5.setBounds(50,220,70,40);
    c.add(l5);

    ButtonGroup bg =new ButtonGroup();

    male =new JRadioButton("Male");
    male.setSelected(true);
    male.setBounds(50,270,80,40);
    male.addActionListener(this);

    female =new JRadioButton("Female");
    female.setBounds(140,270,80,40);
    bg.add(male);
    bg.add(female);
    c.add(male);
    c.add(female);
    female.addActionListener(this);



    JLabel l6=new JLabel("Marital Status");
    l6.setBounds(50,370,100,40);
    c.add(l6);

    [COLOR="#FF0000"] maritalStatus= new JComboBox();
    maritalStatus.setBounds(155,370,100,50);
    c.add(maritalStatus);
    maritalStatus.addItem("Marid");
    maritalStatus.addItem("Single");
    maritalStatus.setSelectedIndex(-1);
    maritalStatus.addActionListener(this);


    l7=new JLabel("name of husband");
    l7.setBounds(60,440,100,50);
    c.add(l7);
    l7.setEnabled(false);

    t7=new JTextField();
    t7.setBounds(170,440,100,50);
    c.add(t7);
    t7.setEnabled(false);


    l8=new JLabel("name of wife");
    l8.setBounds(280,440,100,50);
    c.add(l8);
    l8.setEnabled(false);

    t8=new JTextField();
    t8.setBounds(390,440,100,50);
    c.add(t8);
    t8.setEnabled(false);

    JButton b1=new JButton("OK");
    b1.setBounds(50,500,100,50);
    c.add(b1);

    JButton b2=new JButton("More");
    b2.setBounds(160,500,100,50);
    c.add(b2);
    setSize(600,600);
    setVisible(true);



    }
    public void actionPerformed(ActionEvent e)
    {

    if ( e.getSource() == maritalStatus) {
    if(maritalStatus.equals("Mariad") && male.equals("Male"){


    l7.setEnabled(true);
    t7.setEnabled(true);
    }
    /* else

    if (combo.equals("Mariad")&& female.equals("Female")){

    l8.setEnabled(true);
    t8.setEnabled(true);

    }*/

    }


    }


    public static void main(String args[]){
    new opengui();


    }
    }
    Last edited by Walaa; 11-10-2012 at 05:57 PM. Reason: edit

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    927
    Rep Power
    2

    Default Re: Question in JCombobox

    Here's an example on how to check the contents of a JComboBox: How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). Then use the .equals() on the value that you get from getSelectedItem().

  10. #10
    Walaa is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Question in JCombobox

    Thank you for your help I will try to resolve it this way

Similar Threads

  1. Need help for a jcombobox
    By Alexis in forum AWT / Swing
    Replies: 4
    Last Post: 09-16-2011, 02:13 AM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  4. JComboBox Question
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 12:09 PM
  5. Help with jComboBox
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:08 PM

Posting Permissions

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