Hello, I have several Objects in switch I need to set the text that should be in a string, ill show an example of how i have done it. It´s all made in a JPanel using NetBeans.

private void comboBoxMajorChoosingActionPerformed(java.awt.event.ActionEvent evt) {
comboBoxMinorChoosing1.addItem("English");
comboBoxMinorChoosing1String = comboBoxMinorChoosing1.getSelectedItem().toString();
}
private void comboBoxMinorChoosing1ActionPerformed(java.awt.event.ActionEvent evt) {
if (blabla = true) {
comboBoxMinorChoosing1.addItem("Math");
comboBoxMinorChoosing1String = comboBoxMinorChoosing1.getSelectedItem().toString();
}}

public String comboBoxMinorChoosing1String {
return comboBoxMinorChoosing1String;
}

String comboBoxMinorChoosing1String = "";

It all works as long as I´m not trying to define the text twice, i know it isnt needed in the example I have given but in my actual application I do need to do this, so anyone have any advices?


it is about how to get a String to be defined twice so to say. Let me try to explain in the program you first choose one thing through a combobox (called the MajorChoosing) which then builds up a combobox (called MinorChoosing) and inserts things into this combobox. After that it makes a String so I can compare the SelectedItem later on. This is all defined in the first part(note it is not the actual coding im displaying since it would make it more confusing):

