Seanimus
Seanimus

Reputation: 567

Swing Actions - Linking Menus and Toolbar

I've been studying Java Swing and I'm working on learning actions.
I can successfully create action objects and use them to link items from a JToolBar to JMenuItems. My problem, is that the constructed actions display both icons and text in the toolbar (should only be the icons).
Check out the following code:

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

class MenuDemo{  

   MenuDemo(){  
      JFrame jfrm = new JFrame("Complete Menu Demo");  
      jfrm.setSize(220, 200);  
      jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

      JMenuBar jmb = new JMenuBar();  

      /* Make the action object */  
      ImageIcon setIcon = new ImageIcon("setBP.png");  
      DebugAction setAct = new DebugAction("Set Breakpoint",   
                                         setIcon, KeyEvent.VK_S,   
                                         KeyEvent.VK_B, "Set Breakpoint");  

      /* Make the toolbar */  
      JButton jbtnSet    = new JButton(setAct);  
      JToolBar jtb = new JToolBar("Breakpoints");  
      jtb.add(jbtnSet);  

      /* Make the menu */  
      JMenu jmDebug = new JMenu("Debug");  
      JMenuItem jmiSetBP = new JMenuItem(setAct);  
      jmDebug.add(jmiSetBP);  

      jmb.add(jmDebug);  
      jfrm.getContentPane().add(jtb, BorderLayout.NORTH);  
      jfrm.setJMenuBar(jmb);  
      jfrm.setVisible(true);  
   }  

   class DebugAction extends AbstractAction{  
     public DebugAction(String name, Icon image, int mnem,  
                         int accel, String tTip){  
        super(name, image);  

        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(accel,  
                 InputEvent.CTRL_MASK));  
        putValue(MNEMONIC_KEY, new Integer(mnem));  
        putValue(SHORT_DESCRIPTION, tTip);  
     }  

     public void actionPerformed(ActionEvent ae){  
     }  
   }  

   public static void main(String[] args){  
      SwingUtilities.invokeLater(new Runnable(){  
         public void run(){  
           new MenuDemo();  
         }  
      });  
   }  
}  

This program yields the following GUI:

Screenshot of GUI with Text in JToolbar Icon Screenshot of GUI with Menu Showing

I just want the green button in the JToolBar, not text. I think my problem code is in the DebugAction constructor, where I call super(name, image). For the toolbar buttons, I would only want to pass in the image. But for the menu I want both. How can I "turn off" the text for the JToolBar items? Thanks!

Upvotes: 0

Views: 652

Answers (2)

Seanimus
Seanimus

Reputation: 567

I accepted camickr's answer, but there is a caveat.

For identification purposes, I am grabbing the actionCommand of these buttons. Both setHideActionText(true); and button.setText("") do remove the text, but both produce errors when I try to grab the actionCommand.
To fix this, I explicitly set the actionCommand when I create the action object by adding the additional line in the constructor:

putValue(ACTION_COMMAND_KEY, sometext);

This fixed the errors.

Upvotes: 0

camickr
camickr

Reputation: 324207

How can I "turn off" the text for the JToolBar items?

You turn it off using:

JButton button = new JButton( action );
button.setHideActionText( true );

Or, you can just reset the text on the button:

JButton button = new JButton( action );
button.setText("");

Upvotes: 4

Related Questions