Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Combobox how to disable edit but allow select

Thread view: 
John_Woo - 27 Sep 2006 20:45 GMT
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?

--
Thanks
John
Toronto
Michael Rauscher - 27 Sep 2006 23:08 GMT
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
Jeffrey Schwab - 28 Sep 2006 00:30 GMT
>         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );

Thank you.

>         frame.setContentPane( comboBox );

:-)
Michael Rauscher - 28 Sep 2006 06:54 GMT
Jeffrey Schwab schrieb:

>>         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
>
[quoted text clipped - 3 lines]
>
> :-)

??? For what?

Bye
Michael
Jeffrey Schwab - 28 Sep 2006 15:39 GMT
> Jeffrey Schwab schrieb:
>>
>>>         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
>>
>> Thank you.

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).

>>>         frame.setContentPane( comboBox );
>>
>> :-)

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).

> ??? For what?
>
> Bye
> Michael
Michael Rauscher - 28 Sep 2006 15:57 GMT
Jeffrey Schwab schrieb:
>> Jeffrey Schwab schrieb:
>>>
[quoted text clipped - 5 lines]
> 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*

>>>>         frame.setContentPane( comboBox );
>>>
[quoted text clipped - 3 lines]
> 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
John_Woo - 28 Sep 2006 01:29 GMT
> John_Woo schrieb:
> > Hi,
[quoted text clipped - 24 lines]
> 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?

--
John
Michael Rauscher - 28 Sep 2006 07:05 GMT
John_Woo schrieb:
> class MyComboBox extends JComboBox implements ActionListener
> {
[quoted text clipped - 13 lines]
>
> 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
dnasmars@gmail.com - 28 Sep 2006 10:53 GMT
John_Woo a écrit :

> > John_Woo schrieb:
> > > Hi,
[quoted text clipped - 47 lines]
> --
> John
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
John_Woo - 28 Sep 2006 14:44 GMT
> John_Woo a écrit :
>
[quoted text clipped - 67 lines]
>
> 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
Michael Rauscher - 28 Sep 2006 15:34 GMT
John_Woo schrieb:
>> try this
>>
[quoted text clipped - 18 lines]
>
> 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
John_Woo - 28 Sep 2006 20:40 GMT
> John_Woo schrieb:
> >> try this
[quoted text clipped - 55 lines]
>      }
> }

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
Michael Rauscher - 29 Sep 2006 08:54 GMT
John_Woo schrieb:
> if MyComboBox is as following (without overwrite actionPerformed
> method)
[quoted text clipped - 29 lines]
> 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
John_Woo - 28 Sep 2006 18:38 GMT
> John_Woo a écrit :
>
[quoted text clipped - 67 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.