Re: extending JComboBox breaks getSelectedItem()
- From: Brandon McCombs <none@xxxxxxxx>
- Date: Tue, 23 Jan 2007 00:57:02 -0500
Andrew Thompson wrote:
Brandon McCombs wrote:
...
I've created my own JComboBox and implemented the ActionListener
interface for the new class. I defined the actionPerformed() and all
was well until I realized that calling comboBox.getSelectedItem() no
longer returns anything as long as I define comboBox to be of type
myJComboBox instead of JComboBox. I assume this is due to JComboBox
already implementing the ActionListener interface and I am essentially
overriding what used to be in actionPerformed() . Is there a method to
call in my actionPerformed() so I can still make the getSelectedItem()
work, something like super() if this was occurring in a constructor? Or
if something else is causing it to not work are there any tips for
solving it?
This one should work better for you:
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class myJComboBox extends JComboBox implements ActionListener {
private static final long serialVersionUID = 0L;
private boolean dirType = false;
public myJComboBox(ComboBoxModel mod) {
super(mod);
this.addActionListener(this);
}
public myJComboBox(String[] mod) {
super(mod);
this.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println(getSelectedItem() + " selected");
}
public static void main(String[] args) {
String[] namingContexts = new String[] { "" };
myJComboBox cjcb = new myJComboBox(namingContexts);
cjcb.setEditable(true);
JOptionPane.showMessageDialog(null, cjcb);
System.out.println(cjcb.getSelectedItem() + " custom cb, selected on exit");
String[] fruit = { "Apple", "Orange", "Banana" };
JComboBox cb = new JComboBox(fruit);
cb.setEditable(true);
JOptionPane.showMessageDialog(null, cb);
System.out.println(cb.getSelectedItem() + " default cb, selected on exit");
}
}
The line that prints "custom cb, selected on exit" won't print anything on exit despite there being something in the box (assuming you typed something). The other one will actually print out what you type after you exit.
It seems that when I create mine without any contents and allow the user to enter text in the box that when getSelectedItem() is called it doesn't return anything. Now I didn't try it but I'd guess that it isn't my custom class causing the problem (especially since your code worked fine). The problem would arise simply by wanting to call getSelectedItem() on a combo box that didn't have any items in its String[] or ComboModel. So the question is, is it possible to grab the text from a combobox if the text was simply entered by the user with no existing data in the combo list?
My combobox is allowed to have data in it that is entered programmatically but only after the user presses a button and the data is retrieved from a server. Short of that I allow the user to type in the data but I need to retrieve it eventually and with this problem that retrieval is impossible so far.
thanks
.
- Follow-Ups:
- Re: extending JComboBox breaks getSelectedItem()
- From: Andrew Thompson
- Re: extending JComboBox breaks getSelectedItem()
- References:
- extending JComboBox breaks getSelectedItem()
- From: Brandon McCombs
- Re: extending JComboBox breaks getSelectedItem()
- From: Andrew Thompson
- extending JComboBox breaks getSelectedItem()
- Prev by Date: Re: extending JComboBox breaks getSelectedItem()
- Next by Date: Re: extending JComboBox breaks getSelectedItem()
- Previous by thread: Re: extending JComboBox breaks getSelectedItem()
- Next by thread: Re: extending JComboBox breaks getSelectedItem()
- Index(es):