Questions tagged [jbutton]

A JButton is a common button in Java Swing.

A JButton is a common button in Java Swing that allows users to execute actions and events which have been tied to that button. It is designed to be used with ActionListener and Action classes for interacting with a gui.

Relevant Java Tutorials:

4918 questions
7
votes
2 answers

Resizing JButtons and other components according to text

How do you resize a JButton at runtime so it adapts to the text given by setSize? I've done some searching and this is the code I've come up with so far. Could this be turned into a utility method? FontMetrics metrics = getFontMetrics( font ); int…
James P.
  • 19,313
  • 27
  • 97
  • 155
7
votes
1 answer

Removing space around buttons in GridBagLayout

I have this vexing source written to demonstrate a layout for a game screen mentioned on another question. It puts buttons (or labels, choosable at start-up) into a GridBagLayout. If you choose to not use buttons when prompted (before the GUI…
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
7
votes
5 answers

Is it possible to put text on top of a image in a button?

I have .jpg images in my buttons. I also would like to put some text on top of the images. I use the following syntax for that: JButton btn = new JButton(label,icon); But I do not see text in the buttons (only image). What am I doing wrong?
Roman
  • 124,451
  • 167
  • 349
  • 456
7
votes
4 answers

Swing Jbutton: showing border and background only when it is hovered

I want to add a hovering-effect to my customized Swing.JButton similar to the icon on my Chrome Browser: Before hover >> After hover >> I am able to set the button in the "before" status when it is created, but I am not able to create the "border…
Randomize
  • 8,651
  • 18
  • 78
  • 133
7
votes
2 answers

Renaming a button after clicking - Java JButton

I'm trying to change my button from saying "Start" to "Stop" when I click on it. My attempt at doing this is below, I looked it up and tried to copy the guides but I don't see what I'm doing wrong. I might be missing some "}" because I left out a…
user1692517
  • 1,122
  • 4
  • 14
  • 28
7
votes
2 answers

Adding Jbutton to JTable

Need a simple Swing code to demonstrate how to add a button in a column of a Jtable using tablecellrenderer and tablecelleditor.
deva
  • 141
  • 1
  • 1
  • 9
7
votes
3 answers

Pass variables to ActionListener in Java

I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { setPage(i); …
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
6
votes
3 answers

How to create a custom JButton in java with an image base?

I recently read this thread (Creating a custom button in Java) on creating custom buttons in java by extending the JButton class, however all the solutions on this thread use graphics drawn in java. I wanted to have my button based on a button image…
user1060899
  • 91
  • 1
  • 2
  • 5
6
votes
2 answers

How to actually select a JButton

This will be a really theoretical question, just bear with me a bit. I need to do something with my JButtons, and I have no idea where to start. So what I need is to be able to click on a JButton, and have a visual proof that it is selected, for…
Cristol.GdM
  • 505
  • 2
  • 5
  • 21
6
votes
3 answers

Make 2 JButtons Equal in size

I have two JButtons with texts "Ok" and "Cancel". I am using GridBagLayout to align them in a JDialog. I have set the anchor to GridBagConstraints.CENTER. Due to the difference in the number of characters in the texts "Ok" and "Cancel", the buttons…
Kaushik Balasubramanain
  • 1,248
  • 6
  • 28
  • 42
6
votes
2 answers

How to create partly transparent JButton on fully transparent JFrame?

I am able to make JFrame totally transparent and the JButton is partly transparent just fine until I move my mouse on the button ( do not click ) and move the mouse off from the button ( MouseExited called via MouseListener ). What happens is that…
Pete
  • 63
  • 1
  • 2
  • 4
6
votes
3 answers

How do I control the centering of JButton HTML text in NetBeans?

I'm trying to put a two-line piece of text on a JButton; e.g. +----------+ | READER | | STOP | +----------+ But I'm having trouble getting it centered on the button. I go to the Property editor for the JButton, and enter…
Chap
  • 3,649
  • 2
  • 46
  • 84
6
votes
5 answers

How to set the button color of a JButton (not background color)

I have a JButton that I would like to change the background color of to white. When using the Metal Look And Feel, I achieve the desired effect with setBackground: Unfortunately, the concept of "background color" is different when using the…
Joseph Trebbien
  • 309
  • 1
  • 3
  • 8
6
votes
3 answers

How do I Click a JButton without the user Clicking it?

I have a JButton and when a player clicks it it tell my Action Listener that a button is clicked. What i want to know is that is there a command or something that acts as if a player clicked the button. Like Tic Tac Toe, i have it so 2 players can…
Joe C
  • 1,788
  • 2
  • 17
  • 27
6
votes
4 answers

Swing/Java: How to use the getText and setText string properly

I'm trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked. Right now it says: 'txt' and I understand why. But I don't know how I can use the string! Can anyone explain me what I'm doing wrong and how…
Opoe
  • 1,337
  • 8
  • 30
  • 56