Apr 26, 2013 ... Also known as drop down list. • JComboBox() an empty drop down list. •
JComboBox(Object[] list) a drop down list filled with the array elements.
4/26/2013
JComboBox • Also known as drop down list
JAVA GUI 2 JComboBox (drop down list) JMenuBar, JMenu, JMenuItem JOptionPane (dialog boxes) JCheckBox, JRadioButton, ButtonGroup, ItemListener JList JSplitPane
• JComboBox() • JComboBox(Object[] list)
an empty drop down list a drop down list filled with the array
elements • addItem(Object obj)
add a new item to end of list
• insertItemAt(Object obj, int index) • getItemAt(int index) • getSelectedItem() • getSelectedIndex() • getItemCount()
# of items in the drop down list
• addActionListener(ActionListener listener) • addItemListener(ItemListener listener)
JComboBox Example public class ComboBoxDemo { private JFrame frame; private String[] strFruits = { “Apple”, “Pear”, “Mango”, “Banana”, “Pineapple” }; private JComboBox ddlFruits; // executed whenever an item from the drop down list is chosen by the user private class ComboBoxActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(ddlFruits.getSelectedItem()); } } public ComboBoxDemo() { frame = new JFrame(“JComboBox Demo”); frame.setLocation(100, 200); // initial location frame.setSize(400, 300); // initial size frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Adding a Menu to Frame • JMenuBar • JMenuBar() • add(JMenu) • JMenu • JMenu(String) • add(JMenuItem) • addSeparator()
• JMenuItem • JMenuItem(String) • JMenuItem(String menuTitle, int mnemonicKey) • addActionListener(ActionListener) • setEnabled(boolean) • Frame • setJMenuBar(JMenuBar)
ddlFruits = new JComboBox(strFruits); ddlFruits.addActionListener(new ComboBoxActionListener()); frame.getContentPane().add(ddlFruits, BorderLayout.NORTH); frame.setVisible(true); } ..... }
Setting Mnemonic (key short-cut) • For component to have a short-cut key • component.setMnemonic(int key_sequence)
Adding Menu to Application private JFrame frame; miNew= new JMenuItem(“New”); miOpen= new JMenuItem(“Open”); miCopy= new JMenuItem(“Copy”); miCut = new JMenuItem(“Cut”); miZoom= new JMenuItem(“Zoom”);
private JMenuBar menuBar; private JMenu menuFile, menuEdit, menuView; private JMenuItem miNew, miOpen, miCopy, miCut, miZoom; public MenuDemo() {
• example key_sequence: constants in KeyEvent class • function keys: VK_F1, VK_F2, VK_F3, ... • arrow keys: VK_KP_DOWN, VK_KP_UP, ... • letter keys: VK_A, VK_B, VK_C, ..., VK_Z • # keys: VK_0, VK_1, VK_2, ..., VK_9 • # keys on Number Pad: VK_NUMPAD0, VK_NUMPAD1, ...
frame = new JFrame(“Menu Demo”);
miNew.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(“Start a new file”); } } miNew.setMnemonic(KeyEvent.VK_N);
frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = frame.getContentPane(); menuBar = new JMenuBar();
menuFile.add(miNew); menuFile.add(miOpen);
frame.setJMenuBar(menuBar); menuFile = new JMenu(“File”);
menuEdit.add(miCopy); menuEdit.add(miCut);
menuEdit = new JMenu(“Edit”); menuView = new JMenu(“View”);
• e.g., menuItem.setMnemonic(VK_F1); this menuItem can
be activated by F1 function key.
menuView.add(miZoom);
menuBar.add(menuFile); menuBar.add(menuEdit);
.....
menuBar.add(menuView); frame.setVisible(true); // continue to right
}
1
4/26/2013
JOptionPane • allows to create a popup box to communicate with the user • static methods to easily pop up dialog boxes • showMessageDialog: to show messages • showConfirmDialog: to ask for confirmation • showInputDialog: to ask user to enter text or choose among options • showOptionDialog: most flexible way to interact with the user • Message Type Constants: • ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE • Examples • JOptionPane.showMessageDialog(parentComponent, message) • JOptionPane.showMessageDialog(parentComp, message, title, msgType)
JPanel as a drawing pad (or Canvas)
JOptionPane Example • String input = JOptionPane.showInputDialog(frameWorld, “Enter # rows: ”); • creates a pop-up dialog box • frameWorld (container) is the parent and the dialog box displays at the
center of the parent • returns as a String what user enters • int answer = JOptionPane.showConfirmDialog(null, “Question”); • brings up a pop-up box with Yes, No, Cancel options • the title for the box is “Select an Option” • returns one of the following values: • JOptionPane.YES_OPTION • JOptionPane.NO_OPTION • JOptionPane.CANCEL_OPTION • See JOptionPane API for more information
MouseListener
• create a new class that extends JPanel
• Java interface
• override JPanel’s void paintComponent(Graphics g) method
• Contains 5 abstract methods for detecting mouse events
to draw something
• mouseEntered(MouseEvent e)
• start by clearing the screen: super.paintComponent(g);
• mouseExited(MouseEvent e)
• redraw everything
• mouseClicked(MouseEvent e)
• when the drawing should be updated, call repaint() method • for example, after new shapes are added to the Canvas • repaint() calls paintComponent(...)
• mousePressed(MouseEvent e) • mouseReleased(MouseEvent e)
• Useful methods in MouseEvent • Point getPoint(): returns the (screen) location of the mouse event • int getX(): returns the x-coordinate of the mouse event • int getY(): returns the y-coordinate of the mouse event
MouseMotionListener
MouseAdapter
• Java interface
• a convenient class which implements both • MouseListener • MouseMotionListener • overrides ALL methods with { }
• Contains 2 abstract methods for detecting mouse motion
events • mouseMoved(MouseEvent e): mouse moves w/o any button pressed • mouseDragged(MouseEvent e): mouse moves w a button pressed
• A class can _extend_ MouseAdapter and override ONLY
the methods it needs • e.g., DrawListener in HW7 only overrides mouseDragged,
mousePressed, and mouseReleased (instead of all 7 methods).
2
4/26/2013
JColorChooser
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public ColorChooserDemo() { bnColor = new JButton("Choose a color"); bnColor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color newColor = JColorChooser.showDialog(frame, "Choose a color", currentColor);
public class ColorChooserDemo {
• Offers a convenient static method which shows a color-chooser
dialog • JColorChooser.showDialog(Component parent, String title, Color
initColor) • parameters • parent: parent component for this dialog • title: title for the dialog • initColor: initial color set when the color chooser is shown • addActionListener(ActionListener) • return value • Color: if the user presses OK, the dialog box returns the color user chooses (or initColor if no change) • null: if the user presses CANCEL or closes the dialog box
private private private private
if (newColor != null) { currentColor = newColor; content.setBackground(currentColor); }
JFrame frame; Container content; JButton bnColor; Color currentColor;
public ColorChooserDemo() { int fWidth = 300; int fHeight = 300; currentColor = Color.WHITE; frame = new JFrame("Color Chooser Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE); frame.setLocation(200, 300); frame.setSize(fWidth, fHeight);
} }); content.add(bnColor,BorderLayout.NORTH); frame.setVisible(true);
} /** * @param args */ public static void main(String[] args) { new ColorChooserDemo(); } }
content = frame.getContentPane(); content.setBackground(currentColor);
JList • Similar to JComboBox • Allows multiple select and can display multiple items as
well • Constructors • JList(): creates an empty JList object • JList(E[] array): creates a JList object populated with values from the array • Useful methods • getSelectedIndices() • setListData(E[] array) • see SplitDemo.java (in class)
JSplitPane • Allows a way to have multiple Panes in a single frame • horizontal or vertical split into two panes • can nest if more than two panes are needed • Constructor • JSplitPane( split_direction, container1, container2); • split_direction • JSplitPane.HORIZONTAL_SPLIT • JSplitPane.VERTICAL_SPLIT
• container can be any container (JPanel, JScrollPane, ...)
• see SplitDemo.java (in class)
3