Combobox how to disable edit but allow select

J

John_Woo

Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?
 
M

Michael Rauscher

John_Woo said:
Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?

import javax.swing.*;

public class Test {

public static final void main( String args[] ) throws Exception {
JComboBox comboBox = new JComboBox(
new String[]{"1.", "2.", "3."} );
comboBox.setEditable( false );
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( comboBox );
frame.pack();
frame.setVisible( true );
}
}

Works for me.

Bye
Michael
 
J

John_Woo

Michael said:
John_Woo said:
Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?

import javax.swing.*;

public class Test {

public static final void main( String args[] ) throws Exception {
JComboBox comboBox = new JComboBox(
new String[]{"1.", "2.", "3."} );
comboBox.setEditable( false );
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( comboBox );
frame.pack();
frame.setVisible( true );
}
}

Works for me.

Bye
Michael

Thanks lots, but my case

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A

}
}

without line A it works as yours, but with line A, it just couldn't
work (whenever select, it left it blank).

Can you fix it?
 
M

Michael Rauscher

John_Woo said:
class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A

}
}

without line A it works as yours, but with line A, it just couldn't
work (whenever select, it left it blank).

Can you fix it?

a) Why do you want to extend JComboBox? In most (99.9 %) cases you don't
need to. You can just _use_ it.

b) the problem is neither JComboBox nor the mentioned "line A".

import java.awt.event.*;
import javax.swing.*;

public class Test {

static class MyComboBox extends JComboBox
implements ActionListener {
public void actionPerformed( ActionEvent e ) {
System.out.println( "Action" );
}

public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this );
}
}

public static final void main( String args[] ) throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( new MyComboBox() );
frame.pack();
frame.setVisible( true );
}
}

c) If it really doesn't work when line A is present then I'd guess the
problem's in your action listener.

d) Next time please post an SSCCE [1] - otherwise we'll move in a circle.

Bye
Michael

[1] http://www.physci.org/codes/sscce
 
D

dnasmars

John_Woo a écrit :
Michael said:
John_Woo said:
Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?

import javax.swing.*;

public class Test {

public static final void main( String args[] ) throws Exception {
JComboBox comboBox = new JComboBox(
new String[]{"1.", "2.", "3."} );
comboBox.setEditable( false );
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( comboBox );
frame.pack();
frame.setVisible( true );
}
}

Works for me.

Bye
Michael

Thanks lots, but my case

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A

}
}

without line A it works as yours, but with line A, it just couldn't
work (whenever select, it left it blank).

Can you fix it?
try this

public class MyComboBox extends JComboBox implements ActionListener {
public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A
}

public void actionPerformed(ActionEvent e) {
System.out.println(" this is ugly ");
this.setForeground(Color.white);
}
}

I hope this helps
 
J

John_Woo

John_Woo a écrit :
Michael said:
John_Woo schrieb:
Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?

import javax.swing.*;

public class Test {

public static final void main( String args[] ) throws Exception {
JComboBox comboBox = new JComboBox(
new String[]{"1.", "2.", "3."} );
comboBox.setEditable( false );
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( comboBox );
frame.pack();
frame.setVisible( true );
}
}

Works for me.

Bye
Michael

Thanks lots, but my case

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A

}
}

without line A it works as yours, but with line A, it just couldn't
work (whenever select, it left it blank).

Can you fix it?
try this

public class MyComboBox extends JComboBox implements ActionListener {
public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A
}

public void actionPerformed(ActionEvent e) {
System.out.println(" this is ugly ");
this.setForeground(Color.white);
}
}

I hope this helps

That's it! Thank you.

Color.black is better.

Michael's example (using an static inner class ) is fine, just didn't
understand why for standalone class, we have to explixitly set the
foreend color.

John
 
M

Michael Rauscher

John_Woo said:
That's it! Thank you.

Color.black is better.

I don't know what you're doing but I know it must be something strange ;)
Michael's example (using an static inner class ) is fine, just didn't
understand why for standalone class, we have to explixitly set the
foreend color.

You do *not* need to. It doesn't make any difference if one uses a
static inner class or a top level class:

import java.awt.event.*;
import javax.swing.*;

class MyComboBox extends JComboBox implements ActionListener {
public void actionPerformed( ActionEvent e ) {
System.out.println( "Action" );
}

public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this );
}
}

public class Test {
public static final void main( String args[] ) throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( new MyComboBox() );
frame.pack();
frame.setVisible( true );
}
}

Again, post an SSCCE of the problem.

Bye
Michael
 
J

Jeffrey Schwab

For not using JFrame.EXIT_ON_CLOSE, and more to the point, for showing
the correct way to dispose of a Swing frame. I cringe every time I see
EXIT_ON_CLOSE, in the same way I cringe when I see System.exit(1).

