1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

41
1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives

Transcript of 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

Page 1: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

1

1. Generic (JComponent) services2. Atomic Swing components3. Containers4. Drawing primitives

Page 2: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

2

Component ServicesAll (?) Swing-components, except top-level containers, inherit from JComponent, Container and Component

Main services

Customizing Component Appearance

Setting Component State

• get/set foreground/background colour• get/set font• get/set border• specify/get opaqueness

• setting tooltips• (in)activate component• control visibility

Page 3: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

3

Component Services

Handling Events

Painting Components

Containment Hierarchy

• (de)registration of event listeners• check if pixel is part of component• check to which component pixel belongs

• repaint() : request to repaint the components• revalidate() : request to lay out again component• paintComponent() : overridden method for custom drawing

• add/remove components from container• get contained components in container

Page 4: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

4

Component Services

Laying Out Components• get/set preferred, maximum and minimum sizes• get set alignment mode• get/set layoutmanager

Page 5: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

5

Examples :Setting Tooltips

JComponent :void setToolTipText(String)

class ToolTip extends JFrame {JButton change = new JButton("Switch");ToolTip(String title) {

super(title);Container cp=getContentPane();cp.setLayout(new FlowLayout());change.setToolTipText("Press to switch");cp.add(change);

}}

public class TooltipTest { public static void main(String[] args) { ToolTip app=new ToolTip("Setting tooltips"); app.setSize(200,100); app.setVisible(true);

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Page 6: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

6

Examples :Controlling Look and Feel

Look and feel options• swing : default

or : getCrossPlatformLookAndFeelClassName()• motif : “com.sun.java.swing.plaf.motif.MotifLookAndFeel”• system : getSystemLookAndFeelClassName()

Format try {

UIManager.setLookAndFeel( ……);} catch(Exception e) { }

• before instantiating visible components• don’t change L&F during program execution ! (possible but difficult)

Page 7: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

7

Examples :Controlling Look and Feel

class LF extends JFrame {JButton change = new JButton("Switch");LF(String title) {

super(title);Container cp=getContentPane();cp.setLayout(new FlowLayout());change.setToolTipText("Press to switch");cp.add(change);

}} public class LookAndFeelTest {

public static void main(String[] args) { try { // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } catch(Exception e) { } LF app=new LF("Trying different L&F's"); app.setSize(200,100); app.setVisible(true); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Page 8: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

8

Examples :Adding Borders

Types of borders• TitledBorder• EtchedBorder• EmptyBorder• BevelBorder• LineBorder• SoftBevelBorder• MatteBorder• CompoundBorder

How ?• Construct a Border-object• invoke setBorder(…) on the relevant JComponent

Inherit from AbstractBorder,Implementing the Border interface

import javax.swing.border.*;

Page 9: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

9

Examples :Adding Borders

class BorderT extends JFrame {private static int num=5;private JButton[] b=new JButton[num];public BorderT(String title) {

super(title);for(int i=0;i<b.length;i++) {

b[i]=new JButton("Button "+i);}b[1].setBorder(new TitledBorder("Border Title"));b[2].setBorder(new EtchedBorder());b[3].setBorder(new LineBorder(Color.red));b[4].setBorder(new BevelBorder(BevelBorder.RAISED));getContentPane().setLayout(new FlowLayout());for(int i=0;i<b.length;i++)

getContentPane().add(b[i]);

}} // End of class BorderT

Page 10: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

10

Binary choices

No state

State

JButton

JToggleButtonJRadioButtonJCheckBox

Page 11: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

11

Multivalued choices

>1 alternative

1 alternative at most

• JList• list of buttons• JMenu

• JList (after setting seletion mode)• JComboBox• list of buttons added to ButtonGroup• JMenu (JRadioButtonMenuItem, JCheckBoxMenuItem)

Page 12: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

12

Multivalued choices

JList JMenu

JComboBox (non-editable) JComboBox (editable)

Page 13: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

13

Multivalued choices

“continuous” value

JSlider

Fires ChangeEvents when value changes

Page 14: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

14

Multivalued choicesclass SliderT extends JFrame {

private JSlider s=new JSlider(0,100,70);private JLabel output=new JLabel("Value = 70");public SliderT(String title) {

super(title);Container cp=getContentPane();s.setPaintTicks(true);s.setMinorTickSpacing(5);s.setMajorTickSpacing(20);cp.setLayout(new GridLayout(2,1));cp.add(s);cp.add(output);ChangeListener c=new ChangeListener() {

public void stateChanged(ChangeEvent e) {output.setText("Value = "+s.getValue());

}};s.addChangeListener(c);

}} // End of class SliderT

Page 15: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

15

Text

1 line

• JTextField

>= 1 line

• JTextArea (plain text) • JEditorPane (styled text)• JTextPane (styled text)

Page 16: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

16

Text

JTextArea

JTextPane

• several styles• embedded pictures• displays HTML

[Sun, Swing Tutorial]

Page 17: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

17

Output only ...

• JLabel• JProgressBar

• Tool tips

Page 18: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

18

Output only ...class ProgressT extends JFrame {

private JProgressBar pb=new JProgressBar(0,20);private JButton start=new JButton("(Re)Start");private JButton next=new JButton("Next");private int count=0;public ProgressT(String title) {

super(title);Container cp=getContentPane();cp.setLayout(new GridLayout(3,1));cp.add(pb);cp.add(next);cp.add(start);pb.setStringPainted(true);next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){

pb.setValue(++count);if(count==20) next.setEnabled(false); }});start.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

count=0;pb.setValue(0);next.setEnabled(true); }});

}} // End of class ProgressT

