How to know what you selected in the JComboBox - java

I am making a Basic calculator app in java which has 2 JTextFields and 1 JComboBox. What i want to know if there is a way to let a JButton detect what you selected in the JComboBox, when i did it with the text field it looked something like this
static String divide = "/";
if (n == JOptionPane.OK_OPTION) {
if (symbol.getText().equals(divide)){
<code>
}
}
So is there a similar way to do this with JComboBoxs??
String[] symbols = {times, minus, plus, divide};
That's the JComboBox's content code.

You can get selected item from JComboBox with method .getSelectedItem().
Say you have String[] symbols = {times, minus, plus, divide}; as an input when constructing JComboBox (see constructor JComboBox(E[] items) )
JComboBox jcb = new JComboBox(symbols);
//you will see the string you selected
System.out.println(jcb.getSelectedItem());

Perhaps use action listener
String[] quantities1 = {"/","+"};
JComboBox comboBox = new JComboBox(quantities1);
comboBox.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//do stuff when a section is performed
//you can use comboBox.getSelectedItem() to get the selected value
}
}
);

Related

Getting input from multiple button groups in Java

I have an array of question, an array of possible answers and an array of properties I want to find out by answering the questions
String[] questions = new String[]{"Question1", "Question2", "Question3"};
String[] possibleAnswers = new String[]{"yes,no", "yes,no", "big,small"};
String[] properties = new String[]{"", "", ""};
I created a label for every question and JRadioButtons for every answer for that question by using split on the corresponding element in the possibleAnswers array.
for (int i = 0; i < questions.length; i++) {
//label that holds the current question
JLabel questionLabel = new JLabel(questions[i]);
questionPanel.add(questionLabel);
// string that holds answers for current question i.e. {yes, no}
String[] currentQuestionAnswers = possibleAnswers[i].split(",");
ButtonGroup buttonGroup = new ButtonGroup();
for (int j = 0; j < currentQuestionAnswers.length; j++) {
JRadioButton btnRadio = new JRadioButton(currentQuestionAnswers[j]);
// action listener that will store the selected answer and the question
btnRadio.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
answer = btnRadio.getText();
// some code that moves answer at the right index in the properties array
}
});
buttonGroup.add(btnRadio);
questionPanel.add(btnRadio);
}
}
This image shows what I want it to look like.
For each question I want to find a property. Let's say {Question1, Question2, Question3} are actually these questions {"talks?", "expensive?", "dimension?"}. When I get the text from the selected buttons, I'll have talks -> yes, expensive -> no, dimension -> big, so the properties array will become {"yes", "no", "big"}.
I know how to get the selected answer, but I can't figure out how to find the question that corresponds to that answer. I thounght I can somehow use the button groups I created, but I don't know how.
I hope this makes sense and someone can help. Thank you.
One way to do it would be to extend JRadioButton and add a simple ID field (a tag). I can't test this code right now but I think
it should work.
class MyRadioButton extends JRadioButton{
private int tag;
public getTag(){ return tag;}
public setTag(int val){ tag = val}
}
And then in the actionPerfomed method, just check this tag and take an appropriate action based on it.
int tag = 0;
btnRadio.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
answer = btnRadio.getText();
switch(btnRadio.getTag()){
case 0:
//do some action
break;
case 1:
....
}
// some code that moves answer at the right index in the properties array
}
});
btnRadio.setTag(tag++);//this will set a unique tag for each radio button
buttonGroup.add(btnRadio);
Alternatively, if you don't want to extend JRadioButton,which arguably might be overkill for your use case, you can use RadioButton.setActionCommand and ActionEvent.getActionCommand
btnRadio.setActionCommand("some_unique_string")
and then just check this command string in actionPerformed
if("some_unique_string".equals(ae.getActionCommand())
//do something

Selecting Items in JComboBox

I have two combo boxes with a list of distance units to convert from/to. Now, when I want to choose eg. "Centimeter" from the ComboBox From (distance)..., I really don't need to have "Centimeter" in ComboBox To (distance)..., because it doesn't make sense to convert from centimeter to centimeter.
So when "Centimeter" is selected in From (distance)... I want it to be removed in To (distance)... ComboBox. But, when I change my selection (say "Meter"), I want "Centimeter" to be back and "Meter" disappear etc.
I managed to remove the selected item in To (distance)... box, but don't know how to return it back when I change my selection. Besides, when I change selection, the code below just removes the corresponding item in To (distance)... ComboBox.
Please guide me to the correct solution. Here is the corresponding code. I can give you the whole code if you need. Thank you!
private String[] convertFromDistance = {"From (distance)...", "Centimeter", "Inch", "Kilometer", "Knot", "Meter", "Mile", "Millimeter", "Yard"};
private String[] convertToDistance = {"To (distance)...", "Centimeter", "Inch", "Kilometer","Knot", "Meter", "Mile", "Millimeter", "Yard"};
private JComboBox fromListDistance, toListDistance;
fromListDistance.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String list = (String)fromListDistance.getSelectedItem();
for (int i=0; i<convertToDistance.length; i++) {
if (convertToDistance[i].equals(list)) {
toListDistance.removeItem(convertToDistance[i]);
//here should go the code for adding back the item if selection is changed
}
}
}
});
toListDistance = new JComboBox<String>(convertToDistance);
first thing you i should do in your problem is to know what is the selected choice from fromListDistance combo box ..
after that i have to refill the toListDistance combo box except for the choice that the user have selected ..
it's easy to do this with if statement
fromListDistance.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//saving the selected choice
String choise=fromListDistance.getSelectedItem().toString();
//here we remove all items from the combo box
toListDistance.removeAllItems();
for (int i = 0; i < convertToDistance.length; i++) {
String distance=convertToDistance[i];
//compare the selected choice with the convertToDistance[i]
if (choise.equals(distance)) {
continue;
}
toListDistance.addItem(distance);
}
}
});

