Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why "Enter" key does not fire JButton??

1,200 views
Skip to first unread message

tszeto

unread,
Dec 28, 2001, 6:03:17 PM12/28/01
to
Hello,

I just noticed something strange. When I hit the "Enter" key, my in-focus
JButton does not fire. Clicking with my mouse works. And so does hitting the
"Space" bar.

Do I need to re-assign key-mapping? I thought the default behavior was
hitting "Enter" to fire?

Any help appreciated.

Thanks,
Ted


wrightkevin

unread,
Dec 29, 2001, 1:23:00 AM12/29/01
to
Hi Ted,

Does this give you what you are after? -

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestEnter{
public static void main(String[] args){
JFrame frame = new JFrame();
Container cont = frame.getContentPane();
cont.setLayout(new FlowLayout() );

JButton button = new JButton("Click me!");
button.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println("button was clicked");
}
});

JRootPane jf = frame.getRootPane();
cont.add(button);

JButton b1, b2, b3;
b1 = new JButton("b1");
b2 = new JButton("b2");
b3 = new JButton("b3");

cont.add(b1); cont.add(b2); cont.add(b3);

jf.setDefaultButton(button);

frame.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setBounds(20,20,400,200);
frame.setVisible(true);
}
}

HTH

Kevin Wright.

"tszeto" <tsz...@mindspring.com> wrote in message
news:a0itjl$gea$1...@slb6.atl.mindspring.net...

tszeto

unread,
Dec 29, 2001, 2:47:14 AM12/29/01
to
Kevin,

Thanks for the reply. After looking more carefully through Google, I found
what I was after. The code below lets the "Enter" key fire off any button
that's in focus.

UIManager.put("Button.focusInputMap", new UIDefaults.LazyInputMap(new
Object[] {
"ENTER", "pressed",
"released ENTER", "released"
}));

You need to place this before the buttons you initialize.

"wrightkevin" <wrigh...@ntlworld.com> wrote in message
news:ajdX7.29$ll6....@news6-win.server.ntlworld.com...

wrightkevin

unread,
Dec 29, 2001, 3:35:07 AM12/29/01
to
Ted,

Tried that - seems to work a treat - Thanks for the info!

Kevin Wright.


"tszeto" <tsz...@mindspring.com> wrote in message

news:a0jsa4$63d$1...@nntp9.atl.mindspring.net...

Niko Schwarz

unread,
Dec 29, 2001, 7:39:07 AM12/29/01
to
tszeto wrote:

Its not a bug... its a feature.
A button is fired when you hit space, iirc, and the key-behaviour is, so i
think, platform-specific.

even under ms-win enter does not fire a button with focus, but instead the
*standard* button. since you wont find that in java, enter doesnt do
anything with a button.

regards,

nick

--
Antonym, n.:
The opposite of the word you're trying to think of.

Christian Kaufhold

unread,
Dec 29, 2001, 9:30:32 AM12/29/01
to
Hello!

Niko Schwarz <niko.s...@gmx.net> wrote:

> Its not a bug... its a feature.

> A button is fired when you hit space, iirc, and the key-behaviour is, so i
> think, platform-specific.

Or, in Swing, LAF-specific.

> even under ms-win enter does not fire a button with focus, but instead the
> *standard* button. since you wont find that in java, enter doesnt do
> anything with a button.

Then what is JRootPane.setDefaultButton() for?


Christian

wrightkevin

unread,
Dec 29, 2001, 1:27:55 PM12/29/01
to
Hi,
Best as I can tell it allows a specific button to be allocated as the
'default button' so when you press the 'enter/return' key its registered
actionListener method fires. I'm sure more informed guys can tell
us more though!

Kevin Wright.

"Christian Kaufhold" <use...@chka.de> wrote in message
news:3t3c2dd32...@aves.chka.de...

wrightkevin

unread,
Dec 29, 2001, 1:31:48 PM12/29/01
to
Hi again,

I guess I should have added to "key its registered
actionListener method fires" - irrespective of the gui's
current focus.

Kevin Wright.

"Christian Kaufhold" <use...@chka.de> wrote in message
news:3t3c2dd32...@aves.chka.de...

