Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #764 > unrolled thread

extending JComboBox break

Started by"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
First post2011-04-27 15:29 +0000
Last post2011-04-27 15:29 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  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

#764 — extending JComboBox break

From"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
Subjectextending JComboBox break
Message-ID<45b5940b$0$28066$4c368faf@roadrunner.com>
  To: comp.lang.java.gui
hello,

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?

Here is my custom JComboBox class:

private class myJComboBox extends JComboBox implements ActionListener {
	private static final long serialVersionUID = 0L;
	public myJComboBox(ComboBoxModel mod) {
		super(mod);
	}
	public myJComboBox(String[] mod) {
		super(mod);
	}
		
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("dirType")) {
		//directoryType is a JComboBox
		String d =
		(String)directoryType.getSelectedItem();
			if (!d.equals(""))
				dirType = true;
			else
				dirType = false;
		}
		if (host && port && hostType && dirType)
			fetchBtn.setEnabled(true);
		else
			fetchBtn.setEnabled(false);
	}
}

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

[toc] | [next] | [standalone]


#765 — Re: extending JComboBox b

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectRe: extending JComboBox b
Message-ID<1169528882.331614.277200@q2g2000cwa.googlegroups.com>
In reply to#764
  To: comp.lang.java.gui
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?
>
> Here is my custom JComboBox class:

It took me so long to get it to compile and run,
that by the time I had seen it on-screen, it
failed to dispay the problem you described.

Please consider the benefit of posting SSCCE's*
in future, to save time for those people who are
trying to help..

In any case, my altered code which 'fails to
demonstrate the problem', is..

<sscce>
import java.awt.event.*;
import javax.swing.*;

class CustomJComboBox extends JComboBox implements ActionListener {

  public CustomJComboBox(ComboBoxModel mod) {
    super(mod);
    this.addActionListener(this);
  }

  public CustomJComboBox(String[] mod) {
    super(mod);
    this.addActionListener(this);
  }


  public void actionPerformed(ActionEvent e) {
     System.out.println(e);
     System.out.println( getSelectedItem() );
  }

  public static void main(String[] args) {
      String[] fruit = {
        "Apple",
        "Orange",
        "Banana"
      };
      CustomJComboBox cjcb = new CustomJComboBox(fruit);
      JOptionPane.showMessageDialog(null, cjcb);
    }
}
</sscce>

* <http://www.physci.org/codes/sscce>

Andrew T.

---
 * 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

[toc] | [prev] | [next] | [standalone]


#767 — Re: extending JComboBox b

From"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectRe: extending JComboBox b
Message-ID<45b5a3b3$0$8937$4c368faf@roadrunner.com>
In reply to#765
  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

[toc] | [prev] | [next] | [standalone]


#768 — Re: extending JComboBox b

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectRe: extending JComboBox b
Message-ID<1169532354.755893.257230@11g2000cwr.googlegroups.com>
In reply to#767
  To: comp.lang.java.gui
Brandon McCombs wrote:
...
> This one should work better for you:

I suspect Micheal has suggested the correct solution
(I was almost about to try that myself, but stopped
when I did not observe the behaviour).

Let us know if the call to super does not fix it,
and I will investigate your new example further.

Andrew T.

---
 * 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

[toc] | [prev] | [next] | [standalone]


#766 — Re: extending JComboBox b

From"Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectRe: extending JComboBox b
Message-ID<ep46tu$r8d$1@registered.motzarella.org>
In reply to#764
  To: comp.lang.java.gui
Brandon McCombs schrieb:
> hello,
> 
> I've created my own JComboBox and implemented the ActionListener 

Why?

> 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 

Sure: super.actionPerformed(e); where e is a reference to the ActionEvent.

Bye
Michael

---
 * 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

[toc] | [prev] | [next] | [standalone]


#769 — Re: extending JComboBox b

From"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
Date2011-04-27 15:29 +0000
SubjectRe: extending JComboBox b
Message-ID<45b5aa32$0$28117$4c368faf@roadrunner.com>
In reply to#766
  To: comp.lang.java.gui
Michael Rauscher wrote:
> Brandon McCombs schrieb:
>> hello,
>>
>> I've created my own JComboBox and implemented the ActionListener 
> 
> Why?
> 
>> 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 
> 
> Sure: super.actionPerformed(e); where e is a reference to the ActionEvent.
> 
> Bye
> Michael

Yes, that did the trick. Thanks Michael.  For some reason my brain just 
wasn't thinking to fix it that way but at least I was on the right track 
with using super(), just using the wrong train.

---
 * 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web