Sunncity.com - Developer & I.T. Zone

 
Sunncity Web

Technology Menu

Developer Center
Delphi Online Tutorial
JAVA Online Tutorial
The Code Project
XML Code Library
FREE Download
Our Free Components
Our Free Software
Free Components
Delphi & Kylix Training
SMTC Training Course
About Delphi EXAM

Search Component


Search Software(s)


by oneNetwork

 

Java Online Tutorial - PACKAGE JAVAX.SWING

PACKAGE JAVAX.SWING.*

One thing I didn't mention in the last chapter is that Internet Explorer uses a Java Virtual Machine made by microsoft and it is (sadly) not swing-compatible (yet). Sun provides a Java Virtual Machine which can be plugged into Internet Explorer and this is swing compatible. To use this, you also have to modify the html file which runs your applet to use the Sun's JVM. Sun has provided a html converter for this purpose which can be downloaded from the web. Do not worry, the size of the html converter is less than 200KB.

Now, you can extend from JApplet insteadof Applet and use all of the attractive features which make swing swing (pun intended). As the last chapter, I am not going to break down the chapter into components, layouts and event handling, because you will seldom encounter these elements in isolation in your java code. Usually all three elements will combine together to form a coherent whole. Enough theory, now lets see some examples.


Swing Component

As we have said before, to move from the awt to swing, all you have to do is append a J to your component name and extend from JApplet instead of Applet. (if it is an applet, at all.) Otherwise you extend from JFrame or JPanel or whatever.

 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RadioButtonDemo extends JPanel  {
static JFrame frame;
static String birdString = "Bird";
static String catString = "Cat";
static String rabbitString = "Rabbit";
JLabel picture;

public RadioButtonDemo() {
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic('b');
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic('c');
catButton.setActionCommand(catString);

JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic('r');
rabbitButton.setActionCommand(rabbitString);

ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(rabbitButton);

RadioListener myListener = new RadioListener();
birdButton.addActionListener(myListener);
catButton.addActionListener(myListener);
rabbitButton.addActionListener(myListener);

picture = new JLabel(new ImageIcon("images/" + birdString + 
".gif"));
picture.setPreferredSize(new Dimension(177, 122));

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(0,1));
radioPanel.add(birdButton);
radioPanel.add(catButton);
radioPanel.add(rabbitButton);

setLayout(new BorderLayout());
add(radioPanel, BorderLayout.WEST);
add(picture, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

}
		
		
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
picture.setIcon(new ImageIcon("images/" + 
e.getActionCommand() + ".gif"));

}
}

public static void main(String args[]) {
frame = new JFrame("RadioButtonsDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(new RadioButtonDemo(), 
BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

}

}

Back to TOP


Using a JToolbar

Often you may want to use a toolbar instead of a menu. See the example below. By default, the user can drag the toolbar to a different edge of its container or out into a window of its own. However, for the drag-out behaviour to work properly, the toolbar must be in a container that uses BorderLayout. The toolbar must be the only other component in the container and it must not be in the center.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ToolBarDemo extends JFrame {

  protected JTextArea textArea;
  protected String newline="\n";

  public ToolBarDemo() {
  super("ToolBarDemo");
  addWindowListener(new WindowAdapter() {
  public void windowclosing(WindowEvent e) {
  System.exit(0);
  }
  });

JToolBar toolBar = new JToolBar();
addButtons(toolBar);
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);

JPanel contentPane = new JPanel() ;
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension( 400, 100));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
setContentPane(contentPane);

}

protected void addButtons(JToolBar toolbar) {

JButton button = null;

button = new JButton(new ImageIcon("images/left.gif"));
button.setToolTipText("This is the left button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for the first button");
}
});
toolbar.add(button);

button = new JButton(new ImageIcon("images/middle.gif"));
button.setToolTipText("This is the middle button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for the second  button");
}
});
toolbar.add(button);

button = new JButton(new ImageIcon("images/right.gif"));
button.setToolTipText("This is the right button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for the third button");
}
});
toolbar.add(button);

}

protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
}

public static void main(String [] args) {
ToolBarDemo frame = new ToolBarDemo();
frame.pack();
frame.setVisible(true);

}

}

Be sure to try out the examples. A link to them is here.

Back to TOP

 

Now on to the next chapter All about Input & Output Streams


All questions and comments can be addressed to the author.
All material appearing within this website is copyright protected and may not be reproduced elsewhere without the express written permission of the author (Sanjeev Dasgupta)

 


Support this site BUY from Sunncity Gift Store