another JToggleButton help
hi, i have this code in JToggleButton
final JToggleButton tb = new JToggleButton("TB");
tb.setUI(new javax.swing.plaf.basic.BasicButtonUI(){
protected void paintButtonPressed(Graphics g,AbstractButton b){}});
this button when clicked, doesn't change the UI, it is always clicked.
Now, i wanted to know what is the code for, it is always unclicked even if its clicked logically or not... Thanks everyone
> Now, i wanted to know what is the code for, it is always unclicked even if its clicked logically or not.
the code sets the JToggleButton UI to an instance of BasicButtonUI, where
paintButtonPressed() is overridden - to do nothing.
i.e. when the button is clicked, nothing happens (normally the 'clicked' button is painted)
(reply to email)
change
tb.setUI(new javax.swing.plaf.basic.BasicButtonUI(){
to
tb.setUI(new javax.swing.plaf.metal.MetalButtonUI(){
(note also the plaf.basic to plaf.metal)
I should have used the MetalButtonUI in the original code
> i.e. when the button is clicked, nothing happens (normally the 'clicked' button is painted)
hi, thank you very much for the reply, but the metalButton UI kinda don't fit the look and feel. about the normally the 'clicked' button is painted, how can I paint the 'unclicked' button, i searched the API high and low, still I can't find it... Thx so much
> how can I paint the 'unclicked' button,
the simple way is to include, in your toggleButton's actionPerformed,
toggleButton.setSelected(false);
but this then becomes just a standard button, and you will lose the option
of checking the button's state for true or false, since it will always return to false.
might be a good time for you to post a sample working program, with your L&F,
and tell us what you would like it to do
it can still be selected and unselected but the paint of the button is not selected always... thx
might be simpler to just use a JButton and associate a boolean flag with itboolean buttonSelected;and in actionPerformed()buttonSelected = !buttonSelected;will toggle the state with each click
im very sorry if im annoying, thx for the reply... I can't have a boolean because its not simpler in the app, because I have 100 to 200 jtoggleButton, so its complicated to have a flag...
see if you can ID the UI for the toggleButton.
add this
System.out.println(tb.getUI().getClass().getName());
you then might be able to do something similar to earlier posts, but with
100-200 buttons you'd be looking at doing it via the UIManager, and this
presents another potential problem of affecting other running programs
im very sorry, It can't Id the UI, it only prints where the togle button is created. So there is no opposite way like the paintButtonPressed? like paintButtonUnpressed maybe? Is there a way? Thx so much!
post a sample program, with your L&F - just a frame with a toggleButton
just the same code u gave last time, but instead its pressed, its not
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
final JToggleButton tb1 = new JToggleButton("TB1");
final JToggleButton tb2 = new JToggleButton("TB2");
tb1.setUI(new javax.swing.plaf.basic.BasicButtonUI(){
protected void paintButtonPressed(Graphics g,AbstractButton b){}});
tb2.setUI(new javax.swing.plaf.metal.MetalButtonUI(){
protected void paintButtonPressed(Graphics g,AbstractButton b){}});
System.out.println(tb1.getUI().getClass().getName());
System.out.println(tb2.getUI().getClass().getName());
JFrame f = new JFrame();
f.getContentPane().add(tb1,BorderLayout.NORTH);
f.getContentPane().add(tb2,BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
tb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("tb1 "+(tb1.isSelected()? "":"un")+"selected");
}
});
tb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("tb2 "+(tb2.isSelected()? "":"un")+"selected");
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
thx...
everything posted works, but you've stated it doesn't match your look and feel.
so, this was the part needed - "with your L&F", in the sample program.
looking at the source code for the UI and model, it revolves around isPressed,
isArmed, isSelected and a couple of others, and basically, whatever is changed
so that the buttons appear 'unpressed', also changes the isSelected value.
also looks like manipulating the pressedIcon doesn't work, so you possibly may
have to write your own toggleButton to do exactly what you want.