simplier way to add jbutton to jtable

well i am making this system that has a table, and i have to put buttons in the last column. i've been researching but all the codes i saw are really confusing. there is one tho, but there are still some parts that i didn't understand. here's the site where i got it http://www.javaquery.com/2013/05/how-to-implement-jbutton-in-jtable.html
String[] InvoiceArray = new String[20];
//Declare above variable globally. Used by two-three methods. Change variable name as per your need.
/*
* import the ButtonColumn class if you are not working in IDE
* I used formWindowOpened event to load content in Jtable but you can use other event.
* All you need is put the code with in that event.
*/
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Object[][] rowData = new Object[4][2]; // 4: is number of row ; 2: is number of column
Object columnNames[] = {"Invoice No", "View Report"}; // Name of columns
for (int i = 0; i < 4; i++) {
InvoiceArray[i] = i + "-2345";
rowData[i][0] = i + "-2345";
rowData[i][1] = "View Order " + i; // Can change the text of button.
}
DefaultTableModel tm = new DefaultTableModel(rowData, columnNames);
jTable1.setModel(tm);
ButtonColumn buttonColumn = new ButtonColumn(jTable1, showOrder, 1); // 1: is column number. column count starts with 0,1,2...
}
what's the InvoiceArray for? and should i make the showOrder from the last line? and also, i didn't understand the code he posted on how to make a listener on it. here it is:
Action showOrder = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//JTable table = (JTable) e.getSource(); // If you have multiple component following the ActionEvent
int modelRow = Integer.valueOf(e.getActionCommand());
if (InvoiceArray[modelRow] != null) {
/* We are placing invoice no in array
* And track the button click index
* And fetch index in invoice no
*/
System.out.println("Your Invoice No:" + InvoiceArray[modelRow]);
} else {
JOptionPane.showMessageDialog(rootPane, "No records found!");
}
}
};
i know there are some explanations already. i understand some of them but not all. just a simplier way to add jbutton on jtable and also listeners for the jbutton. thank you so much
just a simplier way to add jbutton on jtable and also listeners for the jbutton.
There is no simple way. You need to understand how renderers and editors work in a JTable. Read the section from the Swing tutorial on Concepts: Renderers and Editors for the basics.
Then you can check out Table Button Column which does the hard work for you. You only need to provide the Action to be invoked when you click on the button.
what's the InvoiceArray for?
It is used to load data into the JTable. This is basic usage of a JTable and has absolutely nothing to do with adding a button to a column of the table.
After the data is loaded you should forget about the invoiceArray. The Action you write should access the data via the TableModel or the JTable.

