Reputation: 89
I have JComboBox loaded with objects, which have their own "toString" method. This combobox is used for selection and returning this objects with "ActionListener" on this combobox. All worked fine, until I decided to add function for dynamically adding new objects by typing text to this combobox and submitting with "Submit" button.
For example,
my class is:
public class SomeCustomClass {
private int id;
private String name;
public SomeCustomClass(String name){
this.name = name;
}
// getters and setters here
}
When I type text "Some test text" in the combobox and submit it, I want to have a this box with new object "SomeCustomClass", where name = "Some test text".
Variant 1 Create some custom from String to SomeCustomClass cast method. Is it possible? Is it a good idea?
Variant 2 Find a way to catch a String before in triggers ActionListener on combobox, make a new SomeCustomClass object with text and push it back to combobox again. But how? I haven't found method getString(getText) for JComboBox.
Variant 3 Your ideas...
I'm kind a new to Java, may be I've missed something.
Upvotes: 0
Views: 1151
Reputation: 347184
So, in 10 minutes of testing I found that...
JComboBox#getSelectedIndex
will return -1
if the value does not exist in the modelJComboBox#getSelectedVaue
will return a String
if the value does not exist in the modelSo using either one of these (or both), you should know when the value exists in the model or not. If it does not, you should be able to create a new object, passing in the String
value and adding it to the JComboBox
(assuming you're using a DefaultComboBoxModel
)
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
DefaultComboBoxModel<Fruit> fruitModel = new DefaultComboBoxModel<>();
fruitModel.addElement(new Fruit("Apple"));
fruitModel.addElement(new Fruit("Banana"));
fruitModel.addElement(new Fruit("Grapes"));
fruitModel.addElement(new Fruit("Pears"));
JComboBox cb = new JComboBox(fruitModel);
cb.setEditable(true);
add(cb);
cb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = cb.getSelectedIndex();
Object value = cb.getSelectedItem();
if (!(value instanceof Fruit)) {
System.out.println(value + " is not a fruit");
cb.addItem(new Fruit(value.toString()));
} else {
System.out.println(value + " is a fruit");
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
}
Upvotes: 1