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
12
votes
6 answers

How do I assign Enter as the trigger key of all JButtons in my Java application?

I'm writing a Java Swing application using the Metal look-and-feel. Every time there is a JButton in my application the user uses the Tab key to move the focus to the button and then hits the Enter key. Nothing happens! If he hits the Space key the…
Alfred B. Thordarson
  • 4,460
  • 8
  • 39
  • 37
12
votes
8 answers

How to change the look of a disabled JButton in java

I am making a game and when I disable a button with setEnabled(false); the buttons turns grey which clashes with the other colors in the game. Is their a way to change the color of the button when it is disabled?
Patrick
  • 151
  • 2
  • 2
  • 4
11
votes
3 answers

Java - having buttons displaying arrows

I would like to have a buttons in Java which shows the arrows - like on the keyboard. So far I have this JButton arrowUp = new JButton("^"); JButton arrowDown = new JButton("v"); JButton arrowLeft = new JButton("<"); JButton arrowRight = new…
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
11
votes
3 answers

Is it possible to include JButton in a JTable?

I have a JTable that stores the results of a database query, so far so good. What I want is for the last column in each table to have a clickible JButton that will open the edit screen for the object represented in that row, and that means the…
Benjamin Confino
  • 2,344
  • 3
  • 26
  • 30
11
votes
5 answers

Removing the three dots "..." from a JButton?

Hey, I am creating a calculator program, with some small buttons, I want one of the buttons to have "Ans" on them, but whenever I make the JButton smaller then 50, 50, it will show three dots. "...", how can I remove these dots and show the normal…
Stan
  • 3,659
  • 14
  • 35
  • 42
11
votes
6 answers

Why does setBackground to JButton does not work?

I have the following simple code: btn = new JButton(); btn.setBackground(backgroundColor) I worked when I used: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); But it stopped to work after I have commented…
Roman
  • 124,451
  • 167
  • 349
  • 456
11
votes
2 answers

How to make Java JButton visually depress with touch screen?

I've got a simple Swing GUI with JButtons being run on a Surface tablet with a touchscreen. The buttons have ActionListeners. When these buttons are clicked from a mouse they visually depress correctly. However when they are tapped on the…
Stuart Lacy
  • 1,963
  • 2
  • 18
  • 30
11
votes
5 answers

How to change Yes/No option in confirmation dialog?

I want to change YES and NO to something like Agree/Disagree. What should I do? int reply = JOptionPane.showConfirmDialog(null, "Are you want to continue the process?", …
Kashama Shinn
  • 241
  • 2
  • 4
  • 11
11
votes
3 answers

Pros and cons of using ActionListener vs. MouseListener for capturing clicks on a JButton

I have a JButton, and would like to capture mouse clicks on it. What are the practical and philosophical differences between using an ActionListener vs. using a MouseListener on the JButton ?
Parag
  • 12,093
  • 16
  • 57
  • 75
11
votes
1 answer

New Line \n is not working in JButton.setText("fnord\nfoo") ;

On a JButton, I want to list information on multiple lines. I tried \n as a new line character but it didn't work. The following code: JButton.setText("fnord\nfoo") ; will be displayed as: fnordfoo How do I force a line break?
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
10
votes
4 answers

Change state of toggle button from another button

I'm creating a Java GUI using Swing with Eclipse and Window Builder Pro. I'm using JButtons and JToggleButtons. I want to change toggle button's state from another button. For example, when I click the clear grid, all the toggle buttons will be…
Ömer Faruk AK
  • 2,409
  • 5
  • 26
  • 47
10
votes
2 answers

"Un-rollover" a JButton when a JOptionPane is displayed

I have a situation where I need to display a JOptionPane after clicking on a JButton. The JButton has a default icon, and a rollover icon (which displays when, well, the mouse rolls-over the button). However, once the button is clicked and a…
aspiring_sarge
  • 2,355
  • 1
  • 25
  • 32
10
votes
2 answers

How can I make a button exactly the same size of its text?

Is it possible to make a JButton take exactly the size of its text? Since by default, a JButton will have a small amount of padding (both horizontal and vertical) around its text. I would like to remove that padding.
PB_MLT
  • 834
  • 3
  • 13
  • 30
10
votes
3 answers

JOptionPane with username and password input

I have my own Dialog pop up with two textfields, two JLabel and a "ok" JButton. The pop up is a login window. The window works perfect I just want to know how I am able to add a "cancel" JButton, so the user is able to cancel the login. Here is my…
Gerret
  • 2,948
  • 4
  • 18
  • 28
10
votes
5 answers

Null pointer exception when an ImageIcon is added to jbutton in NetBeans

An ImageIcon is added to button properties using NetBeans. print.setFont(new java.awt.Font("Serif", 0, 14)); print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/print.gif"))); print.setMnemonic('P'); …
VenuMadhava
  • 103
  • 1
  • 1
  • 6