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
29
votes
5 answers

How to create on click event for buttons in swing?

My task is to retrieve the value of a text field and display it in an alert box when clicking a button. How do I generate the on click event for a button in Java Swing?
Suresh
  • 501
  • 1
  • 5
  • 11
27
votes
4 answers

Can't a Swing component be added to multiple containers?

I'm trying (testing something else) to add one JButton reference into two JPanels to test it, and it disappears from the first panel it was added to! So, can't a Swing component be added to multiple containers? Thank you in advance.
Juan Diego
  • 863
  • 1
  • 10
  • 22
27
votes
3 answers

How to disable javax.swing.JButton in java?

I have created a swings application and there is a "Start" button on the GUI. I want that whenever I clicked on that "Start" button, the start button should be disabled and the "Stop" button be enabled. For that I have written the following code in…
Amit
  • 33,847
  • 91
  • 226
  • 299
23
votes
9 answers

Rounded Swing JButton using Java

Well, I have an image that I would like to put as a background to a button (or something clicable). The problem is that this image is round, so I need to show this image, without any borders, etc. The JComponent that holds this button has a custom…
José Leal
  • 7,989
  • 9
  • 35
  • 54
23
votes
6 answers

Why will BoxLayout not allow me to change the width of a JButton but let me change the height?

I'm trying to get the Layout of a JDialog of mine to fit a particular look that a program in which I'm porting to Java has, I've used several LayoutManagers before with great success yet for some reason I cannot seem to get this working at all. My…
Brandon Buck
  • 7,177
  • 2
  • 30
  • 51
22
votes
7 answers

How to place a JButton at a desired location in a JFrame using Java?

I want to put a JButton at a particular coordinate in a JFrame. I used setBounds() for the JPanel (which I placed on the JFrame) and also setBounds() for the JButton. However, they don't seem to function as expected. My Output: This is my…
Haxed
  • 2,887
  • 14
  • 47
  • 73
21
votes
2 answers

Can I make Swing JButtons have smaller margins?

I prefer buttons with minimal margins, about as wide as their text caption. Is there a way to achieve that in a JButton in Swing? What I am seeing in practice now is that even if I try to use setMaximumSize() and similar, it just ends up eating the…
EndangeringSpecies
  • 1,564
  • 1
  • 17
  • 39
21
votes
3 answers

Add a complex image in the panel, with buttons around it in one customized user interface

How can i have this image like below into the slavePanel and on top of that JPanel adjust the JButtons which looks like the image but having buttons correctly wrapped around? (Right now they are shaped in 1 row, 4 column) // // Shot Gun mover…
user285594
20
votes
2 answers

Setting JButton text to align to the left?

I previously had a JLabel, that I wanted to be click-able. The easiest way I found to do this was make it a JButton and using the following code. It now looks like a JLabel button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2,…
Halfwarr
  • 7,853
  • 6
  • 33
  • 51
20
votes
7 answers

How to close a GUI when I push a JButton?

Does anyone know how to make a jbutton close a gui? I think it is like System.CLOSE(0); but that didnt work. it also could be exitActionPerformed(evt);, but that didn't work either. just the line of code will work. EDIT: never mind guys. the answer…
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
20
votes
8 answers

How to set background color of a button in Java GUI?

Below is the code which creates 9 buttons in gridlayout form on a specific pannel3. What i want is to make the background of each button black with grey text over it. Can anyone help please? for(int i=1;i<=9;i++) { p3.add(new…
Salar
  • 259
  • 2
  • 6
  • 16
20
votes
5 answers

Draw a JButton to look like a JLabel (or at least without the button edge?)

I've got a JButton that for various reasons I want to act like a button, but look like a JLabel. It doesn't actually have to be a JLabel under the hood, I just don't want the raised button edge to show up. Is there an easy way to turn off the…
Electrons_Ahoy
  • 36,743
  • 36
  • 104
  • 127
20
votes
2 answers

smooth rounded corners in swing

I want to make smooth rounded corners for my swing application, but I can't get my desired result... here's the screenshots: 1.setShape() for JFrame : 2.overriding paintComponent() method for JPanel instead of using setShape()…
FaNaJ
  • 1,329
  • 1
  • 16
  • 39
18
votes
8 answers

Right click on JButton

I am trying to write a Minesweeper clone in Java for fun. I have a grid of JButtons whose labels I will change to represent the danger count, flags, etc. My problem is, I don't know how to get a right click on a JButton to depress the button. …
I82Much
  • 26,901
  • 13
  • 88
  • 119
18
votes
9 answers

How to change a JButton color on mouse pressed?

I want to have custom colors according to the mouse events (mouse enter, exit, pressed, etc). So to accomplish this, I wrote the code below. It is fine for everything, except in the case of the mouse pressed event, which does nothing. It only works…
ktulinho
  • 3,870
  • 9
  • 28
  • 35