JToolBar/JButton text/icon on and off

Is there an easy way to turn on/off all text/icon display in all the buttons of a JToolBar in one shot? You know, I want to let the user choose to view the toolbar in 3 ways:
1. icon only
2. text only
3. icon plus text
I know I could iterate over each JButton and do
jbutton.setText(null) or jbutton.setIcon(null), but this way I will have to store the original text/icon somewhere so I can easily restore them later on. This is cumbersome.

Hi,
try something like this :
regards
[email protected]
//demonstrates use of the Action object
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class xtendMenu extends JxFrame {
JPanel jp;
JMenuItem menuitem;
ActionToolBar toolbar;
public xtendMenu() {
super("Extended Menu");
JMenuBar mbar = new JMenuBar(); //set up menu bar
setJMenuBar(mbar);
//Add File menu
JMenu mFile = new JMenu("File");
mbar.add(mFile);
//create two Action Objects
Action Open = new FileButton("Open", new ImageIcon("open.gif"), this);
menuitem = mFile.add(Open);
menuitem.setIcon(null);
Action Exit = new ExitButton("Exit", new ImageIcon("exit.gif"));
mFile.addSeparator();
menuitem = mFile.add(Exit);
menuitem.setIcon(null);
//Exit.setEnabled(false); --used to test disable
//now create toolbar that fixes up the buttons as you add them
toolbar = new ActionToolBar();
getContentPane().add(jp = new JPanel());
jp.setLayout(new BorderLayout());
jp.add("North", toolbar);
//add the two action objects
toolbar.add(Open, "Open a file");
toolbar.addSeparator();
toolbar.addSeparator();
toolbar.addSeparator();
toolbar.add(Exit, "Exit from program");
setSize(300, 200);
setVisible(true);
static public void main(String argv[]) {
new xtendMenu();
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public abstract class ActionButton extends AbstractAction {
Hashtable properties;
public ActionButton(String caption, Icon img) {
properties = new Hashtable();
properties.put(DEFAULT, caption);
properties.put(NAME, caption);
properties.put(SHORT_DESCRIPTION, caption);
properties.put(SMALL_ICON, img);
public void putValue(String key, Object value) {
properties.put(key, value);
public Object getValue(String key) {
return properties.get(key);
public abstract void actionPerformed(ActionEvent e);
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class ExitButton extends ActionButton {
JFrame fr;
public ExitButton(String caption, Icon img) {
super(caption, img);
public void actionPerformed(ActionEvent e) {
System.exit(0);
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class ActionToolBar extends JToolBar
public ActionToolBar()
setFloatable(false);
public void add(Action act, String tip)
JButton button = super.add(act);
button.setText("");
button.setToolTipText(tip);
button.setMargin(new Insets(0,0,0,0));
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class FileButton extends ActionButton
JFrame fr;
public FileButton(String caption, Icon img, JFrame frm)
super(caption, img);
fr = frm;
public void actionPerformed(ActionEvent e)
FileDialog fdlg = new FileDialog(fr, "Open File", FileDialog.LOAD);
//JFileChooser fdlg = new JFileChooser();
//fdlg.showOpenDialog(fr);

Similar Messages

  • TS3218 My iPod Nano 6th generation fell one time and it kept working with a small crack in the screen.But after a while it just gave out and does not turn on.I try to charge it and all it does is blink the apple icon on and off.When disconnected nothing h

    My iPod Nano 6th generation fell one time and it kept working with a small crack in the screen.But after a while it just gave out and does not turn on.I try to charge it and all it does is blink the apple icon on and off.When disconnected nothing happens.

    I am having the identical trouble as described above with my month old Macbook Air. I tried everything with the ditto results. Please help anyone!
    Mine is a Macbook Air 13', 128GB, 1.86GHz with 4 GB RAM.

  • Mapping between Icon component and Bar chart component

    Hi experts,
    I have added one world map, on which i have added 3 icon component in different area of world. At present i have added 3 different bar chart for each icon component. Now i want to take only one bar chart which sohws data according my selection of icon component.
    can anyone help on this, plz?
    Thanks in Advance,
    Rishit

    Hi
    Try the following:
    Icon1 Source data = Unchecked 0, Checked 1
    Icon1 Destination Cell $D$2
    Icon2 Source data = Unchecked 0, Checked 2
    Icon2 Destination Cell $D$3
    Icon3 Source data = Unchecked 0, Checked 3
    Icon3 Destination Cell $D$4
    Have all the icons set to be unselected.
    In cell $D$5 have the formula =max($D$2:$D$4).  This cell now controls the Dynamic Visibility of the Bar Charts.
    Barchart1, Dynamic Visibility Status = $D$5, Key = 1
    Barchart2, Dynamic Visibility Status = $D$5, Key = 2
    Barchart3, Dynamic Visibility Status = $D$5, Key = 3
    I think that's what you are asking! Please keep in mind that you have to click the icons on and off to show/hide the charts.
    Regards
    Charles

  • JButton help, icon +text position

    Hi, is it possible to have JButton with icon on far left and text between the icon and far right hand side?

    Yo can do it by using the setHorizontalTextPosition() method.
    here is one example
    import java.awt.*;//Frame
    import javax.swing.*;
    public class Test extends JFrame
         public Test()//Constructer. Creates the frame
              JButton bt = new JButton("Done", new ImageIcon("image.gif"));
              bt.setHorizontalTextPosition(AbstractButton.LEFT);
              setBounds(300,300,300,300);
              getContentPane().setLayout(new FlowLayout());
              getContentPane().add(bt);
              setVisible(true);//shows on the screen
         public static void main(String[] args)
              new Test();//Creates a new Instance of our class
    }

  • JButton text position without an icon

    Hi,
    I'm trying to set the text on my JButtons to be in the top left corner. I've read many of the topics on JButton text position, but they all assume you have an icon with your text. I want to be able to do it without icons (as my buttons don't need them). I tried using the setHorizontalTextPosition and setVerticalTextPosition methods, but the text is still in the same spot.
    Does anyone have any ideas?
    Message was edited by:
    raardvark

    Hi,
    I'm trying to set the text on my JButtons to be in
    the top left corner. I've read many of the topics on
    JButton text position, but they all assume you have
    an icon with your text. I want to be able to do it
    without icons (as my buttons don't need them). I
    tried using the setHorizontalTextPosition and
    setVerticalTextPosition methods, but the text is
    still in the same spot.
    Does anyone have any ideas?
    Message was edited by:
    raardvarkPut a new border on the button that has some space on the bottom and right. Or add insets with the new border.

  • My phone constantly turns it self on and off, it won't let my messages load I carnt text or receive and texts or calls, I have updated the new iso7, can anyone help?

    My phone constantly turns it self on and off, it won't let my messages load I carnt text or receive and texts or calls, I have updated the new iso7, can anyone help?

    Hello Bricky2013,
    The following article has some useful tips that can help stabilize your iPhone.
    Will not turn on, will not turn on unless connected to power, or unexpected power off
    Verify that the Sleep/Wake button functions. If it does not function, inspect it for signs of damage. If the button is damaged or is not functioning when pressed, seek service.
    Check if a Liquid Contact Indicator (LCI) is activated or there are signs of corrosion. Learn about LCIsand corrosion.
    Connect the iPhone to the iPhone's USB power adapter and let it charge for at least ten minutes.
    After at least 30 minutes, if:
    The home screen appears: The iPhone should be working. Update to the latest version of iOS if necessary. Continue charging it until it is completely charged and you see this battery icon in the upper-right corner of the screen . Then unplug the phone from power. If it immediately turns off, seek service.
    The low-battery image appears, even after the phone has charged for at least 20 minutes: See "iPhone displays the low-battery image and is unresponsive" symptom in this article.
    Something other than the Home screen or Low Battery image appears, continue with this article for further troubleshooting steps.
    If the iPhone did not turn on, reset it while connected to the iPhone USB power adapter.
    If the display turns on, go to step 4.
    If the display remains black, go to next step.
    Connect the iPhone to a computer and open iTunes. If iTunes recognizes the iPhone and indicates that it is in recovery mode, attempt to restore the iPhone. If the iPhone doesn't appear in iTunes or if you have difficulties in restoring the iPhone, see this article for further assistance.
    If restoring the iPhone resolved the issue, go to step 4. If restoring the iPhone did not solve the issue, seek service.
    iPhone: Hardware troubleshooting
    http://support.apple.com/kb/TS2802
    Cheers,
    Allen

  • HT3529 My friend switched his phone number from an iPhone to a Blackberry. My phone still wants to use iMessage to send him a text and won't let me send it as a text unless I turn off iMessage on my phone. Is there a fix for this?

    My friend switched his phone number from an iPhone to a Blackberry. My phone still wants to use iMessage to send him a text and won't let me send it as a text unless I turn off iMessage on my phone. Is there a fix for this?

    jengrz wrote:
    My friend switched his phone number from an iPhone to a Blackberry. My phone still wants to use iMessage to send him a text and won't let me send it as a text unless I turn off iMessage on my phone. Is there a fix for this?
    TechCrunch posted a solution today that applies to this problem, I think.
    Go to: https://supportprofile.apple.com/MySupportProfile.do
    Click your old iPhone and "unregister" it.
    More info here:
    http://m.techcrunch.com/2012/01/05/i...d=tc_home_art&

  • HT201401 last night my iphone screen all of a sudden was solid blue and now it is black. I put it on the charger and this morning it is totally black.  However, I did hear the text message signal go off. When I turned the volume on and off a big white bel

    Last night my iphone screen all of a sudden was solid blue and now it is black. I tried turning it on and off.  Still nothing appeared on the screen.  I put it on the charger and this morning it is totally black.  However, when I hit the on and off button, I did hear the text message signal go off. When I turned the volume on and off a big white bell flashed.  In the last week, there were some other display problems such as the screen blowing up large. The only way to fix it was to turn off the phone and then turn it back on. 
    What does all of this mean? It seems like I've had these problems since the iphone 4 upgrade.

    just do reset, press home button and power butoon for a while untill you can see apple
    gud luck

  • Is there a way to increase the icon size (and the text below) without using zoom?  I am trying to help out someone who has moderate visual impairment that does not want to use zoom just to see the icons and their labels.

    Is there a way to increase the icon size (and the text below) without using zoom in iOS 7?  I am trying to help out someone who has moderate visual impairment that does not want to use zoom just to see the icons and their labels.

    Hello Apple.
    It seems you have gone to great lengths to improve accessibility in many areas of the iPad. Why was this obvious problem with icon text size missed?  (It's not with the Mac.). And for so long too.
    Do you employ people with actual accessibility problems to help you do UI design?
    I think too, that some buttons are too close for people who might have motor control problems.
    I love my iPad but I fear as I age, the iPad might not keep up with me.

  • TS3274 my ipad won"t turn off. there is a small window with an email text in it and it appears frozen on the screen. How can i turn the ipad off?

    my ipad won"t turn off. there is a small window with an email text in it and it appears frozen on the screen. How can i turn the ipad off?

    Try a reset: Simultaneously hold down the Home and On buttons until the device shuts down. Ignore the off slider if it appears. Once shut down is complete, if it doesn't restart on it own, turn the device back on using the On button. In some cases it also helps to double click the Home button and close all apps BEFORE doing the reset.

  • My iphone 5 does not turn on anymore.  when i plug the charger in the apple icon turns on and off but the home screen never shows up.what do i do to fix it? how can i get my info back?

    My iphone 5 does not turn on anymore.  when i plug the charger in the apple icon turns on and off but the home screen never shows up.what do i do to fix it? how can i get my info back?

    from the information you provided i can't tell if it is a hardware problem or a software problem. I do have an idea though, try to press your home button and lock button at the same time.

  • My system preferences have suddenly disappeared! I have the language and text icon but that is the only icon that is displayed! where have my system preferences gone and how do i get them back??

    I went to my system preferences today and they are all gone except for my language and text icon! I have the language and text icon but that is the only icon that is displayed! where have my system preferences gone and how do i get them back??

    Double click on the picture of the HD on your desktop, then look at the bottom of the window that opens.
    First check the S.M.A.R.T. status on your HD: Applications > Utilities > Disk Utility > in the panel at left, select the first item in the list/your HD mechanism > look atthe bottom of the main window next to S.M.A.R.T. status and see if it says “Verified” or something more ominous like “Failing.”
    If S.M.A.R.T.status is "Verified," run Repair Disk: Boot from install disc (insert disc > restart > immediately hold down c key and keep holding it until you see “Preparing Installation”) > at first screen select the language and click Continue > click on the Utilities Menu in the menu bar > open Disk Utility > select your HD in the panel on the left side > click Repair Disk at bottom of main window. Run this at least twice, and keep running it until it says “appears ok” twice in a row. If that doesn’t happen, you may need a stronger utility such as DiskWarrior or if the directory is damaged beyond repair, you may need to reinstall the OS, or you may have a damaged HD (repair utilities can only repair the directory structure, not the HD itself). When this is finished, quit Disk Utility, quit the installer, and restart. Once booted normally, go to Applications > Utilities > Disk Utility and run Repair Permissions.

  • Mac Mini desktop folders/icons and top menu blinking on and off

    Hello. I've just returned to my Mac Mini after being away for a few days. Upon booting and having the desktop fully load, I have been experiencing all folders/icons and top menu bar blinking on and off every ten seconds. I have only opened Fire Fox to try and diagnose why this is happening. No solution yet.
    I am running Snowleopard with Mavericks and I'm aware Mavericks is a little buggy at the moment (beach ball used to blink on and off every now and again and performance has gotten slow) I only have the HDD replaced last week as the last one failed. I really need to fix this problem soon as I rely on my Mac to work from home.
    Any help would be greatly appreciated
    Jan

    I have sovled the problem! for now at least. I came across another users suggestion to dissable "sync status icons" in google drive as it confics with the finder in Mavericks. I guess this would only work with others having this problem with Google Drive and Mavericks.

  • HT201263 After updating to the ios6 my ipod was going on and off displaying the apple icon and then had a white screen that had multi colors on it. Then it had a blue screen.plz help me

    I was updating my iPod touch 4g to the ios6 and it told me to hook it up to the iTunes and so I did. After doing that the computer told me to restore I tried and it didn't work. My ipod then was Turing on and off displaying the apple icon then my screen turned white with rainbow colors going down the screen then at some point my ipod had a multi colored blue screen. The screen was no longer blue after I let it die. Plz help I can't find any solutions.

    Try:
    - iOS: Not responding or does not turn on
    - If not successful and you can't fully turn the iPod fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - If still not successful that indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.

  • Apple icon clicking on and off?!?!?!?

    My nano froze and I was kindly pushed in the direction of restoring my nano on disk format...whcih I did and everything started working okay-ish! Then after going back to normal format the apple icon appeared and for a short while I had the normal screen, but the apple came back and has been clicking on and off ever since (I say clicking not flashing because I can actually hear the click when the icon goes off!!!)
    I am really stuck now...the battery is charged, I have reset...reset....reset....restored on disk format....and still it isn't working!!!!!
    Please someone help me!!!

    jack...sounds lik the click wheel is almost in a permanently pressed state and it's making the Nano re-set? If you haven't used your one free call to ttech support, I'd use it now....if there's a defect, Apple will fix it.

Maybe you are looking for