Saturday, March 10, 2012

Basic Swing Tutorial

Swing is a set of libraries that provide Java with the controls to make window like forms and graphics.

First Swing Form

Start a new Java project and add a new Java class called "SampleWindow". Here is the code for the class:


import javax.swing.*;

public class SampleWindow extends JFrame {
 
        public SampleWindow(){
  JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
 
 }
 
 

}

First we must import the swing libraries. Our class inherits (extends) the JFrame class that provides the basic window elements. In the constructor of the class we instantiate a new instance of JFrame

Add another class to the project called "Project" or whatever you want to call it. It should have a void main method. The code for this class is:


public class Project {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  SampleWindow w= new SampleWindow();
  w.setBounds(200, 100, 250, 150);
  w.setTitle("First Window");
  w.setVisible(true);
 }
}

In the main we instantiate the JFrame window and set a few of its properties, its bounds, its title and its visibility. The result is a simple empty frame.

Adding a Panel

Now we will add a panel and a couple of controls to the project. Add a new class called contentPanel. The code is below:


import java.awt.FlowLayout;

import javax.swing.*;

public class contentPanel extends JPanel{

 JPanel panel;
 JLabel label;
 JButton button;
 
 public contentPanel(){
  panel =new JPanel();
  panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  AddControls();
 }
 
 private void AddControls(){
  label = new JLabel("Click the Button"); 
  button = new JButton("Click Me");
  
  panel.add(label);
  panel.add(button);
  
  this.add(panel);
 }
}

We also need to add the panel to the JFrame


import javax.swing.*;

public class SampleWindow extends JFrame {
 
 public SampleWindow(){
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  JPanel p = new contentPanel();
  this.add(p);
 
 }
 
 

}

This class extends JPanel. We add two controls a JLabel and a JButton. In the constructor we initialize the panel and set the layout type to flow layout. The AddControls method initializes the controls and adds them to the panel. The last line adds the panel to the current container context.

When you run the program you get the following window:,/p>

Adding an ActionListener

Clicking on the button doesn't do anything at this point. In order to make it work we need to implement the actionListener interface. Here is the code with the actionListener added


import java.awt.FlowLayout;
import java.awt.event.*;

import javax.swing.*;

public class contentPanel extends JPanel implements ActionListener{

 JPanel panel;
 JLabel label;
 JButton button;
 
 public contentPanel(){
  panel =new JPanel();
  panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  AddControls();
 }
 
 private void AddControls(){
  label = new JLabel("Click the Button"); 
  button = new JButton("Click Me");
  button.addActionListener(this);
  
  panel.add(label);
  panel.add(button);
  
  this.add(panel);
 }
 
 public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();
  if (source==button)
   label.setText("You Clicked me");
 }
}


Note the added import statement that imports events. Also note the line that ties the button to the listener.


button.addActionListener(this);

in the actionPerformed method we first determine what control is the source of the event. If the control is the button we change the text of the label. Below is a picture of the frame after clicking the button.

No comments:

Post a Comment