Two JComboBoxes user can't choose same value

I've got two JComboBox. First one has model with elements "one", "two", "three" and second has model with elements "three", "four". And when user choose element "three" in first JComboBox, he can't choose element "three" in second one JComboBox. And in reverse - when user choose element "three" in second one JComboBox, he can't choose same element in first one CB. How can i do this in Java ?
Thank you for answers.
One more question. When i dynamically create ComboBoxes (when someone clicked the button - in button actionListener), and every new ComboBox has same model - same list of elements. How can i check this case ? Same like Blip said ?
In my opinion you could add ItemListener to the 2 JComboBox. If the user selects the object "three" in any of the JComboBox then the object "three" could be removed from the other JComboBox's model. And if the user deselects the object "three" in one JComboBox then the object "three" can be added to the model of the other JComboBox.
You could implement in the following way:
Lets have have the two JComboBox stored in variables box1 and box2 which could be implemented as :
JComboBox<String> box1 = new JComboBox<>(new String[]{"one", "two", "three"});
JComboBox<String> box2 = new JComboBox<>(new String[]{"three", "four"});
Now add itemListener to both these JComboBox and pass it to a method say boxItemSelected:
box1.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
box2.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
Now to implement the boxItemSelected(e)
void boxItemSelected(ItemEvent e){
//Check the item selected/deselected is "three" else do nothing.
if (e.getItem().equals("three")){
//Find the box on which this action was not performed to change its model.
JComboBox<String> oppositeBox;
if(e.getSource().equals(box1)){
oppositeBox = box2;
}else{
oppositeBox = box1;
}
//Check the item is selected or deselected to remove or add "three" to item list.
if(e.getStateChange() == ItemEvent.SELECTED){
oppositeBox.removeItem("three");
}else{
oppositeBox.addItem("three");
}
}
}
Addition Information
Here if you have more than 1 items that are overlapping and can be selected only in one JComboBox, a slight modification of the method boxItemSelected could be take care of your problem as Illustrate below:
Change the line in the above code from:
if (e.getItem().equals("three"))
to
if (e.getItem().equals("three") || e.getItem().equals("<new item>") || ....)
And change
oppositeBox.removeItem("three");
oppositeBox.addItem("three");
to
oppositeBox.removeItem(e.getItem());
oppositeBox.addItem(e.getItem());
Here under any circumstance the user cannot select the same items in both the JComboBox. And all these happen behind the scene without the knowledge of the user using the user-interface.
When i dynamically create ComboBoxes (when someone clicked the button
- in button actionListener), and every new ComboBox has same model - same list of elements. How can i check this case ? Same like Blip said
?
In response to the above I am considering that all the items in all the JComboBox same and are stored in a List variable, and if 1 item is selected in any one of the JComboBox then that item cannot be selected in rest of the JComboBox. If my assumption is correct, then I suggest you do the following:
Create a List<JComboBox<?>> say boxes and initialise it as below:
List<JComboBox<?>> boxes = new ArrayList<>();
In your JButton (the button that dynamically JComboBox) variable's ActionListener implementation create a variable say items as an instance of List to store the items of the JComboBox and add the items
List<String> items = new Vector<>();
items.add("One");
items.add("Two");
......
Now remove the items from from the items variable that are selected in other JComboBox that are dynamically generated:
Iterator<JComboBox<?>> iterator = boxes.iterator();
while(iterator.hasNext()){
JComboBox<?> existing = iterator.next();
items.remove(existing.getSelectedItem());
}
Now after initialisation of the JComboBox instance say box set the Model of the box to the previously initialised and trimmed List variable items
box.setModel(new DefaultComboBoxModel(items));
now add the JComboBox variable box to the List variable boxes:
boxes.add(box);
Also in this above mentioned ActionListener implementation add the ItemListener to box, the newly instantiated variable of JComboBox and pass it to the method boxItemSelected:
box.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
Now the implementation of the boxItemSelected has to be changed to accommodate the changes:
void boxItemSelected(ItemEvent e){
//Create an iterator to iterate over the boxes
Iterator<JComboBox<?>> iterator = boxes.iterator();
while(iterator.hasNext()){
//Get the current instance of comboBox from the list
JComboBox<?> current = iterator.next();
//If the box in which the select or de-select
//event has occurred is the current comboBox then do nothing.
if(e.getSource().equals(current)(
continue;
}
//If the event is select then remove the Item from the
//current comboBox else add the Item to the current comboBox.
if(e.getStateChange() == ItemEvent.SELECTED){
current.removeItem(e.getItem());
}else{
current.addItem(e.getItem());
}
}
}
So this is definitely possible. The best way I think would be to check the selected index of both boxes and then perform your logic based on the results of that index value. In the example below you can see that demonstrated. What is happening is there is an ActionListener on ComboBox1 and 2. If the values match, I used index but you can also get the string value to make sure they don't match up.
public class SOF extends JFrame {
private JPanel mainPanel, comboPanel;
private JComboBox jcb1, jcb2;
public SOF()
{
super("Combo Box Example");
mainPanel = new JPanel();
mainPanel.add(configureCombo());
add(mainPanel);
setSize(200,200);
setLocationRelativeTo(null);
setVisible(true);
jcb1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
//You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random.
if((jcb1.getSelectedIndex() == 2) && (jcb2.getSelectedIndex() == 0))
{
JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
jcb1.setSelectedIndex(-1);
jcb2.setSelectedIndex(-1);
}
//ANOTHER OPTION
If((jcb1.getSelectedValue().equals("three") && (jcb2.getSelectedValue().equals("three")
{
LOGIC
}
}
});
jcb2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
//You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random.
if((jcb2.getSelectedIndex() == 0) && (jcb1.getSelectedIndex() == 2))
{
JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
jcb1.setSelectedIndex(-1);
jcb2.setSelectedIndex(-1);
}
}
});
}
private JPanel configureCombo()
{
String[] cb1List = {"one", "two", "three"};
String[] cb2List = {"three", "four"};
comboPanel = new JPanel(new GridLayout(1,2));
jcb1 = new JComboBox(cb1List);
jcb2 = new JComboBox(cb2List);
comboPanel.add(jcb1);
comboPanel.add(jcb2);
return comboPanel;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
SOF s = new SOF();
}
}
On either one if matching boxes are selected an error message will pop up and deselect both comboboxes.
You could futz with the combobox's model, but easier would be to just check the selected values when the user asks the program to accept the values, perhaps in the ActionListener of a JButton, and then if two of the same values have been selected, deselect them and warn the user with a JOptionPane. Alternatively, you could have code in listeners (either Actionlistener or ItemListener) added to both JComboBox's that check the other combobox's selected value to make sure that they are not the same, and if so, warn the user and deselect the erroneous selection.