The idea of setting the content pane to something that's only nominally
a container made me smile. I'm not knocking it; it makes sense in the
context of your example. I just would have expected frame.add(comboBox).
 
M

Michael Rauscher

Jeffrey said:
For not using JFrame.EXIT_ON_CLOSE, and more to the point, for showing
the correct way to dispose of a Swing frame. I cringe every time I see
EXIT_ON_CLOSE, in the same way I cringe when I see System.exit(1).

I thought the "thanks" goes to the fact, that the SSCCE exits and you
didn't have to use Ctrl-Break. *lol*
The idea of setting the content pane to something that's only nominally
a container made me smile. I'm not knocking it; it makes sense in the
context of your example. I just would have expected frame.add(comboBox).

I tend to use setContentPane in my examples because it works with 1.4,
too and it's shorter than frame.getContentPane().add(...) :)

Bye
Michael
 
J

John_Woo

John_Woo a écrit :
Michael said:
John_Woo schrieb:
Hi,

after setEditable(false) in a JComboBox, it can't allow to select a
item (display blank).

I'm wondering, how to disable the edit but with select function enable?

import javax.swing.*;

public class Test {

public static final void main( String args[] ) throws Exception {
JComboBox comboBox = new JComboBox(
new String[]{"1.", "2.", "3."} );
comboBox.setEditable( false );
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( comboBox );
frame.pack();
frame.setVisible( true );
}
}

Works for me.

Bye
Michael

Thanks lots, but my case

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A

}
}

without line A it works as yours, but with line A, it just couldn't
work (whenever select, it left it blank).

Can you fix it?
try this

public class MyComboBox extends JComboBox implements ActionListener {
public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A
}

public void actionPerformed(ActionEvent e) {
System.out.println(" this is ugly ");
this.setForeground(Color.white);
}
}

I hope this helps

That's it. Thank you.

setForeground(Color.black); is better.

just didn't know why Michael's example (using inner class) works fine,
but my case (using independent class ) has the foreground issue.

John
 
J

John_Woo

Michael said:
John_Woo said:
>> addItem("b");

That's it! Thank you.

Color.black is better.

I don't know what you're doing but I know it must be something strange ;)
Michael's example (using an static inner class ) is fine, just didn't
understand why for standalone class, we have to explixitly set the
foreend color.

You do *not* need to. It doesn't make any difference if one uses a
static inner class or a top level class:

import java.awt.event.*;
import javax.swing.*;

class MyComboBox extends JComboBox implements ActionListener {
public void actionPerformed( ActionEvent e ) {
System.out.println( "Action" );
}

public MyComboBox() {
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this );
}
}

public class Test {
public static final void main( String args[] ) throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( new MyComboBox() );
frame.pack();
frame.setVisible( true );
}
}


Thanks Michael,
if MyComboBox is as following (without overwrite actionPerformed
method)

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Test {
public static final void main( String args[] ) throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
MyComboBox my = new MyComboBox();
frame.setContentPane( my );
frame.pack();
frame.setVisible( true );
}

}

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A
}

}
then once selecting an item, the field always displays blank. I still
have no idea about it.

John
 
M

Michael Rauscher

John_Woo said:
if MyComboBox is as following (without overwrite actionPerformed
method)

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Test {
public static final void main( String args[] ) throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
MyComboBox my = new MyComboBox();
frame.setContentPane( my );
frame.pack();
frame.setVisible( true );
}

}

class MyComboBox extends JComboBox implements ActionListener
{
MyComboBox()
{
addItem("a");
addItem("b");
addItem("c");
setEditable( false );
addActionListener( this); //line A
}

}
then once selecting an item, the field always displays blank. I still
have no idea about it.

Ah, now *I* do understand. You really didn't override actionPerformed
(and according to the API you must not).

OK, let's see what's going on there.

First of all, we have to distinguish two things:
a) JComboBox's implementation of ActionListener
b) JComboBox's action listeners

If you select an element from the list, JComboBox#setSelectedIndex gets
called which in turn calls JComboBox#setSelectedItem which in turn fires
an action event (that is to notify every ActionListener which is on
JComboBox's listener list). Note that a) is not affected by this.

What you do is to add JComboBox's implementation of ActionListener (a)
to it's own listener list (b). Now, JComboBox's implementation of
ActionListener will be included in the event processing chain mentioned
above.

JComboBox's actionPerformed() gets the item from it's editor and updates
the data model (among other things).

But what's the editor's item of an uneditable JComboBox? It's an empty
string. And that's the result.

So, the solution is pretty clear: don't do anything with JComboBox's
implementation of ActionListener :)

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top