/*
* Jacareto Copyright (c) 2002-2005
* Applied Computer Science Research Group, Darmstadt University of
* Technology, Institute of Mathematics & Computer Science,
* Ludwigsburg University of Education, and Computer Based
* Learning Research Group, Aachen University. All rights reserved.
*
* Jacareto is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* Jacareto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with Jacareto; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
package jacareto.examples;
import jacareto.toolkit.awt.RatioLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* Class for testing some swing components.
*/
public class SwingTest extends JFrame {
/** The left list. */
private JList leftList;
/** The right list. */
private JList rightList;
/** The middle button (between the two lists). */
private JButton middleButton;
/** The text field. */
private JTextField inputField;
public JLabel valueLabel;
public JSlider vSlider;
/**
* Creates a new swing selection frame.
*/
public SwingTest () {
super("Swing Selection Frame Copy");
//
// adding a menu bar
//
JMenu menu;
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);
// Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic (KeyEvent.VK_A);
menu.getAccessibleContext ().setAccessibleDescription ("The only menu in this program that has menu items");
menuBar.add (menu);
// a group of JMenuItems
JMenuItem menuItem;
menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext ().setAccessibleDescription ("This doesn't really do anything");
menu.add (menuItem);
menuItem = new JMenuItem("Both text and icon");
menuItem.setMnemonic (KeyEvent.VK_B);
menu.add (menuItem);
menuItem = new JMenuItem("icon only ???");
menuItem.setMnemonic (KeyEvent.VK_D);
menu.add (menuItem);
// a group of radio button menu items
JRadioButtonMenuItem rbMenuItem;
menu.addSeparator ();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected (true);
rbMenuItem.setMnemonic (KeyEvent.VK_R);
group.add (rbMenuItem);
menu.add (rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic (KeyEvent.VK_O);
group.add (rbMenuItem);
menu.add (rbMenuItem);
// a group of check box menu items
JCheckBoxMenuItem cbMenuItem;
menu.addSeparator ();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic (KeyEvent.VK_C);
menu.add (cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic (KeyEvent.VK_H);
menu.add (cbMenuItem);
// a submenu
JMenu submenu;
menu.addSeparator ();
submenu = new JMenu("A submenu");
submenu.setMnemonic (KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add (menuItem);
menuItem = new JMenuItem("Another item");
submenu.add (menuItem);
menu.add (submenu);
// Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic (KeyEvent.VK_N);
menu.getAccessibleContext ().setAccessibleDescription ("This menu does nothing");
menuBar.add (menu);
//
// Upper left panel
//
JPanel upperLeftPanel = new JPanel();
upperLeftPanel.setLayout (new RatioLayout());
//
// fill the list
//
DefaultListModel listModel = new DefaultListModel();
listModel.addElement ("Adams, Jack");
listModel.addElement ("Miller, John");
listModel.addElement ("Connor, Diane");
listModel.addElement ("Smith, Tom");
leftList = new JList(listModel);
rightList = new JList(new DefaultListModel());
middleButton = new JButton("< >");
middleButton.setName ("middleButton");
upperLeftPanel.add ("0.05,0.05;0.30,0.90", leftList);
upperLeftPanel.add ("0.65,0.05;0.30,0.90", rightList);
upperLeftPanel.add ("0.40,0.05;0.20,0.90", middleButton);
//
// Lower left panel
//
JPanel lowerLeftPanel = new JPanel();
lowerLeftPanel.setLayout (new RatioLayout());
inputField = new JTextField();
lowerLeftPanel.add ("0.05,0.05;0.9,0.9", inputField);
//
// Upper right panel
//
JPanel upperRightPanel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButtonOne = new JRadioButton("one", true);
JRadioButton radioButtonTwo = new JRadioButton("two", false);
radioButtonTwo.setEnabled (false);
JRadioButton radioButtonThree = new JRadioButton("three", false);
JCheckBox checkBoxCheck = new JCheckBox("check", true);
buttonGroup.add (radioButtonOne);
buttonGroup.add (radioButtonTwo);
buttonGroup.add (radioButtonThree);
upperRightPanel.add (radioButtonOne);
upperRightPanel.add (radioButtonTwo);
upperRightPanel.add (radioButtonThree);
upperRightPanel.add (checkBoxCheck);
//
// Upper right panel, contains two sliders
//
JPanel lowerRightPanel = new JPanel();
vSlider = new JSlider(JSlider.VERTICAL, 0, 100, 1);
vSlider.setEnabled (false);
valueLabel = new JLabel();
valueLabel.setText ("" + vSlider.getValue ());
vSlider.addChangeListener (new ChangeListener() {
public void stateChanged (ChangeEvent e) {
SwingTest.this.valueLabel.setText ("" + SwingTest.this.vSlider.getValue ());
}
});
JSlider hSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 1);
vSlider.createStandardLabels (10);
hSlider.createStandardLabels (10);
vSlider.setPaintLabels (true);
vSlider.setMajorTickSpacing (20);
vSlider.setMinorTickSpacing (5);
vSlider.setPaintTicks (true);
hSlider.setPaintLabels (true);
hSlider.setMajorTickSpacing (20);
hSlider.setMinorTickSpacing (5);
hSlider.setPaintTicks (true);
lowerRightPanel.add (valueLabel);
lowerRightPanel.add (vSlider);
lowerRightPanel.add (hSlider);
//
// tabbed pane
//
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab ("uwe", upperLeftPanel);
tabbedPane.addTab ("carola", lowerLeftPanel);
tabbedPane.addTab ("juri", upperRightPanel);
tabbedPane.addTab ("christoph", lowerRightPanel);
tabbedPane.setSelectedIndex (0);
getContentPane ().add (tabbedPane);
//
// Whole frame
//
//getContentPane().setLayout( new RatioLayout() );
//getContentPane().add(tabbedPane);
/*
getContentPane().add( "0.0,0.0;0.80,0.9", upperLeftPanel );
getContentPane().add( "0.0,0.9;0.80,0.1", lowerLeftPanel );
getContentPane().add( "0.8,0.0;0.20,1.0", rightPanel );
*/
//
// event handler for button
//
ButtonListener buttonListener = new ButtonListener();
middleButton.addActionListener (buttonListener);
//
// event handler for left list
//
leftList.addMouseListener (new MouseAdapter() {
public void mouseClicked (MouseEvent e) {
if (e.getClickCount () == 2) {
SwingTest.this.moveListItem ();
}
}
});
//
// event handler for right list
//
rightList.addMouseListener (new MouseAdapter() {
public void mouseClicked (MouseEvent e) {
if (e.getClickCount () == 2) {
SwingTest.this.moveListItem ();
}
}
});
}
/**
* Moves an item between the two lists.
*/
private void moveListItem () {
if (leftList.getSelectedIndex () != -1) {
addItemToList (rightList, leftList.getSelectedValue ());
removeItemFromList (leftList, leftList.getSelectedIndex ());
} else if (rightList.getSelectedIndex () != -1) {
addItemToList (leftList, rightList.getSelectedValue ());
removeItemFromList (rightList, rightList.getSelectedIndex ());
}
}
/**
* This removes the item with the given index from the given JList.
*
* @param list JList object from which the item should be removed.
* @param index Index of the item to remove.
*/
private void removeItemFromList (JList list, int index) {
DefaultListModel listModel = (DefaultListModel) list.getModel ();
if (index <= listModel.getSize ()) {
listModel.remove (index);
//removed item in last position
if (index == listModel.getSize ()) {
index--;
}
//otherwise select same index
list.setSelectedIndex (index);
}
}
/**
* Adds the given item to the given list.
*
* @param list JList object to which the item should be added.
* @param item Item which should be added.
*/
private void addItemToList (JList list, Object item) {
if (item != null) {
DefaultListModel listModel = (DefaultListModel) list.getModel ();
listModel.addElement (item);
}
}
/**
* The main method which shows the frame.
*
* @param args DOCUMENT ME!
*/
public static void main (String[] args) {
JFrame f = new SwingTest();
f.setBounds (400, 300, 400, 300);
f.setVisible (true);
}
/**
* The button listener class.
*/
class ButtonListener implements ActionListener {
//~ Methods --------------------------------------------------------------------------------
public void actionPerformed (ActionEvent event) {
SwingTest.this.moveListItem ();
}
}
}
|