4

I made a button and did a .setText() on it because I have to compare the value of the .setText() with something else.

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

Is there an option for this? I've considered making a custom font and apply it on the text in the .setText() but I'm wondering if there's a more efficient option to my problem.

Thanks in advance guys.

EDIT: I can't use .setText(" ") because I have to compare the value within it.

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
Glenndisimo
  • 251
  • 1
  • 7
  • 14
  • 1
    Use spaces as text if you want to hide it and leave the button visible. However this will most likely confuse the user of your application. Probably using text to identify a button is only a workaround. What do you want to achieve by hiding the text? Why do you need to compare the button's text? Have you considered other options? – harpun Mar 09 '13 at 22:19
  • 1
    Why not simply `setText(" ")`? Rather than compare text, I would compare the JButton's (or better, its Action's) actionCommand String. – Hovercraft Full Of Eels Mar 09 '13 at 22:19
  • You shouldn't compare it in that way. You'll have less problems if you change the logic of your program. See the answer of @Hovercraft. – Igor Rodriguez Mar 09 '13 at 22:28

5 Answers5

7

You state:

EDIT: I can't use .setText(" ") because I have to compare the value within it.

Nonsense. As I've mentioned in a comment, set the JButton's text to " ", and don't use the JButton's text for comparison. Instead use its actionCommand easily obtained via getActionCommand(). Or use a HashMap<JButton, SomethingElse>.

You may consider changing the JButton's Action when you need to change its behavior and state which is easily done by calling setAction(...)

For example,

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);
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Or he can set the name with setName(String). Obviously, comparing the "text" property is not a good way... – Igor Rodriguez Mar 09 '13 at 22:27
  • I haven't learned how to use those yet. I probably made a big mistake to use .settext for comparison but currently it's working and I don't have to the time to rewrite that part (also because I don't posses enough knowledge at this point). I think my only solution is to create a custom font? – Glenndisimo Mar 09 '13 at 22:35
  • @Glenndisimo: no, that solution is a kludge. Don't do it. Please look at edit for example. – Hovercraft Full Of Eels Mar 09 '13 at 22:41
  • Thanks a lot! Makes much more sense, going to rewrite my part. Thanks! – Glenndisimo Mar 09 '13 at 22:54
0

Write buttonName.setText(" ") this will not display any name to the button. And whenever you feel like displaying the name (on any event) then set it again buttonName.setText("some text")

asifsid88
  • 4,631
  • 20
  • 30
  • I'm aware that ("") doesn't display any text but I have to have the .settext to have a value for comparison reasons. But I just don't want that value to be visible for the end user – Glenndisimo Mar 09 '13 at 22:22
0

If you insist not to use setText(""), try setting same colour as a background colour and text colour. Check the below links

setBackground(java.awt.Color)

setForeground(java.awt.Color)

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
-1

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

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

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

..and so on

where event is an object of ActionEvent

This way the buttons will have a unique names and be invisible. Horrible coding, I know. But it does the trick ;)

aroon
  • 1
-2

Instead of .setText(), use .setTag() and .getTag() to attach some value to a View - including a Button - for later retrieval.

These methods are there directly for that kind of purpose.

SylvainL
  • 3,926
  • 3
  • 20
  • 24
  • 2
    This looks to be an Android answer and not a Swing answer. I think that you'll want to delete your answer as it does not appear to be relevant to the original poster's problem. – Hovercraft Full Of Eels Mar 09 '13 at 22:43
  • Sorry, I forgot to check for this Swing thing. However, while I don't know much about Swing, I would be surprised if there is no equivalent of setTag()/getTag() for the JButton; as all frameworks that I've seen to date - including the very old XMotif - have all a mecanism to attach a user defined piece of information to a view/widget/... whatever the name used in that particular framework. – SylvainL Mar 09 '13 at 23:05
  • 1
    @Sylvain unfortunately [JButton](http://docs.oracle.com/javase/6/docs/api/javax/swing/JButton.html) does not have a `setTag()`/`getTag()`. The closest thing offered in the Swing API is probably the [`setName()` / `getName()`](http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#method_detail) methods on `Component` – Jason Braucht Mar 10 '13 at 03:36
  • 1
    After a quick search, here they are: putClientProperty(Object, Object) and getClientPropety(Object) from the JComponent class. I have yet to find any framework who doesn't have the equivalent of getTag() and putTag() for their widgets. Also, another solution would be to simply subclass the JButton and add whatever field/method you need but personally, I prefer to use what's already there. – SylvainL Mar 10 '13 at 05:32