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

[441 byte] By [JavaStartera] at [2007-10-3 9:41:29]
# 1

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

Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

(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

Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 3

> 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

JavaStartera at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 4

> 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

Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 5
it can still be selected and unselected but the paint of the button is not selected always... thx
JavaStartera at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 6
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
Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 7
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...
JavaStartera at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 8

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

Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 9
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!
JavaStartera at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 10
post a sample program, with your L&F - just a frame with a toggleButton
Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 11

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

JavaStartera at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...
# 12

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.

Michael_Dunna at 2007-7-15 4:57:36 > top of Java-index,Desktop,Core GUI APIs...