it appears for a few seconds when the mouse cursor stays motionless tool tip: a tool tip is a small and transient message about a container or atomic component it appears for a few seconds when the mouse cursor stays motionless on the container or atomic componentBIG QUESTIONlabel about the big question no need to create an object representing the tool tip just apply setToolTipText method on the existing cont. or comp. object: c.setToolTipText(text); JButton n1 = new JButton("#1"); p1.add(n1); // Button 1 is contained in p1 n1.setToolTipText("Nested Pannels");
button: JButton class a button is a rectangular area that, if clicked, creates an event object a button can contain a string text and/or an icon, like a label: buttonicon REWIND text create the button object: button = new JButton(); button icon REWIND text
set the font of the button: button.setFont(font); set the text and/or the icon: button.setText(text); button.setIcon(icon); if both text and icon, precise the position of the text relative to the icon: button.setHorizontalTextPosition( JButton.LEFT or JButton.CENTER or JButton.RIGHT); button.setVerticalTextPosition( JButton.TOP or JButton.BOTTOM); set enabled appearance (default appearance) or disabled appearance: button.setEnabled(true); button.setEnabled(false); establishes a character ( ‘a’ for instance) as an equivalent of the button: button.setMnemonic(character);
menu bar and menus: JMenuBar , JMenu , JMenuItem classes the menu bar contains several menus each of these menus is folded by default, but unfolds when we click it each menu , when unfolded, contains some menu items (sorts of buttons) eventually some separators between them eventually other (sub)menus a menu item, if selected, creates an event object
menu bar folded menu unfolded sub menu first menu second menu third menu menu item n.0 menu item n.1 menu item n.2 sub menu n.3 menu item n.4 unfolded menu sub menu n.5 folded sub menu separator
create the menu bar object: menu_bar = new JMenuBar(); add the menu bar to a frame (or applet): frame.setJMenuBar(menu_bar); create a menu object: menu = new JMenu(); set the text and/or the icon for the folded menu: menu.setText(text); menu.setIcon(icon); add to or remove from the menu bar a menu: menu_bar.add(menu); menu_bar.remove(menu);
create a menu item object: menu_item = new JMenuItem(); set the text and/or the icon of the menu item: menu_item.setText(text); menu_item.setIcon(icon); if both text and icon, precise the position of the text relative to the icon: menu_item.setHorizontalTextPosition( JMenuItem.LEFT or JMenuItem.CENTER or JMenuItem.RIGHT); menu_item.setVerticalTextPosition( JMenuItem.TOP or JMenuItem.CENTER or JMenuItem.BOTTOM);
add to or remove from the menu a menu item, a separator or another menu (the elements are piled up in the order in which they are added): menu.add(menu_item); or menu.remove(menu_item); menu.addSeparator(); menu.add(other_menu); or menu.remove(other_menu); set enabled appearance (default appearance) or disabled appearance: menu.setEnabled(true); or menu.setEnabled(false); menu_item.setEnabled(true); or menu_item.setEnabled(false); establishes a character (‘a’ for instance) as an equivalent of the menu item: menu_item.setMnemonic(character);
check boxes: JCheckBox class any number of check boxes in a group can be selected at a given time Hakan Mehmet Fatma
first, create a panel panel with grid layout policy (any number of rows, 1 column) for each of the check boxes ci: ci = new JCheckBox(); ci.setText(text); eventually: ci.setIcon(icon); ci.setSelected(true or false); panel.add(ci); the current state (true or false) of check box ci is returned by ci.isSelected()
radio buttons: JRadioButton class at most one button at a time can be selected Yiltan Mehmet Ali
first, create a panel with grid layout policy (any number of rows, 1 column) for each of the radio buttons ri: ri = new JRadioButton(); ri.setText(text); eventually: ri.setIcon(icon); ri.setSelected(true or false); p.add(ri); initially, we should select either no button or just one button (if we select more than one button, then only the first one will actually be selected)
Then, group all radio buttons ri into a group of type ButtonGroup: g = new ButtonGroup(); g.add(r1); . . . . . . g.add(rn); the current state of radio button ri is returned by ri.isSelected()
text field: JTextField class one editable line of text at the end of which we type Return typing Return will trigger an event Return will NOT be represented by a newline character at the end of the string text this is a line of text currently edited
create the textfield object: textfield = new JTextField(); or textfield = new JTextField(desired number of characters); set the font of the text field: textfield.setFont(font); eventually, set an initial text: textfield.setText(text); obtain the modified text: textfield.getText() returns the current text select all the text: textfield.selectAll();
text area: JTextArea class several editable lines of text in a rectangular area corresponding to a certain number of rows and of columns line wrapping: when we arrive at the end of a row, the text line is wrapped onto the next row This is a full text currently edited; the number of rows is 4 and the number of columns is 29
create the textarea object: textarea = new JTextArea( desired number of rows , desired number of columns); set the font of the text area: textarea.setFont(font); turn line wrapping on: textarea.setLineWrap(true); enforce wrap between words: textarea.setWrapStyleWord(true); eventually, set an initial text: textarea.setText( row1 string + row2 string + …. + rown string );
obtain the modified text: textarea.getText() returns the current text select all the text: textarea.selectAll();
Scrollable text area: encapsulate the text area inside a scroll pane of type JScrollPane: scrollpane = new JScrollPane(textarea , vertical scrollbar policy , horizontal scrollbar policy); the scrollbar policies are defined as: vertical scrollbar policy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED or JScrollPane.VERTICAL_SCROLLBAR_ALWAYS or JScrollPane.VERTICAL_SCROLLBAR_NEVER horizontal scrollbar policy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED or JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS JScrollPane. HORIZONTAL_SCROLLBAR_NEVER then, add the scroll pane to the container if you have turned lined wrapping on, the horizontal scroll bar will never become active...
combo box: JComboBox class editable popup menu whose choices are some strings (stored in an array) we can select a choice string (it creates an event object) we can edit the selected choice string (as in a text field) (typing Return also creates an event object) choice n.1 folded combo box choice n.2 choice n.0 choice n.3 unfolded combo box selected choice
create the combo box object: combobox = new JComboBox(); or combobox = new JComboBox(array of strings); authorize the selected string to be edited: combobox.setEditable(true); select a certain choice string by specifying its index (starts from 0): combobox.setSelectedIndex(index); insert a new choice string at index position: combobox.insertItemAt(string , index); obtain the selected string: (String )combobox.getSelectedItem()
list: Jlist class create an instance of class JList: aaa bbb uuu ccc zzz www create an instance of class JList: list = new JList(array of strings); set the newly selected index: list.setSelectedIndex(index);
obtain the currently selected index list obtain the currently selected index list.getSelectedIndex() returns index test if there is no selection: list.isSelectionEmpty() returns true or false forces all strings to be non selected: list.clearSelection();
file chooser: JFileChooser class a.jpg b.gif u.jpg c.jpg z.gif w.jpg instruc. cancel All Files (*.*) File name: Look in: directory create an instance of class JFileChooser with initial selection of the user's home directory: fc = new JFileChooser(); with initial selection of a given directory: fc = new JFileChooser(directory_name_string);
show the dialog box (related to frame): fc.showDialog(frame , instruction_string) returns value JFileChooser . APPROVE_OPTION or JFileChooser . CANCEL_OPTION or JFileChooser . ERROR_OPTION obtain the selected file: fc.getSelectedFile() returns an instance of File fc.getSelectedFile().getPath() returns a string = full path of the file
slider: JSlider class cursor that the operator can slide with the mouse along a (graduated) rule the value currently pointed at by the cursor can be read by the program the minimum value, maximum value and current value are all integers (int) major tick 6 12 18 24 minor tick current value pointed at by the cursor major interval minor interval
create the slider object: slider = new JSlider( JSlider.HORIZONTAL or JSlider.VERTICAL , min , max , init_value); modify the min, max and current values: slider.setMinimum(min); slider.setMaximum(max); slider.setValue(value);
by default, max value is at right if horizontal slider, at top if vertical slider to have it at left if horizontal slider, at bottom if vertical slider: slider.setInverted(true); establish major and minor ticks according to some intervals: slider.setMajorTickSpacing(major_interval); slider.setMinorTickSpacing(minor_interval); display the ticks: slider.setPaintTicks(true); display the values at each major tick: slider.setPaintLabels(true); obtain the current value of the slider: slider.getValue()
Events created by containers and atomic components
the GUI managing thread implements the response to an event by calling a previously defined event method such event methods must be executed very fast so as not to delay the GUI managing thread
event source, event, event listener: event source object: frame applet panel button menu item check box radio button text field combo box list slider
creates an event object of a certain type ActionEvent ChangeEvent FocusEvent KeyEvent MouseEvent ComponentEvent WindowEvent
one or several event listener objects(attached to the event source object) respond to the corresponding type of event object by calling some specific event methods ActionListener ChangeListener FocusAdapter KeyAdapter MouseAdapter MouseMotionAdapter ComponentAdapter WindowAdapter
an event listener responding to an event of type X has for name XListener or Xadapter use Xadapter if you want to define only one of the possible event methods associated to event X
defining an event listener: an event listener is an object which contains the definition of one (or several) event method that event method receives the event object as argument = defined and created as an anonymous class which embeds the event method(s) reason why: no function pointer as in C…
define an event listener of events of type X with an event method and attach it to a source event object s: s.addXListener(new XListener() { public void event_method(XEvent e) { . . . . . . } } );
or, if there are several possible event methods associated to this type of event: s.addXListener(new XAdapter() { public void event_method(XEvent e) { . . . . . . } } ); inside the event methods of the event listeners, we can address with no restriction at all (no need of final…) the elements of the GUI = they are instance variables of the class of the GUI and not local variables or arguments of constructor Gui()
Example button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println( "button1 clicked"); } } ); panel1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println( "mouse clicked inside the panel1"); } } );
table of events and associated event listeners and event methods: possible event sources event object information on event object e Attach a listener to the event source listener of the event object possible event methods button (if clicked) menu item (if selected) check box (if clicked) radio button (if clicked) text field (if Return typed) combo box (if item selected or Return typed) ActionEvent addActionListener ActionListener actionPerformed slider (if its cursor is being moved) ChangeEvent addChangeListener Change -Listener stateChanged list (if selection in the list changes) ListSelectionEvent addListSelection ListSelection valueChanged
frame (if keyboard focus applet is changed) FocusEvent addFocusListener FocusAdapter focusGained focusLost frame (if keyboard touched applet while frame or applet has keyboard focus) KeyEvent e.getKeyChar() addKeyListener KeyAdapter keyTyped (character key) e.getKeyCode() (KeyEvent.VK_SHIFT , KeyEvent.VK_UP) keyPressed (action or keyReleased modifier key)
panel (if mouse cursor is frame inside panel, frame applet or applet) MouseEvent (recommended for use with panel) e.getX() e.getY() e.getClickCount() e.getButton() (MouseEvent.BUTTON1 , MouseEvent.BUTTON2 , MouseEvent.BUTTON3) addMouseListener MouseAdapter mouseClicked mousePressed mouseReleased mouseEntered (the event mouseExited source) addMouseMotion -Listener MouseMotion -Adapter mouseDragged mouseMoved
frame applet ComponentEvent addComponent -Listener Component -Adapter componentMoved componentResized WindowEvent AddWindow Window windowClosed windowIconified windowDeiconified