Skip to content
Advertisement

how to make more buttons in java GUI swing

I would like to use the java swing to make two buttons, one that add +25 to 100, the initial value, and one other that adds +10 to 100, always the initial value:

JavaScript

as you can see it should create two buttons, this is what I receive as output at first:

JavaScript

https://i.stack.imgur.com/1MSHz.png

and after I click it, it just adds 25 to the first one:

JavaScript

Advertisement

Answer

When creating a Swing GUI, it’s a really good idea to separate your data model from the GUI view. This separation makes it easier to code the model and easier to code the view. The controller updates the model, which in turn updates the view.

Here’s a GUI I whipped up to illustrate this model / view / controller pattern.

Increment GUI

The first thing I did was define some fields and an array.

JavaScript

The startValue is the starting value (100), the currentValue is the current value (205 in the picture), and values is an int array that holds the values 10 and 25.

Here’s the code that sets these values. I set these values in the constructor of the class, so they’re set before anything else happens.

JavaScript

Since I put the values in an int array, if I want to add another value, all I have to do is add the value to the values array.

Now that we have the model built, let’s start on the view. This is my main method.

JavaScript

I call the SwingUtilities invokeLater method to ensure that the Swing components I create and execute are created and executed on the Event Dispatch Thread. This keeps us from having threading problems with the GUI.

When I instantiate the class, the model fields are set in the constructor, then the GUI is created.

Here’s the method I wrote to create the JFrame.

JavaScript

The JFrame methods have to be called in a specific order. This is the order that I use for most of my Swing applications.

The setDefaultCloseOperation method allows me to close the application when the X button in the upper right is left-clicked. The pack method takes the Swing components and “packs” them into the smallest possible JFrame consistent with the size of the Swing components. The setLocationByPlatform method sets the location of the JFrame on the screen consistent with the platform operating system. Finally, the setVisible method makes the JFrame visible.

We created two JPanels, one to hold the “Value” JLabel and JTextField, and the other to hold the JButtons. We do this because I want to use two different Swing layout managers to layout the Swing components.

The JFrame has a BorderLayout by default. I placed the value JPanel before the first line (on top) and the button JPanel in the center.

Next, I coded the details of the value JPanel in the createValuePanel method.

JavaScript

I used a JTextField to hold the current value. I made it non-editable, so the user cannot change the value.

I wrote the setValueField method as a separate method because we’re going to use this method in the actionPerformed method of the ActionListener.

We use a FlowLayout because I want the Swing components to flow from left to right.

Next, I coded the details of the button JPanel in the createButtonPanel method.

JavaScript

I used a GridLayout to hold a single column of the JButtons. I put some empty space around the JButtons with an empty border to make the GUI more visually appealing.

Because I create the JButtons in a for loop, I create as many JButtons as there are values in the values array. This way, when you add a value to the values array, a new JButton will be created.

A JButton can hold an action command String as well as display text. We use this action command String to pass the increment value to the actionPerformed method.

Finally, I coded the actionPerformed method. It’s pretty simple. I get the increment value from the JButton, increment the current value, and display the current value in the JTextField.

JavaScript

Here’s the entire runnable example. I hope this explanation helps you to create more complex Swing GUIs.

JavaScript
Advertisement