Christian Kaufhold

unread,
Dec 30, 2001, 9:55:54 AM12/30/01
to
Hello!

wrightkevin <wrigh...@ntlworld.com> wrote:

[JRootPane default button]

> Best as I can tell it allows a specific button to be allocated as the
> 'default button' so when you press the 'enter/return' key its registered
> actionListener method fires. I'm sure more informed guys can tell
> us more though!


It was a rhetorical question.

The "usual" key strokes seem to be "space" for choosing the currently
focused button and "enter" to choose the current default button (which
often happens to be the same than the focused one).

If you want to change this, you most likely will create incompabilities,
for example, the default button cannot be pressed at all because the
focused button also (first) reacts to the same key stroke and then
swallows it.

If the keyboard assignments provided by the LAF are unusual for the (ori-
ginal) LAF, complain at the LAF provider. If they are not, why change
them (see above)?


Christian

Christian Kaufhold

unread,
Dec 29, 2001, 9:30:32 AM12/29/01
to
Hello!

Niko Schwarz <niko.s...@gmx.net> wrote:

> Its not a bug... its a feature.

> A button is fired when you hit space, iirc, and the key-behaviour is, so i
> think, platform-specific.

Or, in Swing, LAF-specific.

> even under ms-win enter does not fire a button with focus, but instead the
> *standard* button. since you wont find that in java, enter doesnt do
> anything with a button.

Then what is JRootPane.setDefaultButton() for?


Christian

