JButton is created "selected" even when I declare it not to be so
/**
* Generate value and conditions for {@link #backButton}
*/
privatevoid generateBackButton(){
SimpleBrowser.this.backButton =new JButton("<--");
SimpleBrowser.this.backButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent evt){
SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
SimpleBrowser.hasClickedBack =true;
SimpleBrowser.this.currentHistoryIndex--;
SimpleBrowser.this.setURL(
(URL)SimpleBrowser.this.actualHistoryURLVector.get(
SimpleBrowser.this.currentHistoryIndex)
);
SimpleBrowser.this.setURLPath(SimpleBrowser.this.getURL().toString());
SimpleBrowser.this.processor.processURL(SimpleBrowser.this.getURL());
}
});
}
});
SimpleBrowser.this.backButton.setSelected(false);
SimpleBrowser.this.backButton.setFont(SimpleBrowserGlobals.FONT);
}
The moment this particular JButton is created it is created as "selected", even though I have done "setSelected(false)". It does not fire unless I click onto it, of course, but it appears as if I did and that's not good. Is there a way I can fix this?
Thanx
Phil
# 1
Button can't be selected if it's not toggle button (JCheckBox, for example) but button can be focused. It means that it has a focus and if you press 'space' button your 'back button' will be pressed.Read more about working with KeyboardFocusManager.
# 2
> Button can't be selected if it's not toggle button
> (JCheckBox, for example) but button can be focused.
> It means that it has a focus and if you press 'space'
> button your 'back button' will be pressed.
>
> Read more about working with
> KeyboardFocusManager.
Does nothing, tried this:
SimpleBrowser.this.backButton.setSelected(false);
SimpleBrowser.this.backButton.setFocusPainted(false);
SimpleBrowser.this.backButton.setFocusTraversalKeysEnabled(false);
To no avail, the JButton still appears as if I clicked onto it when I have not yet done so.
# 3
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 4
> If you need further help then you need to create a
> [url
> http://homepage1.nifty.com/algafield/sscce.html]Short,
> Self Contained, Compilable and Executable, Example
> Program[/url] (SSCCE) that demonstrates the incorrect
> behaviour, because I can't guess exactly what you are
> doing based on the information provided.
>
> Don't forget to use the [url
> http://forum.java.sun.com/help.jspa?sec=formatting]Cod
> e Formatting Tags[/url] so the posted code retains
> its original formatting.
This is the absolute simplest class I could create that does the same thing: creates a JButton in a JPanel in a JFrame but the JButton appears already selected:
/*
* SimpleFrame.java
*
* Created on March 16, 2007, 12:27 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ppowell.tools.ObjectTools;
import java.awt.*;
import javax.swing.*;
/**
*
* @author Phil Powell
*/
public class SimpleFrame extends JFrame {
JButton backButton;
JPanel topPanel;
/** Creates a new instance of SimpleFrame */
public SimpleFrame() {
initComponents();
}
// ADD EVERYTHING TO THE FRAME
public void addToFrame() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
add(topPanel, c);
}
/**
* Generate value and conditions for {@link #backButton}
*/
private void generateBackButton() {
backButton = new JButton("<--");
backButton.setSelected(false);
backButton.setFocusPainted(false);
backButton.setFocusTraversalKeysEnabled(false);
backButton.setFont(new Font("Arial", Font.PLAIN, 12));
}
// SET EVERYTHING UP
public void initComponents() {
topPanel = new JPanel(true);
generateBackButton(); // GENERATE THE JBUTTON
layoutTopPanel();// LAYOUT THE TOP PANEL BY PLACING JBUTTON INTO IT
addToFrame(); // ADD PANEL TO FRAME
showFrame(); // SHOW FRAME
}
// LAYOUT THE TOP PANEL
public void layoutTopPanel() {
topPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
topPanel.add(backButton, c);
}
// SHOW THE FRAME
public void showFrame() {
// CODE BORROWED FROM http://today.java.net/pub/a/today/2003/12/08/swing.html WITH MODIFICATIONS
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e) {
System.out.println("Unable to load native look and feel");
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (InstantiationException e3) {
e3.printStackTrace();
} catch (IllegalAccessException e4) {
e4.printStackTrace();
}
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SimpleFrame simp = new SimpleFrame();
}
}
# 5
I don't see the problem with your code. The button does not appear selected to me. I'm using JDK1.4.2 on XP.
Maybe the following will do what you want:
backButton.setFocusable( false );
// backButton.setSelected(false);
// backButton.setFocusPainted(false);
// backButton.setFocusTraversalKeysEnabled(false);
# 6
> I don't see the problem with your code. The button
> does not appear selected to me. I'm using JDK1.4.2 on
> XP.
>
> Maybe the following will do what you want:
> backButton.setFocusable( false );
> // backButton.setSelected(false);
> // backButton.setFocusPainted(false);
> //
> backButton.setFocusTraversalKeysEnabled(false);
I'm using JDK 1.6.0 on XP, maybe that's the problem: newer JDK = worse Swing
Nonetheless I tried your code suggestion; the JButton still appears as if you clicked it, however, if you minimize the JFrame and re-maximize it the JButton appears as if you did NOT click it. So the focus changes only if you physically minimize the entire Swing application
# 7
> > I don't see the problem with your code. The button
> > does not appear selected to me. I'm using JDK1.4.2
> on
> > XP.
> >
> > Maybe the following will do what you want:
> > backButton.setFocusable( false );
> > // backButton.setSelected(false);
> > // backButton.setFocusPainted(false);
> > //
> >
> backButton.setFocusTraversalKeysEnabled(false);
>
>
> I'm using JDK 1.6.0 on XP, maybe that's the problem:
> newer JDK = worse Swing
>
> Nonetheless I tried your code suggestion; the JButton
> still appears as if you clicked it, however, if you
> minimize the JFrame and re-maximize it the JButton
> appears as if you did NOT click it. So the focus
> changes only if you physically minimize the entire
> Swing application
Got it when I simply did this:
SimpleBrowser.this.backButton.setFocusable(false);
Way too simple.