Solved

making toolbar radio buttons, but with ordinary button look and feel

Posted on 2003-11-07
10
311 Views
Last Modified: 2010-05-18
Hello,

Does anyone have any suggestions for making a JToolBar's buttons behave like grouped radio buttons, but have them look like regular buttons using AbstractAction objects? What I'm trying to do is have a button continue to look depressed after it is clicked. Then if they click another button on the toolbar, it then stays depressed and the former button becomes unpressed.

Thanks.
0
Comment
Question by:TheDownsizer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9701913
Would switching the icon using setIcon() and setPressedIcon() be adequate?

I don't think that the 3d effect of the button would look pressed, but it's a thought ;-)
0
 

Author Comment

by:TheDownsizer
ID: 9701984
I want to stay away from having to create icons for the buttons, if possible.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9702030
Thought so :-(

If you want the "pressed" effect to stay, I don't think there's an easy way to do that (certainly nothing as easy as the icons).  I'm not exactly sure how you could do it.  I suspect that you'd need to start digging around in the AbstractButton class, probably extending it for your own purposes and overriding (if possible) the way it displays itself.

Just hang around for a while, one of the other experts is sure to be able to provide some more detail.

At this time of day, objects should be the next heavyweight on the scene ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TheDownsizer
ID: 9702435
I tried wrapping an Action with a JRadioButton and then adding the button to the tool bar, like so:

      JRadioButton j = new JRadioButton(new ModeAction( "mode A", new ImageIcon( "images/modeA.gif" )));
      toolBar.add( j );

It uses an icon, but unfortunately the button on the toolbar looks like a radio button with the label "mode A" to the right of it instead of just showing the icon "modeA.gif".

Any thoughts on how to get just the icon to show instead? I'm thinking maybe once the radio button displays only the icon, the button will probably stay looking pressed after clicking it since it is a radio button.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9702673
0
 

Author Comment

by:TheDownsizer
ID: 9702985
I'm probably not clear enough in my posts, but grouping the button's isn't my problem.

What I'd like to do is get rid of the radio button look by using a button icon. (In a prior post I wrote "I want to stay away from having to create icons for the buttons", but I meant to say I don't want to create *additional* icons for the pressed look and swap different icons in and out, unless it was necessary.) Once the button is clicked, I want the appearance to stay as the shaded, pressed look.

I am using an Action object for a JRadioButton, but the radio button does not display the icon that is provided from the Action object. (I chose an Action object because I may add it to a menu and it makes it simple to do.) It displays the radio button and text. Any ideas how to get the JRadioButton to display the image rather than the text and radio-button from the Action? Thanks.



0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9703105
The buttons are mutually exclusive and can have a pressed look. See the following:

import java.awt.Container;
import javax.swing.*;

public class F extends JFrame {

 public F() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container c = getContentPane();
       JPanel buttonPanel = new JPanel();
       ButtonGroup bg = new ButtonGroup();
       JToggleButton one = new JToggleButton("One");
       bg.add(one);
       JToggleButton two = new JToggleButton("Two");
       bg.add(two);
       JToggleButton three = new JToggleButton("Three");
       bg.add(three);
       buttonPanel.add(one);
       buttonPanel.add(two);
       buttonPanel.add(three);
       c.add(buttonPanel);
       setSize(250, 250);
 }

 public static void main(String[] args) {
       new F().setVisible(true);
 }
}
0
 

Author Comment

by:TheDownsizer
ID: 9703906
Hey CEHJ -

I appreciate your help, but I guess I am just not clear enough about what I want to do. The sample you posted is close to the result I'm looking for, but I purposely wrote the ModeAction class (derived from AbstractAction) to use for the toolbar buttons so it can be reused for corresponding menu items in the future. When I create a JRadioButton from the ModeAction, the resulting component uses the default radio button l&f, rather than showing the icon. Since it's the default l&f, I can't get the pressed button look to take effect.
0
 

Author Comment

by:TheDownsizer
ID: 9704291
I ended up creating toggle buttons created from ModeActions and setting it's text to empty strings. Perhaps not the greatest solution but it does the job:

      ModeAction ma = new ModeAction( "A", new ImageIcon( "A.gif" ));
      JToggleButton button = new JToggleButton( ma );
      button.setText( "" );

That leaves me with a different problem of synchronizing the toolbar with corresponding menu items, which I will open a new question for some other time.

Thanks for the guidance.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9706674
OK - sorry i couldn't be more accurate ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 73
jboss wildfly 10.1 10 420
JAVA API design with micro service cloud in mind 1 77
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question