========= WAS CANCELLED BY =======:
Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!t-online.de!newscore.univie.ac.at!news.mailgate.org!zur.uu.net!ash.uu.net!dfw.uu.net!sea.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: use...@chka.de (Christian Kaufhold)
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <3t3c2dd32...@aves.chka.de>
Control: cancel <3t3c2dd32...@aves.chka.de>
Date: Wed, 2 Jan 2002 02:07:37 GMT
Organization: Navix Internet Subscribers
Lines: 2
Message-ID: <cancel.3t3c2...@aves.chka.de>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949456 28325 166.102.15.34 (2 Jan 2002 05:30:56 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:30:56 GMT
X-No-Archive: yes
Comment: Dude, where's my NewsAgent?
Xref: news.uni-stuttgart.de control:40289927

autocancel

wrightkevin

unread,
Dec 29, 2001, 1:27:55 PM12/29/01
to
Hi,

Best as I can tell it allows a specific button to be allocated as the
'default button' so when you press the 'enter/return' key its registered
actionListener method fires. I'm sure more informed guys can tell
us more though!

Kevin Wright.

"Christian Kaufhold" <use...@chka.de> wrote in message
news:3t3c2dd32...@aves.chka.de...

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsfeed1.earthlink.net!newsfeed.earthlink.net!uunet!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: "wrightkevin" <wrigh...@ntlworld.com>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <TWnX7.9854$Zg2.4...@news11-gui.server.ntli.net>
Control: cancel <TWnX7.9854$Zg2.4...@news11-gui.server.ntli.net>
Date: Wed, 2 Jan 2002 05:04:26 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.TWnX7.9854$Zg2.4...@news11-gui.server.ntli.net>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949328 28325 166.102.15.34 (2 Jan 2002 05:28:48 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:28:48 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

autocancel

Niko Schwarz

unread,
Dec 29, 2001, 7:39:07 AM12/29/01
to
tszeto wrote:

Its not a bug... its a feature.


A button is fired when you hit space, iirc, and the key-behaviour is, so i
think, platform-specific.

even under ms-win enter does not fire a button with focus, but instead the

*standard* button. since you wont find that in java, enter doesnt do
anything with a button.

regards,

nick

--
Antonym, n.:
The opposite of the word you're trying to think of.

========= WAS CANCELLED BY =======:
Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news2.euro.net!uunet!ash.uu.net!sac.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: Niko Schwarz <niko.s...@gmx.net>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <a0kdag$lh74p$1...@ID-42792.news.dfncis.de>
Control: cancel <a0kdag$lh74p$1...@ID-42792.news.dfncis.de>
Date: Wed, 2 Jan 2002 02:26:50 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.a0kdag$lh74p$1...@ID-42792.news.dfncis.de>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949518 28325 166.102.15.34 (2 Jan 2002 05:31:58 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:31:58 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40290105

autocancel

wrightkevin

unread,
Dec 29, 2001, 1:31:48 PM12/29/01
to
Hi again,

I guess I should have added to "key its registered
actionListener method fires" - irrespective of the gui's
current focus.

Kevin Wright.

"Christian Kaufhold" <use...@chka.de> wrote in message
news:3t3c2dd32...@aves.chka.de...

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool1-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsfeed.direct.ca!look.ca!newsfeed1.earthlink.net!newsfeed.earthlink.net!uunet!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: "wrightkevin" <wrigh...@ntlworld.com>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <r_nX7.9876$Zg2.4...@news11-gui.server.ntli.net>
Control: cancel <r_nX7.9876$Zg2.4...@news11-gui.server.ntli.net>
Date: Wed, 2 Jan 2002 03:48:42 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.r_nX7.9876$Zg2.4...@news11-gui.server.ntli.net>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949324 28325 166.102.15.34 (2 Jan 2002 05:28:44 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:28:44 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

autocancel

Christian Kaufhold

unread,
Dec 30, 2001, 9:55:54 AM12/30/01
to
Hello!

wrightkevin <wrigh...@ntlworld.com> wrote:

[JRootPane default button]

> Best as I can tell it allows a specific button to be allocated as the


> 'default button' so when you press the 'enter/return' key its registered
> actionListener method fires. I'm sure more informed guys can tell
> us more though!

It was a rhetorical question.

The "usual" key strokes seem to be "space" for choosing the currently
focused button and "enter" to choose the current default button (which
often happens to be the same than the focused one).

If you want to change this, you most likely will create incompabilities,
for example, the default button cannot be pressed at all because the
focused button also (first) reacts to the same key stroke and then
swallows it.

If the keyboard assignments provided by the LAF are unusual for the (ori-
ginal) LAF, complain at the LAF provider. If they are not, why change
them (see above)?


Christian

========= WAS CANCELLED BY =======:

Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news2.euro.net!uunet!ash.uu.net!sac.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: use...@chka.de (Christian Kaufhold)
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <0t3c2f29b...@aves.chka.de>
Control: cancel <0t3c2f29b...@aves.chka.de>
Date: Wed, 2 Jan 2002 02:49:54 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.0t3c2...@aves.chka.de>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009948936 28325 166.102.15.34 (2 Jan 2002 05:22:16 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:22:16 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40288822

autocancel

wrightkevin

unread,
Dec 29, 2001, 3:35:07 AM12/29/01
to
Ted,

Tried that - seems to work a treat - Thanks for the info!

Kevin Wright.


"tszeto" <tsz...@mindspring.com> wrote in message

news:a0jsa4$63d$1...@nntp9.atl.mindspring.net...

========= WAS CANCELLED BY =======:
Path: news.uni-stuttgart.de!dns.phoenix-ag.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.media.kyoto-u.ac.jp!newsfeed.mesh.ad.jp!uunet!osa.uu.net!sac.uu.net!lax.uu.net!news.navix.net!u.n.a.c.4.n.c.3.l.l.e.r
From: "wrightkevin" <wrigh...@ntlworld.com>
Newsgroups: news.admin.censorship,alt.test,comp.lang.java.programmer
Subject: cmsg cancel <2ffX7.103$ll6....@news6-win.server.ntlworld.com>
Control: cancel <2ffX7.103$ll6....@news6-win.server.ntlworld.com>
Date: Wed, 2 Jan 2002 05:12:13 GMT


Organization: Navix Internet Subscribers
Lines: 2

Message-ID: <cancel.2ffX7.103$ll6....@news6-win.server.ntlworld.com>
NNTP-Posting-Host: 166.102.15.34
X-Trace: iac5.navix.net 1009949597 28325 166.102.15.34 (2 Jan 2002 05:33:17 GMT)
X-Complaints-To: ab...@navix.net
NNTP-Posting-Date: 2 Jan 2002 05:33:17 GMT


X-No-Archive: yes
Comment: Dude, where's my NewsAgent?

Xref: news.uni-stuttgart.de control:40290269

autocancel

wrightkevin

unread,
Jan 4, 2002, 1:40:12 PM1/4/02
to
Hi Christian,

So there I was (today) developing a "Print all known fonts" application.
Guess what I thought might come in handy? Answer - The ability to
designate a single button as the 'Primary button' (without regard to
the gui's current focus) sound familiar?

Am including my source code to try to convince you that the method
might have a use after all.

Interested in your thoughts.

Kevin Wright.

import javax.swing.*;
import javax.swing.text.*;

import java.awt.*;
import java.awt.event.*;

import java.util.*;


public class Jtp {

static java.util.List fontList;

public static void main(String[] args) {

final JFrame frame = new JFrame();
// aha yes, this is the line!
JRootPane jr = frame.getRootPane();
final Label statusBar = new Label();

fontList = new ArrayList();

String[] ftList =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();

for(int i=0; i<ftList.length;i++){
fontList.add(ftList[i]);
}

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

final JTextPane jtp = new JTextPane();

jtp.setBackground(Color.black);
jtp.setForeground(Color.white);

Container cont = frame.getContentPane();
JScrollPane jsp = new JScrollPane(jtp);
JLabel label = new JLabel("Font Size:");
final JTextField tf = new JTextField(5);
Panel topPanel = new Panel();
final Document doc = jtp.getDocument();
JButton jb = new JButton("Font Listing");

// and of course - this.
jr.setDefaultButton(jb);

JButton foreButton = new JButton("Foreground Colour");

foreButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

Color fColour = JColorChooser.showDialog(frame,
"Colour Chooser",
Color.black);

jtp.setForeground(fColour);
}
});

JButton backButton = new JButton("Background Colour");

backButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

Color bColour = JColorChooser.showDialog(frame,
"Colour Chooser",
Color.black);

jtp.setBackground(bColour);
}
});

final Style def =
StyleContext.getDefaultStyleContext()
.getStyle(StyleContext.DEFAULT_STYLE);

jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

statusBar.setText("Processing Fonts");
try {
doc.remove(0, doc.getLength());

for (Iterator it= fontList.iterator(); it.hasNext(); ) {
int fontSize = 12;
String currFont = (String)it.next();

try {
fontSize = Integer.parseInt(tf.getText());
} catch (NumberFormatException nfe) {
// Not really worried - carry on!
}

StyleConstants.setFontSize(def, fontSize);
StyleConstants.setFontFamily(def, currFont);
doc.insertString(doc.getLength(), currFont + "\n",
def);
jtp.setCaretPosition(0);
}
} catch (BadLocationException ble) {
System.out.println("bad location exception");

return;
}
statusBar.setText("Fonts processed!");
}
});
topPanel.add(label);
topPanel.add(tf);
topPanel.add(jb);
topPanel.add(foreButton);
topPanel.add(backButton);
cont.add(topPanel, "North");
cont.add(jsp, "Center");
cont.add(statusBar,"South");

frame.setBounds(20, 20, 530, 700);
frame.setVisible(true);
}
}


"Christian Kaufhold" <use...@chka.de> wrote in message

news:0t3c2f29b...@aves.chka.de...

Christian Kaufhold

unread,
Jan 5, 2002, 5:51:19 PM1/5/02
to
Hello!

wrightkevin <wrigh...@ntlworld.com> wrote:

> So there I was (today) developing a "Print all known fonts" application.
> Guess what I thought might come in handy? Answer - The ability to
> designate a single button as the 'Primary button' (without regard to
> the gui's current focus) sound familiar?

If you don't want the other buttons to become "default", call
setDefaultCapable(false);

> Am including my source code to try to convince you that the method
> might have a use after all.

Which method?

Actually, default button is designed (and implemented) so weakly that
it is much less useful and customizable than it could have been.

Something like what you seem to want to achieve with your code could
actually have been done more explicitly than changing all buttons that
should have *nothing* to do with it.

See http://www.chka.de/swing/laf/default-button.html

[snip code]


Christian
--
Shedding off one more layer of skin
Keeping one step ahead of the persecutor within
Bob Dylan, Jokerman

0 new messages