How do I can validar two JComboBox in an alone JFrame or JDialog?

Good, I am using conecci髇 with base of information but an example without connection would help myself very much!

First, Eh created two JComboBox with the names:

JComboBox TypeDoc =new JComboBox();

JComboBox MatterDoc =new JComboBox();

.

.

.

Now this method

publicvoid actionPerformed(ActionEvent e){

JComboBox cb1 = (JComboBox)e.getSource();

String Type = (String)cb1.getSelectedItem();

Now, it had to do another similar method

JComboBox cb2 = (JComboBox)e.getSource();

String Matter = (String)cb2.getSelectedItem();

My problem is: how to do in order that cb1 with cb2 has relation with Typedoc and MatterDoc?

Already doing it with a JcomboBox it is more simply, but the problem for my is when there is more than 2 JComoBox, thank you very much I wait yours for help, good day

[1042 byte] By [DeCoa] at [2007-10-3 4:33:30]
# 1
I don't quite get what kind of relation do You want to have between these combo boxes?You want ActionListrener to listen to TypeDoc and MatterDoc?
hellbindera at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 2

Do the following works?

public void actionPerformed(ActionEvent e){

String strTmp = null;

JComboBox cBTmp = e.getSource();

if(cBTmp == TypeDoc){

strTmp = (String)cBTmp.getSelectedItem();

return ;

}

if(cBTmp == MatterDoc){

strTmp = (String)cBTmp.getSelectedItem();

return ;

}

}

Message was edited by:

iceman1952

iceman1952a at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 3

The first two are of creation, other two are used in the event listener but they refer to the same JComboBox of creation

What I want is if it is necessary to do of this way

I have a form with two JComboBox of which both have to have values to enter base data

Or how i can do it in the ActionPerformed?

maybe already is not necessary them cb1 and cb2 for it!

DeCoa at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 4

Nice shot! Definitelly works if combo boxes are declared locally.

The nice solution would be:

TypeDoc.setActionCommand("type");

MatterDoc.setActionCommand("matter");

public void actionPerformed(ActionEvent e){

if (e.getActionCommand().equals("type")){

// whatever you want from TypeDoc

} else {

//whatever you want from MatterDoc

}

}

Anyway if you just want selected item stored

public void actionPerformed(ActionEvent e){

obj = ((JComboBox)e.getSource()).getSelectedItem()

//Object obj must be declared globally - Your's strTmp doesn't exist outside actionPerformed

}

hellbindera at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 5

good idea iceman!! interesting!

But hour in the same form I have a button Agree

public void actionPerformed(ActionEvent e) {

String strTmp = null;

JComboBox cBTmp = e.getSource();

if(cBTmp == TypeDoc){

strTmp = (String)cBTmp.getSelectedItem();

return ;

}

if(cBTmp == MatterDoc){

strTmp = (String)cBTmp.getSelectedItem();

return ;

}

if(e.getSource() == btnAgree)

--> methoAgreee() ?'

}

//this method with jcomobox items!

methoAgreee(){

}

But hour in the same form I have a button Agree

How would it implement the method for the election of the intem in the jcomboBox?

thanks very much!!

DeCoa at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 6

The solution is simple. Declare combo boxes and received objects globally for the whole class:

class TestClass extends JDialog implements ActionListener{

JButton approveButton;

JComboBox typeDoc;

JComboBox matterDoc;

Object typeObj;

Object matterObj;

public TestClass(){

approveButton = new JButton();

approveButton.addActionListener(this);

typeDoc = new JComboBox();

matterDoc = new JComboBox();

}

public void actionPerformed(ActionEvent e){

typeObj = typeDoc.getSelectedItem();

matterObj = matterDoc.getSelectedItem();

}

}

There's no point in writing separate function methoAgree() unless You want some action taken when user selects items in combo boxes. Alternativelly:

public void actionPerformed(ActionEvent e){

methodAction(typeDoc.getSelectedItem(),matterDoc.getSelectedItem());

}

public void methodAction(Object typeObj, Object matterObj){

//what to do when user presses button

}

hellbindera at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 7
Certain hellbinder, but in my form it exists besides two JComoboBox more objects as three JTextField and the button to agreeThen it would be something similar to what giving these to meAnyhow I have declared all the rest objects of global form.
DeCoa at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 8
Just declare anything You want to use in actionPerformed globally and will work ;)
hellbindera at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...
# 9
Thank you I am going to try the code and later sending the advances, good it is a very important project that I realize and the whole code would not be important for me to send source by email
DeCoa at 2007-7-14 22:37:07 > top of Java-index,Desktop,Core GUI APIs...