Page 19: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

19

Formatted Information

• JColorChooser

• getColor returns color choosen

• class can createdialog

Page 20: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

20

Formatted Information

• JColorChooser

class ColorT extends JFrame {private JColorChooser cc=new JColorChooser();public ColorT(String title) {

super(title);Container cp=getContentPane();cp.add(cc,BorderLayout.CENTER);

}} // End of class ColorT

Page 21: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

21

Formatted Information

• JFileChooser

• JTable • JTree

[Sun, Swing Tutorial]

Page 22: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

22

Intermediate Containers

• JPanel

• used to group components• behavior inherited from Container

• LayoutManager• methods to add/remove Jcomponents

• invisible by default

Page 23: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

23

Intermediate Containers class PanelT extends JFrame {

private JPanel left=new JPanel();private JPanel right=new JPanel();public PanelT(String title) {

super(title);Container cp=getContentPane();cp.setLayout(new GridLayout(1,2));left.setLayout(new GridLayout(3,1));right.setLayout(new GridLayout(3,1));left.setBorder(new TitledBorder("Left Panel"));right.setBorder(new TitledBorder("Right Panel"));

left.add(new JButton("A"));left.add(new JSlider(0,100,75));left.add(new JCheckBox("Check me ..."));

cp.add(left);right.add(new JToggleButton("B"));right.add(new JProgressBar(0,100));right.add(new JRadioButton("Click me ..."));

cp.add(right);}

} // End of class PanelT

Page 24: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

24

Intermediate Containers

• JScrollPane

Adds scroll bars (if needed) to any JComponent

…JScrollPane scrollLeft=new JScrollPane(left);cp.add(scrollLeft);...

Page 25: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

25

Intermediate Containers

• JTabbedPane

Used to realize hierarchy andsave space

Page 26: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

26

Intermediate Containersclass TabbedPaneT extends JFrame {

private JPanel left=new JPanel();private JPanel right=new JPanel();public TabbedPaneT(String title) {

super(title);Container cp=getContentPane();JTabbedPane tp=new JTabbedPane();

left.setLayout(new GridLayout(3,1));right.setLayout(new GridLayout(3,1));left.add(new JButton("A"));left.add(new JSlider(0,100,75));left.add(new JCheckBox("Check me ..."));right.add(new JToggleButton("B"));right.add(new JProgressBar(0,100));right.add(new JRadioButton("Click me ..."));

tp.addTab("Left",left);tp.addTab("Right",right);cp.add(tp);

}} // End of class TabbedPaneT

Page 27: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

27

Intermediate Containers

• JSplitPane

Used to divide area in 2, allowing splittingratio to vary

Page 28: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

28

Intermediate Containersclass SplitPaneT extends JFrame {

private JPanel left=new JPanel();private JPanel right=new JPanel();public SplitPaneT(String title) {

super(title);Container cp=getContentPane();

left.setLayout(new GridLayout(3,1));right.setLayout(new GridLayout(3,1));left.add(new JButton("A"));left.add(new JSlider(0,100,75));left.add(new JCheckBox("Check me ..."));right.add(new JToggleButton("B"));right.add(new JProgressBar(0,100));right.add(new JRadioButton("Click me ..."));JSplitPane sp=new JSplitPane(

JSplitPane.HORIZONTAL_SPLIT,left,right);sp.setOneTouchExpandable(true);

cp.add(sp);}

} // End of class SplitPaneT

