Make text in JButton invisible

I made a button and did .setText() , because I need to compare the value of .setText() with something else.

I applied .setText() to JButton, but I do not want the text to be visible in my button. If I do setVisible(false) then it hides the whole button, but I want it to hide the text.

Is there an option for this? I considered creating a special font and applying it to the text in .setText() , but I am wondering if there is a more efficient option for my problem.

Thanks in advance guys.

EDIT: I cannot use .setText(" ") because I need to compare the value inside it.

+4
source share
6 answers

You declare:

EDIT: I cannot use .setText ("") because I need to compare the value inside it.

Nonsense. As I mentioned in the commentary, set the JButton text to " " and do not use the JButton text for comparison. Instead, use your actionCommand, which is easy to get through getActionCommand (). Or use HashMap<JButton, SomethingElse> .

You can consider changing the JButton action when you need to change its behavior and state, which is easy to do by calling setAction(...)

For instance,

 import java.awt.event.ActionEvent; import javax.swing.*; public class ButtonActions { private static void createAndShowGui() { JPanel mainPanel = new JPanel(); JButton myButton = new JButton(); StartAction startAction = new StartAction(); PauseAction pauseAction = new PauseAction(); BlankAction blankAction = new BlankAction(); startAction.setNextAction(pauseAction); pauseAction.setNextAction(blankAction); blankAction.setNextAction(startAction); myButton.setAction(startAction); mainPanel.add(myButton); JFrame frame = new JFrame("ButtonActions"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class SwappingAction extends AbstractAction { private Action nextAction; public SwappingAction(String text) { super(text); } public void setNextAction(Action nextAction) { this.nextAction = nextAction; } public Action getNextAction() { return nextAction; } @Override /** * super method needs to be called in child for swap to work */ public void actionPerformed(ActionEvent e) { System.out.println("ActionCommand: " + e.getActionCommand()); ((AbstractButton)e.getSource()).setAction(nextAction); } } class StartAction extends SwappingAction { public static final String START = "Start"; public StartAction() { super(START); } @Override public void actionPerformed(ActionEvent e) { super.actionPerformed(e); // start-specific code goes here } } class PauseAction extends SwappingAction { public static final String PAUSE = "Pause"; public PauseAction() { super(PAUSE); } @Override public void actionPerformed(ActionEvent e) { super.actionPerformed(e); // pause-specific code goes here } } class BlankAction extends SwappingAction { public static final String BLANK = " "; public BlankAction() { super(BLANK); } @Override public void actionPerformed(ActionEvent e) { super.actionPerformed(e); } } 
+6
source

Write buttonName.setText(" ") , this will not display a single button name. And whenever you want to display the name (on any event), set it again buttonName.setText("some text")

0
source

If you insist on not using setText("") , try setting the same color as the background color and text color. Check out the links below.

setBackground (java.awt.Color)

setForeground (java.awt.Color)

0
source

You can set the text invisible by changing the font color to have an alpha value of 0:

invisTextJButton.setForeground(new Color(255, 255, 255, 0));

0
source

Why don't you name the first button "" (1 space). second: "" (2 spaces) third: "" (3 spaces), etc.

Now compare: if ((event.getActionCommand ()). Equals ("")) {// 1st button}

if ((event.getActionCommand ()). equals ("")) {// Second button}

.. etc.

where the event is an ActionEvent object

Thus, the buttons will have unique names and be invisible. Horrible coding, I know. But this is a trick;)

-1
source

Instead of .setText (), use .setTag () and .getTag () to attach some value to the view - including the button - for later retrieval.

These methods exist directly for this purpose.

-2
source

All Articles