Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs

Java Swing Tutorial Day 2. LEARN HOW TO MAKE A GUI


Java Swing Tutorial Day 2

G'Day Again fellow replers. It seems that there really is some people who enjoyed learning java swing.
So I will continue teaching it!


(One of) The java libraries for making Cross-platform Desktop Applications.

Previous Tutorials:
  • Day 1: We learned how to make a JFrame and button there!

The topics for today are...


JComponents are components included in the java library.

Unsurprisingly, hey all start with a J

We will be going over more JComponents in the future, but for today, we will be going over the following.

First up...


A checkbox is just a box which can be selected or not be selected indicated with a check. It is Extremely simple to use.

How to use JCheckBox!:

import javax.swing.*; import java.awt.*; public class Main{ private void setUpFrame(){ //Set up frame... //JCheckBox stuff! JCheckBox box = new JCheckBox("My first JCheckBox!"); //Creating an instance of JCheckBox box.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent e){ if(box.isSelected()){// Use box.isSelected to check whether or not the box is selected System.out.println("The box was selected!"); }else{ System.out.println("The box was unselected!"); } } }); /* Selecting or Unselecting a box fires an ItemEvent so we need an ItemListener Listeners are how you listen to events fired by components in the GUI. Almost every components has a specific event attached to it. When you add a Listener, what ever is in the overridded method is called. In the last tutorial, we used an ActionListener to listen for ActionEvents fired by a button */ frame.getContentPane().add(box); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args){ setUpFrame(); } }

This is how simple it is to use a JCheckBox. You can get all the information about the methods and constructors here.

Up next...


A JLabel is just a label which holds a string of characters and you can print that almost anywhere in a frame.
It is usually used to show the user information

Here is how to create a JLabel and to use it.

Note: From here on out, I will not include the code other than what is required(The JComponent we are going over)

JLabel label = new JLabel("My first JLabel!"); Button b = new Button("Click me to change the text"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String[] phrases = {"My first JLabel!", "Hello!", "The text Changed!", "Help me!", "repl is amazing"}; System.out.println(label.getText()); //label.getText() gets the current text in the JLabel String newText = phrases[(int)(Math.random()*phrases.length)]; //Choosing a random phrase label.setText(newText); //Use this to set the text! } }); frame.add(button); frame.add(label);

That is how simple it is to use a JLabel! You can use that to print information to the gui!

Check out the doc here!

And now the final JComponent we will be going over...


A JTextField is pretty much a text field. The user can type in information in it.

A text field also fires action events when the user presses enter.

In this example, pretend we are writing a chat client...

JTextField message = new JTextField(20); //The 20 means 20 columns. The can be one char per column message.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sendMessage(message.getText()); //sendMessage is just a seperate method which sends the message to the server. //message.getText() returns a string. That string is the text currently in the text field message.setText("Type message"); //This sets the text in the field to be whatever is inside the "". message.selectAll(); //Selects all the text that is currently in the message. message.requestFocus(); //requests focus so that the user can continue to type without having to press the text field again } }); /* There is another noteable method for JTextFields and the is... [yourtextfield].setEditable(boolean isEditable); You type a boolean as a parameter and this sets it to be editable or not. */

That is how simple it is to use a JTextField! Incredibly easily!
Check out the doc as well!

Well, that is it for this tutorial!

I hope you enjoyed and learned something new. You can try these JComponents in a new java swing project.

Please give feedback in the comments.

Thank you!

2 years ago
You are viewing a single comment. View All

Can there be an update for this tutorial series? It would be much appreciated!

1 year ago

edit: I ended it a bit after this, check out my other posts by clicking on my profile and going to the posts tab. filter by tutorials with ctrl/cmd-f

1 year ago

Oh, ok. thx. (I'm new at this!)

1 year ago