private void comboBoxMajorChoosingActionPerformed(java.awt.event.ActionEvent evt) {
comboBoxMinorChoosing1.addItem("English");
comboBoxMinorChoosing1String = comboBoxMinorChoosing1.getSelectedItem().toString();

then whenever the MinorChoosing combobox is clicked on or changed I want it to replace the old String with the new SelectedItem so I can compare them that way. Hope you understand it now.

Thanks in advance

Recommended Answers

All 4 Replies

Not that I do not want to help, but I did not get what you trying to do...

commented: I lost interest at the lack of code tags, but since you've got the keys to the dungeon, perhaps.... +29

I´ll try with all the relevant code then.

Here´s all my code(the relavent part at least) and I wrote with capital letters where I want it to collect the String:

private void HTXStudieretningBoxActionPerformed(java.awt.event.ActionEvent evt) {
HTXStudieretningString = HTXStudieretningBox.getSelectedItem().toString();
if (HTXStudieretningBox.getSelectedItem().toString().equals("Kommunikation/IT")){
System.out.println("lol");
HTXAFagBox1.removeAllItems();
HTXAFagBox1.addItem("Dansk");

HTXAFagBox2.removeAllItems();
HTXAFagBox2.addItem("Kom/IT ");

HTXAFagBox3.removeAllItems();
HTXAFagBox3.addItem("Teknik ");

HTXAFagBox4.removeAllItems();
HTXAFagBox4.addItem("Engelsk");
HTXAFagBox4.addItem("Mat ");
HTXAFagBox4.addItem("Intet ");
HTXAFagBox4.setSelectedItem("Intet ");
HTXAFagBox5.removeAllItems();
HTXAFagBox5.addItem("Mat ");
HTXAFagBox5.addItem("Engelsk");
HTXAFagBox5.addItem("Intet ");
HTXAFagBox5.setSelectedIndex(2);

// HERE IT SHOULD DEFINE THE STRING WITH THE FOLLOWING SENTENCE
HTXAFag5String = HTXAFagBox5.getSelectedItem().toString();

HTXBFagBox1.removeAllItems();
HTXBFagBox1.addItem("Engelsk");

HTXBFagBox2.removeAllItems();
HTXBFagBox2.addItem("Kemi ");

HTXBFagBox3.removeAllItems();
HTXBFagBox3.addItem("Fysik ");

HTXBFagBox4.removeAllItems();
HTXBFagBox4.addItem("Mat ");

HTXBFagBox5.removeAllItems();
HTXBFagBox5.addItem("Tekno ");

HTXBFagBox6.removeAllItems();
HTXBFagBox6.addItem("Samfund");

HTXBFagBox7.removeAllItems();
HTXBFagBox7.addItem("Idehis ");

HTXCFagBox1.removeAllItems();
HTXCFagBox1.addItem("Biologi");

HTXCFagBox2.removeAllItems();
HTXCFagBox2.addItem("Idræt ");
HTXCFagBox2.addItem("Medie ");
HTXCFagBox2.addItem("ErhvØko");
HTXCFagBox2.addItem("Inno ");
HTXCFagBox2.addItem("Program");
HTXCFagBox2.addItem("Psykolo");
HTXCFagBox2.addItem("Sta&Sty");
HTXCFagBox2.setSelectedItem("Psykolo");
HTXCFagBox3.removeAllItems();
HTXCFagBox3.addItem("Idræt ");
HTXCFagBox3.addItem("Medie ");
HTXCFagBox3.addItem("ErhvØko");
HTXCFagBox3.addItem("Inno ");
HTXCFagBox3.addItem("Program");
HTXCFagBox3.addItem("Psykolo");
HTXCFagBox3.addItem("Sta&Sty");
HTXCFagBox3.setSelectedItem("ErhvØko");

HTXCFagBox4.removeAllItems();
HTXCFagBox4.addItem("Intet ");
HTXCFagBox4.setVisible(false);

}
}

private void HTXAFagBox5ActionPerformed(java.awt.event.ActionEvent evt) {

//AND HERE AGAIN REDEFINE THE STRING IF AN ACTION HAPPENS TO THIS COMBOBOX
HTXAFag5String = HTXAFagBox5.getSelectedItem().toString();

if (HTXStudieretningBox.getSelectedIndex() == 0){
System.out.println(HTXAFag5String);
System.out.println(HTXAFag4String);
if (HTXAFag5String.equals("Engelsk")) {
HTXBFagBox1.setVisible(false);
HTXBFagBox1.removeAllItems();
HTXBFagBox1.addItem("Intet ");
HTXBFagBox1.addItem("Engelsk");

HTXAFagBox5.removeAllItems();
HTXAFagBox5.addItem("Mat ");
HTXAFagBox5.addItem("Intet ");
HTXAFagBox5.setVisible(true);
}
if (HTXAFag5String.equals("Mat ")){
HTXBFagBox4.setVisible(false);
HTXBFagBox4.removeAllItems();
HTXBFagBox4.addItem("Intet ");
HTXBFagBox4.addItem("Mat ");

HTXAFagBox5.removeAllItems();
HTXAFagBox5.addItem("Engelsk");
HTXAFagBox5.addItem("Intet ");
HTXAFagBox5.setVisible(true);
}
if (HTXAFag5String.equals("Intet ")){
HTXBFagBox1.setVisible(true);
HTXBFagBox4.setVisible(true);
HTXBFagBox1.removeAllItems();
HTXBFagBox4.removeAllItems();
HTXBFagBox1.addItem("Engelsk");
HTXBFagBox4.addItem("Mat ");

}

}
}

public String HTXAFag5String(){
return HTXAFag5String;
}

String HTXAFag5String = "";

The program runs perfectly fine as long as I don´t try to redefine my String HTXAFag5String in private void HTXAFagBox5ActionPerformed(java.awt.event.ActionEvent evt)

Hope it makes more sence now, thanks in advance.

I don't understand this question either. When you say "runs perfectly fine as long as ..." can you specify exactly what do you observe that is "wrong", and what it should be to be "right"?

Sorry, I spent a time pouring over your code but connection between code and logic is avoiding me. I got that "major" combo box selection will affect selection options in "minor" combo box, but then you have number of other combo boxes that are fill in for unknown reason. Then you go on string definition in various places (I'm not really sure why because string can be pulled from combo box when ever necessary and new declaration of string is redundant) . Can you provide sample of how selection works?
Something like if Internet Computing major then minor can be Bussines, Multimedia, Languages, if major Publishing minor Languages, Multimedia, Bussines etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.