OOP-ch08-GUI and Events
OOP-ch08-GUI and Events
OOP-ch08-GUI and Events
FontMetrics
Graphics
Lightweight
Container
▪ JFrame, Jframe
▪ JPanel
▪ JDialogs
▪ ScrollPanes
▪ Applet: Web Applet
▪ JWindow
Top-level component
▪ Swing provides three generally useful top-
level container classes: JFrame, JDialog,
and JApplet.
Intermediate containers
▪ Swing provides several general-purpose intermediate
containers: scroll pane, split pane, tabbed pane….
Special-Purpose Containers
▪ The rest of the Swing intermediate containers are more
specialized:
Basic component controls
private void
jButton1ActionPerformed(java.awt.
event.ActionEvent evt) {
txtfile.setText(""+displayChosenF
ile());
}
private String displayChosenFile(){
String filestr=null;
JFileChooser file=new JFileChooser(".");
int select=file.showOpenDialog(null);
if(select==JFileChooser.APPROVE_OPTION){
File
selectedFile=file.getSelectedFile();
System.out.println(selectedFile.getParent());
System.out.println(selectedFile.getName());
try{
filestr=selectedFile.getCanonicalPath();
}catch(Exception e){
e.printStackTrace();
}
}
return filestr;
}
JColorChooser
▪ The JColorChooser class is used to create a color chooser dialog box so
that user can select any color.
▪ JColorChooser(): It is used to create a color chooser panel with white
color initially.
▪ JColorChooser(Color initialColor): It is used to create a color chooser
panel with the specified color initially.
Color c = JColorChooser.showDialog(this,
"Choose foreground color", Color.BLACK);
if(c != null){
jTextArea1.setForeground(c);
jButton1.setForeground(c);
}
Swing Menu Components
Objectives
JMenuBar class
▪ The JMenuBar class is used to display menubar on the
window or frame
▪ JFrame f= new JFrame(“Menu demo”);
▪ JMenuBar bar = new JMenuBar();
▪ f.setJMenuBar(bar);
JMenu class
▪ The object of JMenu class is a pull down menu component
which is displayed from the menu bar.
▪ Constructors:
▪ JMenu()
▪ JMenu(String label)
▪ JMenu mfile= new JMenu(“File”);
▪ bar.add(mfile);
▪ mfile.addSeparator();
▪ mfile.setMnemonic(KyEvent.VK_F);
JMenuItem
▪ Constructors:
• JMenuItem()
• JMenuItem(Action a)
• JMenuItem(Icon icon)
• JMenuItem(String text)
• JMenuItem(String text, Icon icon)
• JMenuItem(String text, int mnemonic)
▪ Important methods
• setEnable(boolean enable)
• setMnemonic(int mnemonic)
• setAccelerator(KeyStroke keyStroke)
▪ Add menu item to menu
• menuObject.add(menuItemObject)
menu item
JCheckBoxMenuItem
▪ Constructors:
• JCheckBoxMenuItem()
• JCheckBoxMenuItem(Action a)
• JCheckBoxMenuItem(String text)
• JCheckBoxMenuItem(Icon icon)
• JCheckBoxMenuItem(String text, Icon icon)
• JCheckBoxMenuItem(String text, boolean b)
• JCheckBoxMenuItem(String text, Icon icon, boolean b)
▪ Important methods
• boolean isSelected()
• get/ setSelected (boolean)
• get/setState(boolean)
▪ Add menu item to menu
menuObject.add(checkBoxMenuItemObject)
JCheckBoxMenuItem
▪ Constructors:
• JCheckBoxMenuItem()
• JCheckBoxMenuItem(Action a)
• JCheckBoxMenuItem(String text)
• JCheckBoxMenuItem(Icon icon)
• JCheckBoxMenuItem(String text, Icon icon)
• JCheckBoxMenuItem(String text, boolean b)
• JCheckBoxMenuItem(String text, Icon icon, boolean b)
▪ Important methods
• boolean isSelected()
• get/ setSelected (boolean)
• get/setState(boolean)
▪ Add menu item to menu
menuObject.add(checkBoxMenuItemObject)
Event for Menu item
Example
JTable
▪ The JTable class is used to display data in tabular form. It is
composed of rows and column.
▪ Constructors - Methods of Jtable
• JTable( Object[][] entries, Object[] columnNames )
• constructs a table with a default table model
• JTable( TableModel model )
• displays the elements in the specified, non-null table model
• int getSelectedRow()
• returns the index of the first selected row, -1 if no row is selected
• Object getValueAt( int row, int column )
• void setValueAt( Object value, int row, int column )
• gets or sets the value at the given row and column
• int getRowCount()
• returns the number of row in the table
JTable with changeable choices
▪ JTable:
▪ DefaultTableModel
▪ String[] cols= {“Code", “Name", “number of credits "};
▪ DefaultTableModel model=new DefaultTableModel(cols,0);
▪ JTable table = new JTable(model);
▪ JScrollPane pane = new JScrollPane(table);
▪ Add/remove elements
▪ Use the model, not the JTable directly
▪ void addRow( Object[] rowData )
▪ void insertRow( int row, Object[] rowData
▪ void removeRow( int row )
▪ void setValueAt( Object value, int row, int column )
▪ Void fireTableDataChanged()
Summary
▪ AWT vs Swing
▪ GUI Basics design and Top-level container
▪ Layout Manager, Common Control, Event Listener, Dialogbox
▪ Advanced Control
▪ Text component, Choice component, Menu..
▪ Tabbed pane, Scroll pane
▪ Dialog box
▪ Jlist
▪ Jtable and components in Table
Case study: TRAIN TICKET MANAGEMENT
▪ Class Customer includes (code,full name, birth of date,type) –
the customer type is Retail, Team, Online, A customer code is
a number with 5 digits and auto increasing.
▪ Class Ticket includes (code, seat type, price), A ticket code is a
number with 3 digits and auto increasing.
▪ Class Bill: each customer can buy one or more tickets. Each
seat type, a customer can only buy less than 5 tickets by a
date (dd/mm/yyyy) (each customer code and each ticket code
is appeared one line in a list of Bills)
▪ Design the form, named the java file is Main.java that has the
menu might look like figure:
If users select Customer, show Customer form: a
table (columns: code, first name, last name, birth of
date, type), 3 buttons Add, Save
(CUSTOMER.DAT), Edit and textfield for code, full
name, birth of date, ComboBox for type.
Using the same way above, to select Ticket, Ticket
Bill form has only 2 buttons: Add
form has 3 buttons Add, Save (TICKET.DAT),
and Save (BILL.DAT)
Remove
1 Sort a list of Tickets by price (descending)
2 Sort a list of Bills by date (to combine year, month
and day)
3 Sort a list of Bills by total money (total
money=number of tickets x price)
3 results added to the textarea in Main form
Output in the textarea in Main form
Where year extracted from date in Bill