package example;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.tree.*;
// TODO: Organize the layout with labels/tooltips/descriptions of what
// each one represents. Illustrate all aspects of recording a Swing UI.
//
// Component types and actions
// Focus accelerators
// Menu items
// Static/dynamic popup menus
// Tooltips
// Menu/button accelerators
// Mnemonics
// Tab-based keyboard traversal
// Drag/drop
public class MyCode {
private static class PopupAdapter extends MouseAdapter {
private boolean dynamic;
private JPopupMenu cachedMenu = null;
private int invokes = 0;
public PopupAdapter(boolean dynamic) {
this.dynamic = dynamic;
}
/** Some platforms popup here... */
public void mousePressed(MouseEvent ev) {
maybePopup(ev);
}
/** And some platforms popup here... */
public void mouseReleased(MouseEvent ev) {
maybePopup(ev);
}
/** And just in case... */
public void mouseClicked(MouseEvent ev) {
maybePopup(ev);
}
private void maybePopup(MouseEvent ev) {
if (ev.isPopupTrigger()) {
JPopupMenu menu = getPopupMenu();
menu.pack();
menu.show((Component)ev.getSource(), ev.getX(), ev.getY());
}
}
private JPopupMenu getPopupMenu() {
JPopupMenu menu = cachedMenu;
if (menu == null) {
menu = new JPopupMenu();
if (dynamic) {
menu.add(new JMenuItem("Invoked " + ++invokes + " times"));
menu.add(new JSeparator());
}
menu.add(new JMenuItem("Black"));
menu.add(new JMenuItem("Blue"));
menu.add(new JMenuItem("Orange"));
JMenu submenu = new JMenu("Other");
submenu.add(new JMenuItem("White"));
submenu.add(new JMenuItem("Green"));
menu.add(submenu);
if (!dynamic)
cachedMenu = menu;
}
return menu;
}
}
public static void setSystemProperty(String key, String value) {
System.setProperty(key, value);
}
public static void main(String[] args) {
final JFrame frame = new JFrame("My Code");/* {
// This will cause a NPE in the hierarchy browser
private String name = "My Code Frame";
public String getName() { return name.toString(); }
};*/
JPanel pane = new JPanel();
pane.setName("My Pane");
JLabel label = new JLabel("Static");
label.addMouseListener(new PopupAdapter(false));
pane.add(label);
label = new JLabel("Dynamic");
label.addMouseListener(new PopupAdapter(true));
pane.add(label);
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "My Dialog Message");
}
});
pane.add(button);
JTextField tf = new CustomTextField("Text field");
tf.setFocusAccelerator('a');
tf.setName("My Text Field");
pane.add(tf);
JComboBox cb = new JComboBox();
for (int i=0;i < 20;i++)
cb.addItem("Combo " + i);
pane.add(cb);
//pane.add(new JSpinner());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ev) {
//System.out.println("Action: " + ev.getActionCommand());
}
};
String[] myListData = { "zero", "one", "two", "three",
"four", "five",
"six", "seven", "eight"};
JList myList = new JList(myListData);
myList.setToolTipText("This is a list");
myList.setName("My List");
myList.setVisibleRowCount(4);
JScrollPane myScrollPane = new JScrollPane(myList);
myScrollPane.setName("My ScrollPane");
pane.add(myScrollPane);
DragLabel dl = new DragLabel("Drag me");
dl.setToolTipText("You can drag this label onto the tree to the right");
JPanel labeled = new JPanel(new BorderLayout());
labeled.add(dl, BorderLayout.WEST);
JTree myTree = new DropTree();
myTree.addMouseListener(new PopupAdapter(true));
myTree.setEditable(true);
myTree.setVisibleRowCount(4);
JScrollPane sp = new JScrollPane(myTree);
sp.setBorder(new TitledBorder("Over here"));
labeled.add(sp);
pane.add(labeled);
final Object[][] data = new Object[][] {
{ "0 one", "0 two", "0 three", Boolean.TRUE },
{ "1 one", "1 two", "1 three", Boolean.FALSE },
{ "2 one", "2 two", "2 three", Boolean.TRUE },
{ "3 one", "3 two", "3 three", Boolean.TRUE },
{ "4 one", "4 two", "4 three", Boolean.TRUE },
{ "5 one", "5 two", "5 three", Boolean.FALSE },
};
String[] names = { "one", "two", "three", "four" };
TableModel model = new DefaultTableModel(data, names) {
public Class getColumnClass(int col) {
return col == 3 ? Boolean.class : String.class;
}
};
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(200, myTree.getPreferredSize().height));
JScrollPane scroll = new JScrollPane(table);
pane.add(scroll);
JTextArea ta = new JTextArea("Four score and seven hundred years ago, our forebears extended claws reaching from the innermost mind to the outer limits", 10, 20);
ta.setFocusAccelerator('b');
ta.setToolTipText("<html>This is some <b>HTML</b> tooltip<br>text to look at</html>");
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
pane.add(new JScrollPane(ta));
JTabbedPane tp = new JTabbedPane();
// 1.4 only
//tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
for (int i=0;i < 10;i++) {
tp.add("tab " + i, new JLabel("Contents " + i
+ " "));
}
pane.add(tp);
pane.add(new JCheckBox("check box"));
frame.setContentPane(pane);
JMenuBar menubar = new JMenuBar();
menubar.setName("My Menu Bar");
JMenu menu = new JMenu("File");
JMenuItem mitem = new JMenuItem("Item 1");
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_I,
KeyEvent.ALT_MASK);
mitem.setAccelerator(ks);
menu.add(mitem);
mitem = new JMenuItem("Open");
ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.META_MASK);
mitem.setAccelerator(ks);
mitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new JFileChooser().showOpenDialog(null);
}
});
menu.add(mitem);
JMenu submenu = new JMenu("File submenu");
menu.add(submenu);
mitem = new JMenuItem("Quit");
ks = KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK);
mitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(mitem);
mitem = new JMenuItem("Submenu item");
mitem.addActionListener(al);