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.
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();
} }
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 :)
Re: Question in JCombobox
Thread moved from a staff-only section.
db
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();
}
}
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:
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.
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();
}
}
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().
Re: Question in JCombobox
Thank you for your help I will try to resolve it this way