122
Copyright 2003 Mudra Serv ices 1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out in one page, it can be split into multiple tabs Not available in AWT But somewhat similar to CardLayout A lot of text editors use it to show open files. Can work with multiple files simultaneously.

Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Embed Size (px)

Citation preview

Page 1: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

1

SWING - JTabbedPane

Helps stack pages of information into a single point of reference

If information cannot be laid out in one page, it can be split into multiple tabs

Not available in AWT But somewhat similar to CardLayout

A lot of text editors use it to show open files. Can work with multiple files simultaneously.

Page 2: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

2

SWING - JTabbedPane

Clicking on Name tab

Page 3: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

3

SWING - JTabbedPane

Clicking on Address tab

Page 4: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

4

SWING - JTabbedPane

Clicking on Other tab

Page 5: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

5

SWING - JTabbedPane

package tabbedexample;import java.awt.*;import javax.swing.*;

public class TabbedExample extends JFrame {

public TabbedExample() { // creation of the tabbed pane object JTabbedPane pane = new JTabbedPane();

// create the 3 panels JPanel panel1 = createPanel1(); JPanel panel2 = createPanel2(); JPanel panel3 = createPanel3();

Page 6: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

6

SWING - JTabbedPane

// add the panels to the pane pane.addTab("Name",panel1); pane.addTab("Address",panel2); pane.addTab("Other",panel3); pane.setSelectedComponent(panel1);

// create button panel JPanel buttons = new JPanel(); buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5)); buttons.add(new JButton("Submit"));

// add the panel to top panel getContentPane().add(pane,BorderLayout.CENTER); getContentPane().add(buttons,BorderLayout.SOUTH); }

Page 7: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

7

SWING - JTabbedPane

// create panel 1 private JPanel createPanel1() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.CENTER)); panel.setBorder(BorderFactory.createEtchedBorder()); panel.add(new JLabel("Name: ")); panel.add(new JTextField(30)); return(panel); }

Page 8: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

8

SWING - JTabbedPane

// create panel 2 private JPanel createPanel2() { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEtchedBorder()); panel.add("Center",new JTextArea()); return(panel); }

Page 9: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

9

SWING - JTabbedPane

// create panel 3 private JPanel createPanel3() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); panel.setBorder(BorderFactory.createEtchedBorder()); panel.add(new JCheckBox("Check if you are still in school")); panel.add(new JCheckBox("Check if you have no medical

problems")); return(panel); }

Page 10: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

10

SWING - JTabbedPane

// create main program public static void main(String[] args) { TabbedExample tabbedExample = new

TabbedExample(); tabbedExample.pack(); tabbedExample.setVisible(true); }

}

Page 11: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

11

SWING - JTabbedPane

Remove Tabs pane.removeTabAt(index)

Selecting pages pane.setSelectedIndex(index)

Adding images to tabs pane.addTab(“text”,icon,panel)

Enabling/Disabling tab pane.setEnabledAt(2,false)

Page selection changes can be detected by listening to ChangeEvents generated

Page 12: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

12

SWING - JScrollPane

Used when a panel is too big to be displayed on the screen

A typical example is a text editor where only a part of the text is displayed at a time. Scrollbars are used to scroll the text.

The developer is responsible for only populating the pane.

SWING manages all the repainting, resizing activities associated with the pane

Page 13: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

13

SWING - JScrollPane

An image being scrolled

Page 14: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

14

SWING - JScrollPane

package scrollexample;import java.awt.*;import javax.swing.*;