how can I detect which combobox state changed?

I have two JComboBox in a form and I added an ItemListener to them and I should overwrite itemStateChanged(), now I wanna say if the first JComboBox items selected do something and else if the second JComboBox items selected do another thing, but I don't know how? Maybe code can help you.
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
In the second line of code I don't know how to recognize which JComboBox state has changed.
You can use ItemEvent#getSource()
Example:
public void itemStateChanged(ItemEvent e) {
if(e.getSource() instanceof JComboBox){
JComboBox combo = (JComboBox) e.getSource();
//rest of code
}
Now for distinct combo1 from combo2 , you have 2 options , you can set names to that components like this.
combo1.setName("combo1");
combo2.setName("combo2");
And in the itemListener
if(e.getSource() instanceof JComboBox){
JComboBox combo = (JComboBox) e.getSource();
if("combo1".equals(combo.getName())){
// your code
}
.
.// rest of code
}
Or if you know that they are the same instance, then you can always use ==.
if(combo1 == e.getSource() ){
// your code
}else if (combo2 == e.getSource()){
//code for combo 2
}
There are two ways to do that, the first is to check the source on the event object and see which combo box it matches to.
The alternative is to add a different listener into each combo box, then you know that any calls going into one listener are from the corresponding control. This is a good use for an anonymous inner class.

Categories

Resources