Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #767
| From | "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: extending JComboBox b |
| Message-ID | <45b5a3b3$0$8937$4c368faf@roadrunner.com> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <1169528882.331614.277200@q2g2000cwa.googlegroups.com> |
| Date | 2011-04-27 15:29 +0000 |
| Organization | TDS.net |
To: comp.lang.java.gui
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
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
extending JComboBox break "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
Re: extending JComboBox b "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
Re: extending JComboBox b "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
Re: extending JComboBox b "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
Re: extending JComboBox b "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
Re: extending JComboBox b "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
csiph-web