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 3

G'Day Again fellow replers. Sorry for the late post. I was kinda busy with other java projects...

Today we will continue tackling...


(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!
  • Day 2: We learned how to use JCheckBoxes, JLabels, and JTextFields.

Today, we will continue learning about...


Today, we will be going over the following. I am sorry for not making a very large tutorial for today.

SO a JPanel, what is a JPanel?

A JPanel is literally a container. You can use JPanels to separate the components you are using based on their position on the screen, and you can put as many JPanels as you want on a JFrame and even INSIDE other JPanels.

What is also great, is that you can change the layout for each JPanel to you liking!(Layouts will be gone over in the next tutorial)

The ULTIMATE tool for organizing

JPanels are probably the easiest to use as well.

JPanel panel = new JPanel(); panel.add(aButton); JPanel innerPanel = new JPanel(); innerPanel.add(aTextfield); innerPanel.add(aCheckBox); panel.add(innerPanel); frame.getContentPane().add(panel);

Simplicity to the MAXXX

Now, we welcome the most confusing component so far...


The JTextArea is just like a JTextBox, the user can type something in the box and an action event is fired when the user presses enter. Though it is much more complex to use.

JTextArea text = new JTextArea(10, 20);//10 means preferred amount of lines, 20 means preferred amount of columns per line. //Simple, right? // Well now, its will get confusing JScrollPane scroller = new JScrollPane(text); // We create a scroller for the text area text.setLineWrap(true); // This just means that lines can be wrapped // Also notice that we can edit the text even though it is a part of the scroller scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //This means that there will ALWAYS be a vertical scrollbar on the right scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); //Same as above but setting the horizontal bar to never be there frame.add(scroller); //WHAT, WHY DO WE ADD THE SCROLLER? // I do not know, for a JTextField and some other components, you have to add the scroller. NOT THE COMPONENT

More complicated than other JComponents but If you practice, it will be easy to get the hang of.

Notable Methods:

text.setText("This method sets the text"); text.append("This adds to the text"); text.selectAll("This selects everything in the textbox"); text.requestFocus("requests focus to the area"); text.setFont(aFont); //Fonts are part of the java library, we will go over them in a graphics segment in the future

Well, that is it for this tutorial!

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

Please give feedback in the comments.

Thank you!

Edit: I am sorry for adding so many badly formatted comments

2 years ago




***(^ω^)はい! ***

2 years ago

I discontinued it after and made this:

4 months ago
Load more