Programming - Java GUI (swing)

    Hello again. After talking about some basic GUI Stuff using the awt library, we will now take a look at the javax.swing library that uses pretty much everything from the awt library and adds new features. I will start out with the stuff that stays almost the same, then we will take a look at the stuff that we use from the awt library and that doesn't exist in the swing library, and last but not least we will talk about the new features that swing gives us and awt doesn't give us that easily. Finally, I will create a swing Programm that calls out Input and Message Dialogs and create the same GUI we did last time using the swing library Components.


From awt to swing Component:

    The Components and Containers in the javax.swing library can be used the same exact way we used the awt Components and Containers. The name of the swing ones simply contains a 'J' letter more in front. So, Button becomes JButton, Frame becomes JFrame and so on...
   The same exact way as the Components in the awt library inherited from the Component Class we now have the swing Components inherit from the JComponent Class. All (I'm pretty sure) the Components and Containers from the awt library have a swing counterpart, but there are many new Components that are newly added in the swing library. 

Those new ones are things like:

Containers:

  • JInternalFrame
  • JLayeredPane
  • JDesktopPane
  • JRootPane
  • JSplitPane
  • JTabbedPane
  • JEditorPane
  • JTextPane
  • Box

That use some kind of specific Layout that puts Components inside layouted that way and let the user do more advanced stuff like getting text input in the whole container.

Buttons and Menu Items: 

  • BasicArrowButton
  • MetalScrollButton
  • MetalComboBoxButton
  • JToggleButton
  • JRadioButton
  • JMenu
  • JMenuItem
  • JCheckBoxMenuItem
  • JRadioButtonMenuItem
  • JMenuBar
  • JPopupMenu
  • JToolBar

That can be used for Click Input with some kind of design or special way of getting this information, and even create a Advanced Toolbar Menu.

Other Input:

  • JColorChooser
  • JFileChooser
  • JComboBox
  • JDialog
  • JFormattedTextField
  • JPasswordField
  • JSlider
  • JSpinner
  • JLayer
  • JTable
  • JTree
  • JOptionPane (for Output as well)

That let you get any kind of user Input inside of your programm

Other Stuff:

  • JProgressBar
  • JToolTip
  • JSeparator
  • JViewport


You can read more about all of them here

Stuff that doesn't/does change:

    We will use the same Layouts that we used with the awt library and the Actions and Events in which we will take a look next time can be used in awt and swing Objects. We can also use the exact same Component functions. But, we will also use some new functions that the JComponent gives us. One that will fix the bug that we had last time is the function setDefaultCloseOperation(static value) that lets us declare if the GUI closes when pressing the X Button, using the static value EXIT_ON_CLOSE.

New Features in-depth:

    Now let's get a better look at some of those new features that I love using! 

JOptionPane:

    Let's take a look at the JOptionPane first. This Object lets you do many things that contain:

  • showConfirmDialog(), that asks a question and let's you answer yes/no/cancel
  • showInputDialog(), that let's you input text Input
  • showMessageDialog(), that prints out a message
  • showOptionDialog(), that uses and does all of the above

I will use it for printing Messages and getting Input from the User in our final Coding Section.

JDialog:

    Let's take a look at the JDialog Object now. It does many of the same things that we can du with JOptionPane, but this time we can't simply use it directly, but it has to be inserted into some kind of Container. This way, we can create an more Advanced Dialog Class or Object of our own that contains many Components and other stuff.

JFileChooser:

    The Object JFileChooser opens an window that let's us select a file from our computer. This way we can load any file for reading in a way that doesn't need any keyboard input.

JMenu:

    The Object JMenu with the help of some others can be used for creating a Toolbar Menu. Adding MenuElements and other Menus we can create a Program that contains a very usable Toolbar. 

    This picture I found on Google explains I think everything you have to know. As you can see we will need Actions (we will create a menu when we get into Actions tomorrow).

Others:

    This was just a taste of what we are able to do. Using even more of those Components and creating more advanced custom ones that inherit from them, we can create a beautiful and useful GUI that can contain everything you can imagine. You can even create Computer Games, when we take a look into ButtonInput tomorrow! 

Coding Section:

    One Last thing before we get into Coding. Writing Code can be really difficult, but knowing the Coding can come pretty handy sometimes. There is a way of creating the GUI Code automatically using GUI Builders in many IDE's and that's the way you should do it too! Cause it's much more faster! But, as I already said, sometimes tweaking little things using Code instead of the IDE Tool can be really important.

    We will create the same GUI that we did last time using the swing library and create a programm that gets user Input, asks for confirmation and prints it right back using the JOptionPane. I will don't do more today cause the rest makes more sense later on.

Code 1:

import javax.swing.*;
import java.awt.*;
public class MyWindow{
    public static void main(String[] args) {
 // set up frame
 JFrame frame = new JFrame();
 frame.setSize(500, 500);
 frame.setTitle("MyWindow");
 frame.setBackground(Color.CYAN);
 frame.setLocationByPlatform(true);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 // set up panel
 JPanel panel = new JPanel();
 // set layout to 3x1 grid layout
 panel.setLayout(new GridLayout(3,1));
     
 // set up label
 JLabel label = new JLabel("Hello World!");
 label.setHorizontalAlignment(JLabel.CENTER);
     
 // set up text field
 JTextField textfield = new JTextField();        
 textfield.setText("Write what you want...");
     
 // set up button
 JButton button = new JButton();
 button.setBackground(Color.CYAN);
 button.setText("Press me!");
     
 // add components to panel
 panel.add(label);
 panel.add(textfield);
 panel.add(button);

 // add panel to frame and make it visible
 frame.add(panel);
 frame.setVisible(true);     
    }
}


Code 2:

import javax.swing.JOptionPane;
public class MyFirstSwingProgramm {
   public static void main(String[] args) {
       String name;
       int choice;
       // get Input
       name = JOptionPane.showInputDialog("Give me your Name");
       // confirmation
       choice = JOptionPane.showConfirmDialog(null, "Are you really "
       + name + "?");
       // print it back or not
       if(choice == 0){
           JOptionPane.showMessageDialog(null, "Hello " + name); 
       }
       else{
           JOptionPane.showMessageDialog(null, "Well, fine");
       }
   }
}

That was the End of today's Tutorial. Hope you enjoyed it! :)

    Next Time when we get into Actions and Events we will make an bigger programm that contains all the stuff we learned. So, be ready for a GUI menu with Buttons that prints out messages or gets input via Message and InputDialogs.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now