Page 29: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

29

Intermediate Containers

• JToolBarGraphical presentation of menuIcon based

[Sun, Swing Tutorial]

Page 30: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

30

Top-level Containers

• JFrame-> Base class for stand alone applications

• JApplet-> Base class for applets

• JDialog-> Can pop up from other Container-> no exit when Dialog closes

Can have menu bars

Page 31: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

31

Concept

• every JComponent has a paintComponent method responsible of drawing the component on the screen• paintComponent is automatically called by container object

In practice(1) define a class extending JPanel(2) define a drawing method with signature

public void paintComponent(Graphics g) {super.paintComponent(g);// drawing operations

}(3) make an object of this class, and add it to some container

Page 32: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

32

Graphical Context

“physical” area where component can be painted

(0,0)

(0,200) (500,200)

(500,0)

pixel (“picture element”)

Page 33: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

33

Drawing Primitives

Pen Color

Operations

• public void setColor(Color c) • public Color getColor()

(x1,y1)

(x2,y2)

g.drawLine(x1,y1,x2,y2);

g.setColor(Color.green);

• public abstract void drawLine(int x1, int y1, int x2, int y2)

Graphics object

Lines

Page 34: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

34

Drawing Primitives

Rectangles

(x1,y1)b

h

g.drawRect(x1,y1,b,h);

g.fillRect(x1,y1,b,h);

• public abstract void drawRect(int x1, int y1, int b, int h)• public abstract void fillRect(int x1, int y1, int b, int h)

Page 35: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

35

Drawing Primitives

Ovals

(x1,y1)b

h

g.drawOval(x1,y1,b,h);

g.fillOval(x1,y1,b,h);

• public abstract void drawOval(int x1, int y1, int b, int h)• public abstract void fillOval(int x1, int y1, int b, int h)

Page 36: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

36

Drawing Primitives

Arcs

(x1,y1) b

h

g.drawArc(x1,y1,b,h,s,a);

g.fillArc(x1,y1,b,h,s,a);

• public abstract void drawArc(int x1, int y1, int b, int h,int s, int a)• public abstract void fillArc(int x1, int y1, int b, int h,int s, int a)

sa

Page 37: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

37

Drawing PrimitivesPolygons

(x[0],y[0])(x[2],y[2])

(x[1],y[1])

(x[3],y[3])g.drawPolyline(x,y,4);

g.drawPolygon(x,y,4);

g.fillPolygon(x,y,4);

• public abstract void drawPolyline(int[] x, int[] y, int n)• public abstract void drawPolygon(int[] x, int[] y, int n)• public abstract void fillPPolygon(int[] x, int[]y, int n)

Page 38: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

38

Example class MyDrawing extends JPanel {

public void paintComponent(Graphics g) {super.paintComponent(g);g.setColor(Color.red);g.drawLine(0,0,100,100);g.setColor(Color.blue);g.drawOval(50,50,150,150);

}} // End of class MyDrawing

class DrawingT extends JFrame {private MyDrawing d=new MyDrawing();public DrawingT(String title) {

super(title);Container cp=getContentPane();cp.add(d);

}} // End of class DrawingT

Page 39: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

39

Drawing Primitives

Text

Font • public void setFont(Font f) • public Font getFont()

Draw

(x,y)

g.drawString(“Message”,x,y);

Message

• public abstract void drawString(String s, int x, int y)

Page 40: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

40

Specifying FontsFont characteristics

type

style

size

Options :• Dialog• DialogInput• Monospaced• Serif• SansSerif• Symbol

PLAINBOLDITALIC

Page 41: 1 1. Generic (JComponent) services 2. Atomic Swing components 3. Containers 4. Drawing primitives.

41

Specifying Fonts

Constructor

• public Font(String name, int style, int size)

Examples

Font f1 = new Font(“Dialog”,Font.PLAIN,10);Font f2 = new Font(“Serif”, Font.BOLD, 14);Font f3 = new Font(“Dialog”,Font.BOLD||Font.ITALIC, 20);