public class ScrollExample extends JFrame {

public ScrollExample() { // set the size of the frame setSize(600,400);

// create a JLabel component Icon image = new ImageIcon("cliff_ib.gif"); JLabel label = new JLabel(image);

Page 15: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

15

SWING - JScrollPane

// add the jlabel to the scroll pane JScrollPane pane = new JScrollPane(); pane.getViewport().add(label);

// add the scroll pane to the frame getContentPane().add("Center",pane); }

public static void main(String[] args) { ScrollExample scrollExample = new ScrollExample(); scrollExample.setVisible(true); }

}

Page 16: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

16

SWING - JScrollPane

Control scroll bars pane.setVerticalScrollBarPolicy(

ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); pane.setHorizontalScrollBarPolicy(

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

Control the size of scroll bars // changing the size of the scroll bars JScrollBar vBar = pane.getVerticalScrollBar(); Dimension dim = vBar.getSize(); dim.width = 5; vBar.setPreferredSize(dim);

Page 17: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

17

SWING - JScrollPane

Unit Increment Block Increment The increment size can be changed from

the default by using the Scrollable interface.

Scrollable interface Dimension getPreferredScrollableViewportSize() int getScrollableBlockIncrement() int getScrollableUnitIncrement() boolean getScrollableTracksViewportWidth() boolean getScrollableTracksViewportHeight()

Page 18: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

18

SWING - JScrollPane

class ScrollableImage extends JLabel implements Scrollable { public ScrollableImage(Icon icon) { super(icon); }

public Dimension getPreferredScrollableViewportSize() { return(new Dimension(300,300)); }

public int getScrollableBlockIncrement(Rectangle visibleRect ,int orientation,int direction) { return(20); }

Page 19: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

19

SWING - JScrollPane

public int getScrollableUnitIncrement(Rectangle visibleRect ,int orientation,int direction) { return(20); }

public boolean getScrollableTracksViewportWidth() { return(false); }

public boolean getScrollableTracksViewportHeight() { return(false); } }

Page 20: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

20

SWING - JSplitPane

View two or more pieces of information simultaneously

Resize any panel to view more or less data Orientation

left-to-right top-to-bottom

Text editors usually have split windows to create multiple copies of the same text file. This allows you to look at different parts of the

text document at the same time.

Page 21: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

21

SWING - JSplitPane

A window split into two panels

Vertical Split

Page 22: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

22

SWING - JSplitPane

package splitexample;import java.awt.*;import javax.swing.*;public class SplitExample extends JFrame { public SplitExample() {

// create a split pane JSplitPane splitpane

= new JSplitPane(JSplitPane.VERTICAL_SPLIT);

// create the panels JPanel panel1 = createPanel1(); JPanel panel2 = createPanel2();

Page 23: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

23

SWING - JSplitPane

// add to the split pane splitpane.setTopComponent(panel1); splitpane.setBottomComponent(panel2);

// add splitpane to the frame getContentPane().add(splitpane); }

public static void main(String[] args) { SplitExample splitExample = new SplitExample(); splitExample.pack(); splitExample.setVisible(true); }

Page 24: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

24

SWING - JSplitPane

// create panel 1 private JPanel createPanel1() { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEtchedBorder()); panel.add("Center",new JTextArea(5,30)); return(panel); }

Page 25: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

25

SWING - JSplitPane

// create panel 2 private JPanel createPanel2() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); panel.setBorder(BorderFactory.createEtchedBorder()); panel.add(new JCheckBox("Check if you are still in school")); panel.add(new JCheckBox("Check if you have no medical

problems")); return(panel); }

} // end

Page 26: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

26

SWING - JSplitPane

One split pane can be nested into another split pane for a more complex interface

Setting the divider size splitpane.setDividerSize(20)

A divider movement can be detected by using the AncestorListener Components of the split pane can implement

it

Page 27: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

27

SWING - JEditorPane

Extends from the JTextComponent class Ability to display any mime type Can easily create an HTML viewer for

online help Add internet capability to the

application

Page 28: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

28

SWING - JEditorPane

package editorpaneexample;import java.awt.*;import javax.swing.*;import javax.swing.event.*;import java.net.*;public class EditorPaneExample extends JFrame implements HyperlinkListener { private JEditorPane editorpane;

public EditorPaneExample() { try { setSize(600,600);

Page 29: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

29

SWING - JEditorPane

// create an editor pane pointing to a URL URL url = new URL("file:///D:/OLDui/index.html"); editorpane = new JEditorPane(url); editorpane.setEditable(false); editorpane.addHyperlinkListener(this);

// wrap the editor pane in a scroll pane JScrollPane scrollpane = new JScrollPane(); scrollpane.getViewport().add(editorpane,BorderLayout.CENTER);

// add the pane to the frame getContentPane().add(scrollpane,BorderLayout.CENTER); } catch (Exception exp) { System.out.println(exp.getMessage()); } }

Page 30: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

30

SWING - JEditorPane

public void hyperlinkUpdate(HyperlinkEvent event) { if (event.getEventType() ==

HyperlinkEvent.EventType.ACTIVATED) { // User clicked on a URL try { editorpane.setPage(event.getURL()); } catch (Exception exp) { System.out.println("Error :: " + exp.getMessage()); } } }

Page 31: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

31

SWING - JEditorPane

public static void main(String[] args) { EditorPaneExample editorPaneExample = new

EditorPaneExample(); editorPaneExample.setVisible(true); }

}

Swing handles RTF other than HTML Custom formats can be handled by

setting up custom editor kits

Page 32: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

32

SWING - Action

Synchronizing the action listeners for toolbars buttons, menu items and popup menu items is a nightmare

Synchronizing enabling/disabling is a problem

Actions are the solution to avoid synchronization and foster code reuse

An action object can be added to a toolbar, menu or a popup menu

Action object can be disabled/enabled

Page 33: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

33

SWING - Action

Action class should extend from AbstractAction

public class XAction extends AbstractAction {public XAction(String name,ImageIcon image) {

super(name,image);}

public void actionPerformed(ActionEvent event) {// Handle action event for X here

}}

Page 34: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

34

SWING - Action

Shows the disabled actions

Page 35: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

35

SWING - Action

package actionexample;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class ActionExample extends JFrame { private JPopupMenu popup = new JPopupMenu();

// Handle right clicks public class MouseHandler extends MouseAdapter { public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(),e.getX(),e.getY()); } } }

Page 36: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

36

SWING - Action

// Action class for 'New' class NewAction extends AbstractAction { NewAction(String name,ImageIcon icon) { super(name,icon);

}

public void actionPerformed(ActionEvent e) { // handle new action } }

Page 37: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

37

SWING - Action

// Action class for 'Save' class SaveAction extends AbstractAction { SaveAction(String name,ImageIcon icon) { super(name,icon);

}

public void actionPerformed(ActionEvent e) { // handle save action } }

Page 38: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

38

SWING - Action

// Action class for 'SaveAll' class SaveAllAction extends AbstractAction { SaveAllAction(String name,ImageIcon icon) { super(name,icon);

}

public void actionPerformed(ActionEvent e) { // handle saveAll action } }

Page 39: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

39

SWING - Action

public ActionExample() { setSize(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Action newAction = new NewAction("Open",new ImageIcon("open.gif"));

Action saveAction = new SaveAction("Save",new ImageIcon("save.gif"));

Action saveallAction = new SaveAllAction("Save All",new ImageIcon("saveall.gif"));

// create a menu bar and file menu JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("File");

Page 40: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

40

SWING - Action

fileMenu.add(newAction); fileMenu.add(saveAction); fileMenu.add(saveallAction); menubar.add(fileMenu); setJMenuBar(menubar);

// create a tool bar JToolBar tool = new JToolBar(); tool.add(newAction); tool.add(saveAction); tool.add(saveallAction);

getContentPane().add("North",tool);

Add actions toMenu bar

Add actions toTool bar

Page 41: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

41

SWING - Action

// add to popup menu popup.add(newAction); popup.add(saveAction); popup.add(saveallAction);

// disable saveall action and save action saveAction.setEnabled(false); saveallAction.setEnabled(false);

getContentPane().add("Center",new JPanel()); getContentPane().addMouseListener(new MouseHandler()); }

Add actions toPopup menu item

Page 42: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

42

SWING - Action

public static void main(String[] args) {

ActionExample actionExample = new ActionExample();

// pack and show actionExample.setVisible(true); }

}

Page 43: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

43

SWING - JDialog

Secondary application window Almost like a JFrame

But supports modal operation. Extends from the Dialog class in AWT

Page 44: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

44

SWING - JDialog

A JDialog window

Page 45: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

45

SWING - JDialog

package dialogexample;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class DialogExample extends JFrame implements ActionListener { public DialogExample() { // exit the frame on close this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create a JButton and add it to the content pane JButton jbutton = new JButton("Open Dialog"); jbutton.addActionListener(this); getContentPane().add(jbutton); }

Page 46: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

46

SWING - JDialog

public void actionPerformed(ActionEvent e) { // when the button is clicked, control comes here TestDialog dialog = new TestDialog(this,"Test Dialog",true);

dialog.pack(); dialog.setVisible(true); }

public static void main(String[] args) { DialogExample dialogExample = new DialogExample();

dialogExample.pack(); dialogExample.setVisible(true); }}

Page 47: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

47

SWING - JDialog

package dialogexample;import java.awt.*;import javax.swing.*;public class TestDialog extends JDialog {

public TestDialog(Frame frame, String title, boolean modal) { super(frame, title, modal);

// dispose the dialog on close

this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

// create a button panel JPanel buttons = new JPanel();

Page 48: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

48

SWING - JDialog

buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5)); buttons.add(new JButton("Close"));

// add the buttons JPanel areaPanel = new JPanel(); JTextArea area = new JTextArea(5,40); areaPanel.add(area); areaPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(5,5,5,5))); getContentPane().add("Center",areaPanel); getContentPane().add("South",buttons); }}

Page 49: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

49

SWING - JOptionPane

Simplifies the task of displaying quick information to the user

Messages can be in the form of Error Message Information message Warning message Question message Plain message

Can show confirm dialog

Page 50: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

50

SWING - JOptionPane

JOptionPane option = new JOptionPane();

option.showMessageDialog(this,"This is an Error" ,"Error",JOptionPane.ERROR_MESSAGE);

Page 51: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

51

SWING - JOptionPane

JOptionPane option = new JOptionPane();

option.showMessageDialog(this,"This is an Warning"

,"Warning",JOptionPane.WARNING_MESSAGE);

Page 52: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

52

SWING - JOptionPane

JOptionPane option = new JOptionPane();

int uoption = option.showConfirmDialog(this,"Is this a Question?"

,"Question",JOptionPane.YES_NO_OPTION ,JOptionPane.QUESTION_MESSAGE,null); if (uoption == JOptionPane.YES_OPTION) { …………….}

Page 53: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

53

SWING - JColorChooser

Platform independent way of choosing color

Simplifies the user interface for color selection

Can be Used as an independent dialog for choosing

a color Embedded in a frame which contains other

panel

Page 54: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

54

SWING - JColorChooser

Color color = JColorChooser.showDialog(this,"Choose a Color",Color.blue);

// Act on the color chosen by the user

Page 55: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

55

SWING - JFileChooser

Platform independent way of providing a file selection mechanism

Allows filtering of files

JFileChooser chooser = new JFileChooser(); int value = chooser.showOpenDialog(this); if (value == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); // handle file selection }

Page 56: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

56

SWING - JFileChooser

A file chooser

Page 57: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

57

SWING - JInternalFrame

Allows the capability to embed frames within frames

The internal frames can move within the bounds of the parent frame

Can be maximized,minimized or restored

Also called the MDI (multiple document interface)

Page 58: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

58

SWING - JInternalFrame

Page 59: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

59

SWING - JInternalFrame

package internalexample;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class InternalExample extends JFrame implements ActionListener { private JDesktopPane deskPane; public InternalExample() { setSize(500,500); // create a tool bar JToolBar toolbar = new JToolBar(); JButton newButton = new JButton("Create"); newButton.addActionListener(this); toolbar.add(newButton);

Page 60: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

60

SWING - JInternalFrame

// create a desktop pane deskPane = new JDesktopPane(); getContentPane().add("Center",deskPane); getContentPane().add("North",toolbar); } public void actionPerformed(ActionEvent e) { TestInternalFrame frame = new TestInternalFrame(); frame.setVisible(true); deskPane.add(frame); }

Page 61: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

61

SWING - JInternalFrame

public static void main(String[] args) { InternalExample internalExample = new

InternalExample(); internalExample.setVisible(true); }

} // end Internal Example

Page 62: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

62

SWING - JInternalFrame

package internalexample;import java.awt.*;import javax.swing.*;

public class TestInternalFrame extends JInternalFrame { public TestInternalFrame() { super("Internal Frame");

// set the frame properties setClosable(true); setMaximizable(true); setIconifiable(true); setResizable(true);

Page 63: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

63

SWING - JInternalFrame

// set the size and add test area setSize(400,400); getContentPane().add("Center",new JTextArea()); }

} // end TestInternalFrame

Other methods moveToFront() moveToBack() setMenuBar(JMenuBar menubar) addInternalFrameListener(InternalFrameListener listener)

Page 64: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

64

SWING – Exercise 3

Page 65: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

65

SWING – Exercise

Create a UI layout as shown in the previous page. When the frame is first shown the label at the bottom should say “File not selected”. When the user clicks on button and chooses a file or directory, the label text should change to the full absolute path of the file.

When the window is closed, the application should exit.

Page 66: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

66

SWING - JList

Collection of items from which user can make one or more selection

Has the ability to provide graphics inside the list

Page 67: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

67

SWING - JList

Jlist accepts String[], Vector or ListModel

String[] items = new String[] {

"item1","item2","item3","item4" ,"item5","item6","item7","item8" ,"item9","item10" }; JList list = new JList(items); list.setVisibleRowCount(5); getContentPane().add("Center",new JScrollPane(list));

Page 68: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

68

SWING - JList

// A Custom List Model

class TestModel extends AbstractListModel {

public int getSize() { return(10); }

public Object getElementAt(int index) { return "item" + index; } }

Page 69: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

69

SWING - JList

Page 70: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

70

SWING - JList

package listexample;import java.awt.*;import javax.swing.*;

public class ListExample extends JFrame {

private String[] items =

{"Open","Save","SaveAll","Zoom","Zoomin","Zoomout"}; private String[] images =

{"open.gif","save.gif","saveall.gif","zoom.gif" ,"zoomin.gif","zoomout.gif"};

Page 71: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

71

SWING - JList

// Custom model class TestModel extends AbstractListModel { public int getSize() { return(items.length); }

public Object getElementAt(int index) { return(items[index]); } } // end TestModel

Page 72: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

72

SWING - JList

// Custom cell renderer class CustomCellRenderer extends JLabel implements ListCellRenderer { public CustomCellRenderer() { setOpaque(true); }

public Component getListCellRendererComponent(JList list ,Object value ,int index,boolean isSelected,boolean cellHasFocus) { this.setText(value.toString()); this.setIcon(new ImageIcon(images[index])); if (isSelected) { this.setBackground(Color.blue); this.setForeground(Color.white); }

Page 73: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

73

SWING - JList

else { this.setBackground(Color.white); this.setForeground(Color.black); } return this; }

} // end CustomCellRenderer

public ListExample() { setSize(200,200); JList list = new JList(new TestModel()); list.setCellRenderer(new CustomCellRenderer()); getContentPane().add("Center",new JScrollPane(list)); }

Page 74: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

74

SWING - JList

public static void main(String[] args) { ListExample listExample = new ListExample(); listExample.setVisible(true); }}

Page 75: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

75

SWING - JList

Listening to list activity addListSelectionListener ListSelectionEvent

Any component can be rendered inside the list box. JButton JTextField JCheckBox etc

Page 76: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

76

SWING - JTable

Simple two dimensional display Supports custom data models Supports custom cell rendering

Render any component inside the cell Support custom header rendering Highly flexible component

Can be customized by the programmer Class is called JTable

Page 77: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

77

SWING - JTable

A JTable which shows information about people

Page 78: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

78

SWING - JTable

package simpletable;import java.awt.*;import javax.swing.*;public class SimpleTableExample extends JFrame { public SimpleTableExample() { setSize(600,300);

// create the columns and the values String[] columns = {"Name","Telephone","City","Company"}; String values[][] = { {"Mr. X","703-4228989","Herndon","Bell Atlantic"}, {"Mr. Z","301-6748989","Rockville","Artesia Tech"}, {"Mr. W","703-4258999","Herndon","Intersect Soft"}, {"Mr. A","703-7864456","Herndon","Intelsat"} };

Page 79: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

79

SWING - JTable

JTable table = new JTable(values,columns); JScrollPane pane = new JScrollPane(table);

// add the table to the frame getContentPane().add(pane); }

public static void main(String[] args) { SimpleTableExample simpleTableExample = new SimpleTableExample(); simpleTableExample.setVisible(true); }

} // end program

Page 80: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

80

SWING - JTable

JTable uses the DefaultDataModel class as the model by default

A custom data model can be created for the table by extending AbstractTableModel

Page 81: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

81

SWING - JTable

Using a custom table model

Page 82: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

82

SWING - JTable

package simpletable;import java.awt.*;import javax.swing.*;import javax.swing.table.*;public class SimpleTableExample extends JFrame { public SimpleTableExample() { // set the size of the frame setSize(600,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTable table = new JTable(new SimpleTableModel()); table.setShowHorizontalLines(false); JScrollPane pane = new JScrollPane(table); pane.getViewport().setBackground(Color.white);

Set the tableBackground to white

Page 83: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

83

SWING - JTable

// add the table to the frame

getContentPane().add(pane); }

public static void main(String[] args) { SimpleTableExample simpleTableExample

= new SimpleTableExample(); simpleTableExample.setVisible(true); }

}

Page 84: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

84

SWING - JTable

package simpletable;import javax.swing.table.*;class SimpleTableModel extends AbstractTableModel {

// create the columns and the values private String[] columns =

{"Name","Telephone","City","Company"}; private String values[][] = { {"Mr. X","703-4228989","Herndon","Bell Atlantic"}, {"Mr. Z","301-6748989","Rockville","Artesia Tech"}, {"Mr. W","703-4258999","Herndon","Intersect Software"}, {"Mr. A","703-7864456","Herndon","Intelsat"} };

Page 85: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

85

SWING - JTable

// Return object at (row,col) public Object getValueAt(int row,int col) { return(values[row][col]); }

// Set object at (row,col) public void setValueAt(Object value,int row,int col) { // do nothing }

// Return the column count public int getColumnCount() { return(columns.length); }

Page 86: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

86

SWING - JTable

// Return the row count

public int getRowCount() { return(values.length); }

// Return the column name at col public String getColumnName(int col) { return(columns[col]); }

} // end model

Page 87: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

87

SWING - JTable

A JTable in which the second column is right justified with yellow background

Page 88: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

88

SWING - JTable

package simpletable;import java.awt.*;import javax.swing.*;import javax.swing.table.*;

public class TelephoneRenderer extends JLabel implements TableCellRenderer {

public TelephoneRenderer() { setOpaque(true); }

Page 89: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

89

SWING - JTable

public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {

this.setHorizontalAlignment(JLabel.RIGHT); this.setText((String)value); this.setBackground(Color.yellow); return(this); }

} // end renderer

Return the label

Page 90: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

90

SWING - JTable

Associating a cell renderer to a column

TableColumn column = table.getColumn("Telephone");column.setCellRenderer(new TelephoneRenderer());

Table header rendering can be achieved by

column.setHeaderRenderer(<renderer>)

Page 91: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

91

SWING - JTable

Table selection events can be observed by implementing the ListSelectionListener interface.

ListSelectionEvent is generated whenever the selections change.

table.getSelectionModel().addListSelectionListener(this);

Page 92: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

92

SWING - JTable

public void valueChanged(ListSelectionEvent event) {

if (event.getSource() == table.getSelectionModel() && !event.getValueIsAdjusting()) { int row = table.getSelectedRow(); if (row >= 0) { SimpleTableModel model = (SimpleTableModel)table.getModel(); String name = (String)model.getValueAt(row,0);

JOptionPane pane = new JOptionPane(); pane.showMessageDialog(this,"Selected : " + name); } } }

Page 93: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

93

SWING - JTree

Presents hierarchical information Entirely made up of nodes. Every node has a parent except for the

root node. Supports

custom models custom rendering

Class is called JTree

Page 94: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

94

SWING - JTree

Uses the default tree model to create the tree

Page 95: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

95

SWING - JTree

package treeexample;import java.awt.*;import javax.swing.*;import javax.swing.tree.*;public class TreeExample extends JFrame { private JTree jtree;

public TreeExample() { // set the size setSize(400,400);

// root node DefaultMutableTreeNode filenode

= new DefaultMutableTreeNode("TreeExample.java");

Page 96: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

96

SWING - JTree

// immediate children DefaultMutableTreeNode importnode

= new DefaultMutableTreeNode("Imports"); DefaultMutableTreeNode datanode

= new DefaultMutableTreeNode("Variables"); DefaultMutableTreeNode methodnode

= new DefaultMutableTreeNode("Methods"); filenode.add(importnode); filenode.add(datanode); filenode.add(methodnode);

// import children DefaultMutableTreeNode awt

= new DefaultMutableTreeNode("java.awt.*");

Page 97: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

97

SWING - JTree

DefaultMutableTreeNode swing = new DefaultMutableTreeNode("javax.swing.*");

DefaultMutableTreeNode swingtree = new DefaultMutableTreeNode("javax.swing.tree.*");

importnode.add(awt); importnode.add(swing); importnode.add(swingtree);

// data children DefaultMutableTreeNode variable

= new DefaultMutableTreeNode("JTree jtree"); datanode.add(variable);

Page 98: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

98

SWING - JTree

// method children DefaultMutableTreeNode method1

= new DefaultMutableTreeNode("JTree()"); DefaultMutableTreeNode method2

= new DefaultMutableTreeNode("main()"); methodnode.add(method1); methodnode.add(method2);

// create the tree from the default model DefaultTreeModel model = new DefaultTreeModel(filenode); jtree = new JTree(model);

getContentPane().add("Center",new JScrollPane(jtree));

}

Page 99: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

99

SWING - JTree

public static void main(String args[]) { TreeExample treeexample = new TreeExample(); treeexample.setVisible(true); }

}// end program

Page 100: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

100

SWING - JTree

Custom data models can be used with the JTree

Extends from DefaultTreeModel Object getChild(Object parent,int index) int getChildCount(Object parent) boolean isLeaf(Object node)

Each DefaultMutableTreeNode contains a user object which can store any object in the node

Page 101: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

101

SWING - JTree

package customtreeexample;public class JavaFile { private String name; private String[] imports; private String[] variables; private String[] methods;

public JavaFile() { name = "CustomTreeExample.java"; imports

= new String[] {"java.awt.*","javax.swing.*","javax.swing.tree.*"};

variables = new String[] {"JTree jtree"}; methods = new String[] {"CustomTreeExample()","main()"}; }

Hardcoded values

Page 102: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

102

SWING - JTree

public String getName() { return name; }

public String[] getSubTypes() { return(new String[] {"Imports","Data","Methods"}); }

public String[] getImports() { return(imports); }

public String[] getVariables() { return(variables); }

Page 103: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

103

SWING - JTree

public String[] getMethods() { return(methods); }

} // end JavaFile

Page 104: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

104

SWING - JTree

package customtreeexample;import javax.swing.tree.*;public class SimpleTreeModel extends DefaultTreeModel { private JavaFile file; private DefaultMutableTreeNode root; private String rootName;

public SimpleTreeModel(DefaultMutableTreeNode node,JavaFile file) {

super(node); this.file = file; this.rootName = file.getName(); this.root = node; }

Page 105: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

105

SWING - JTree

// get the child at the specified index public Object getChild(Object parent,int index) { DefaultMutableTreeNode parentNode

= (DefaultMutableTreeNode)parent; String userObject = (String)parentNode.getUserObject(); if (userObject.equals(rootName)) { String[] types = file.getSubTypes(); DefaultMutableTreeNode node

= new DefaultMutableTreeNode(types[index]); return(node); }

Page 106: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

106

SWING - JTree

else if (userObject.equals("Imports")) { String[] imports = file.getImports(); DefaultMutableTreeNode node

= new DefaultMutableTreeNode(imports[index]); return(node); } else if (userObject.equals("Data")) { String[] variables = file.getVariables(); DefaultMutableTreeNode node

= new DefaultMutableTreeNode(variables[index]); return(node); }

Page 107: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

107

SWING - JTree

else if (userObject.equals("Methods")) { String[] methods = file.getMethods(); DefaultMutableTreeNode node

= new DefaultMutableTreeNode(methods[index]);

return(node); }

return(null);

} // end getChild()

Page 108: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

108

SWING - JTree

// get the child count for the parent public int getChildCount(Object parent) { DefaultMutableTreeNode parentNode

= (DefaultMutableTreeNode)parent; String userObject = (String)parentNode.getUserObject(); if (userObject.equals(rootName)) { return(file.getSubTypes().length); } else if (userObject.equals("Imports")) { return(file.getImports().length); }

Page 109: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

109

SWING - JTree

else if (userObject.equals("Data")) { return(file.getVariables().length); } else if (userObject.equals("Methods")) { return(file.getMethods().length); }

return(0);

} // end getChildCount()

Page 110: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

110

SWING - JTree

// return TRUE if the node is a leaf public boolean isLeaf(Object node) { DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)node; String userObject = (String)parentNode.getUserObject(); if (userObject.equals(rootName) || userObject.equals("Imports") || userObject.equals("Data") || userObject.equals("Methods")) { return(false); } return(true); }

} // end SimpleTreeModel

Page 111: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

111

SWING - JTree

Custom Rendering Useful for changing icons, fonts etc Implement TreeCellRenderer class Component

getTreeCellRendererComponent(…) must be implemented

Nodes can contain any Component (TextField, Checkbox etc)

Listening to tree selections Implement TreeSelectionListener

Page 112: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

112

SWING - JTree

Listening for Tree expansions Implement TreeExpansionListener

Hiding root node tree.setRootVisible(false);

Other methods tree.expandPath(path) tree.collapsePath(path) tree.setSelectionPath(path) tree.clearSelection()

Page 113: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

113

SWING – Exercise 4

Page 114: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

114

SWING – Exercise 4

Add a JTable to the center panel which shows the name of the file/directory and the size as columns. Whenever a file or directory is chosen it should update the table.

What mechanism would you use to make the size column right justified?

Page 115: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

115

SWING – Look And Feel

Supports different look and feel Metal Motif Windows Mac

MVC pattern Can create corporate look and feel Change the look And Feel

UIManager.setLookAndFeel(“<class name>”);

Page 116: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

116

SWING – Look And Feel

Add this code to the tree example

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.

WindowsLookAndFeel");

Page 117: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

117

SWING - Multithreading

Threads can improve performance by allowing simultaneous execution of two or more related tasks

Event Dispatching thread Event Dispatching thread and data collection

threads can be separate but needs synchronization

For performance Once a Swing component is displayed on the screen

they cannot be changed from any thread other than the event dispatching thread

Inherently Multi-thread unsafe

Page 118: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

118

SWING - Multithreading

invokeLater Queues the GUI request. The event

dispatching thread will pick it up and execute it at some later time

invokeAndWait Queues and waits till the task is performed.

DO NOT CALL THIS FROM THE EVENT DISPATCHING THREAD

Page 119: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

119

SWING - Multithreading

class RunnableClass implements Runnable {public void run() {

// do gui work here}

}

// will queue the request for the event dispatching thread// to executeSwingUtilities.invokeLater(new RunnableClass());

Page 120: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

120

SWING Exercise 5

Change the code for exercise 4 so that the output is in windows look and feel

Page 121: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

121

SWING – Popular IDEs

Integrated Development Environments Borland JBuilder (most popular)

http://www.borland.com/jbuilder/index.html WebGain Visual Café

http://www.webgain.com/products/visual_cafe/ IBM VisualAge For Java

http://www-3.ibm.com/software/ad/vajava/ Sun[tm] ONE Studio 4

http://wwws.sun.com/software/sundev/jde/index.html

Page 122: Copyright 2003 Mudra Services1 SWING - JTabbedPane Helps stack pages of information into a single point of reference If information cannot be laid out

Copyright 2003 Mudra Services

122

What’s Next Threads

Go from single threaded applications to multithreaded applications

Networking Go from one user applications to muti user

applications Distributed Objects

Go from objects in single JVM to objects in multiple JVMs (maybe in different machines)