44
Swing! Components and Images

Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Embed Size (px)

Citation preview

Page 1: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Swing! Components and Images

Page 2: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Example Swing Components (also called “widgets”)

Graphic from sun.com

Page 3: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Swing• Swing is a text component package

• Enhancement of the AWT package– + Swing has extra functionality (tooltips, double buffering

keyboard shortcuts, etc.)– + You can make Swing components look the same on all

platforms, whereas awt components will vary on different platforms

– - Swing is bigger and more complicated– - Swing is not supported by earlier JVMs– - Awt components tend to be faster than Swing components

• Most class names begin with the capital letter J– JLabel, JButton, JCheckbox, JRadioButton, etc.

• Need to import javax.swing.*;

Page 4: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JLabel• Declare a JLabel

JLabel label;

• Create a JLabel multiple ways:

– With just text: label = new JLabel( “text” );

OR label = new JLabel( “text”, alignment );

– With just an image (image inside an ImageIcon object – see later notes)JLabel label = new JLabel( ImageIcon );

– With text and imageJLabel label = new JLabel( “text”, ImageIcon, alignment );

• where alignment is either – JLabel.LEFT

– JLabel.CENTER

– JLabel.RIGHT

alignment becomes important when we work with layout managers, but the effect won’t be apparent right now.

Page 5: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JLabel Example

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

public class JLabelEx extends JApplet{ JLabel mylabel; public void init( ) { setLayout( new FlowLayout( ) );

mylabel = new JLabel( "Lots of text can go on one line" ); add( mylabel ); }}

Page 6: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JLabel across multiple lines

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

public class JLabelExMultiLine extends JApplet{ JLabel mylabel; public void init( ) { setLayout( new FlowLayout( ) );

mylabel = new JLabel( "<HTML>Lots of text<P>on<P>separate lines" ); add( mylabel ); }}

Page 7: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JLabel with funky text

• Lots of flexibility, but need to use HTML tags

• HTML tags are descriptions of how the text should be displayed

• Each tag is enclosed in brackets < and >

• To use these tags, you need to customize the JLabel string with the <HTML> tag

Page 8: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JLabel Example with HTML tagsimport javax.swing.*;import java.awt.*;

public class JLabelExWithHTML extends JApplet{

JLabel label; public void init( ) { setLayout( new FlowLayout( ) );

label = new JLabel( "<HTML>Hi <FONT SIZE=+4 COLOR=RED>there </FONT> <P>world" ); add ( label ); }}

Page 9: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Images

Image

ImageIcon

JLabel

Page 10: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Images

• Java can handle the following image types– .JPG– .GIF– .PNG

Page 11: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

getCodeBase( ) Images

• Add images to applet following 4 steps:

1) Call to image fileImage img = getImage( getCodeBase( ), "afraid.gif" );

2) Create an ImageIconImageIcon ic = new ImageIcon( img );

3) Create a JLabel with the ImageIcon JLabel label = new JLabel(ic );

4) Add label to appletsetLayout( new FlowLayout( ) );add( label );

Page 12: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Image Exampleimport javax.swing.*;import java.awt.*;

public class ImageExx extends JApplet{ public void init ( ) {

Image img = getImage( getCodeBase( ), "afraid.gif" ); // CANNOT do this:: add( img );

ImageIcon ic = new ImageIcon( img ); // CANNOT DO THIS:: add( ic );

JLabel label = new JLabel( ic ); // have to add to a JLabel // then add to the applet setLayout( new FlowLayout( ) );

add( label ); }}

can not add Image objects directly to the

applet

can not add ImageIcon objects

directly to the applet

Page 13: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Buttons

JButton

Page 14: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Buttons

• Swing component: JButton

• can have– Text– Images– Text & Images

Page 15: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Buttons• Swing component: JButton• can have

– Text

JButton mybutton = new JButton( “Click Me” );

– Image

JButton mybutton = new JButton( ImageIcon );

– Text & Image

JButton mybutton = new JButton( “text”, ImageIcon );

text overlaid on top of image

JButton mybutton = new JButton( “text”, ImageIcon ); mybutton.setHorizontalTextPosition(JButton.CENTER);

Page 16: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Image Buttons

• – mybutton.setBorderPainted( false );

• – mybutton.setContentAreaFilled( false );

• – (both lines above)

• – mybutton.setMargin( new Insets(0,0,0,0) );

where first 0 is top margin, second 0 is left, third 0 is bottom margin and last 0 is right

JButton mybutton = new JButton( imgIcon );

Page 17: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Text Components

JTextField

Page 18: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextField

• Box where users can enter one line of text

JTextField street = new JTextField();JTextField city = new JTextField( 50 );JTextField state = new JTextField( “CO”, 2 );JTextField zip = new JTextField( “80521” );

• JPasswordField – for entering passwords

– displays ****** while user types,otherwise is just like JTextField

Page 19: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextField• Constructors

– JTextField tf = new JTextField( );• creates a text field with a default number 0 of columns

– JTextField tf = new JTextField( 2 );• creates a text field with 2 columns (good for states: NC, IL )

– JTextField tf = new JTextField( “I love JaVa” );• creates a text field with the text “I love JaVa” inside the text box –

box size adjusts to text

– JTextField tf = new JTextField( “Java rocks”, 10 );• creates a text field with the text “Java rocks” inside the text box

which has 10 columns visible

• Note: 1 column = width of ‘M’ character in current font

Page 20: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextField

• Useful methods:– String getText( )

• Returns the text that is inside the boxString theText = textField.getText( );

– void setFont( Font f )• Set the font for the text boxFont fnt = new Font( “Serif”, Font.BOLD, 18 );textField.setFont( fnt );

– void setText( String t )• Enters the text in the string t into the text box

textField.setText( “I like to learn java” );

Page 21: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Text Components

JTextArea

Page 22: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextArea

• Box for users to enter multiple lines of text

• Specify number of rows and columns

Page 23: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextArea

• Constructors– JTextArea ta = new JTextArea ( );

• creates a text area with a default number of columns

– JTextArea ta = new JTextArea ( 5, 60 );• creates a text area with 5 rows and 60 columns

– JTextArea ta = new JTextArea ( “I love JaVa” );• creates a text area with the text “I love JaVa” inside the text box

– JTextArea ta = new JTextArea ( “Java rocks”, 4, 10 );• creates a text area with the text “Java rocks” inside the text box

which has 4 rows visible (height) and 10 columns visible (width)

Page 24: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JTextArea

• Useful methods:– String getText( )

• returns the text that is inside the boxString theText = textField.getText( );

– void setFont( Font f )• set the font for the text boxFont fnt = new Font( “Serif”, Font.BOLD, 18 );textField.setFont( fnt );

– void setText( String t )• enters the text in the string t into the text box

textField.setText( “I like to learn java” );

Page 25: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Choices

JCheckBox

Page 26: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JCheckBox

• A check box can be toggled checked or unchecked

• Can have one or more checkboxes selected(as opposed to radio buttons)

Page 27: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JCheckBox - constructors

• Constructors

– JCheckBox cb = new JCheckBox( );

• creates a new checkbox with no text

– JCheckBox cb = new JCheckBox( imgIcon );

• creates a new checkbox with an image

– JCheckBox cb = new JCheckBox( “happy” );

• creates a new checkbox with the text “happy”

– JCheckBox cb = new JCheckBox( “joyful”, true );

• creates a new checkbox with the text “happy” and checked

– JCheckBox cb = new JCheckBox( “serene”, imgicon );

• creates a new checkbox with the text “happy” and an image

} } }

Page 28: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JCheckBox - methods

• Useful Methods– String getText( )

• returns the checkbox’s text• String theText = cbox.getText( );

– void setEnabled( boolean b )• enables or disables the checkbox• cbox.setEnabled( false );• cbox.setEnabled( true );

Page 29: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JCheckBox Example/** Example showing use of JCheckBox * @author : E.S.Boese (c) Fall 2005 */import javax.swing.*;import java.awt.*;public class JCheckBoxEx extends JApplet{ JCheckBox cb1, cb2, cb3; public void init( ) {

setLayout(new FlowLayout( ) );cb1 = new JCheckBox( "red" );cb2 = new JCheckBox( "blue" );cb3 = new JCheckBox( "pink" );add( cb1 );add( cb2 );add( cb3 );

}}

Page 30: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Choices

JRadioButton

Page 31: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Radio Buttons

• A group of radio buttons represents a set of mutually exclusive options – only one can be selected

• When a radio button from a group is selected, the button that is currently checked in the group is automatically toggled off

• To define the group of radio buttons that will work together, each radio button is added to a ButtonGroup object

Page 32: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JRadioButton - Exampleimport javax.swing.*; import java.awt.*; public class JRadioButtonEx extends JApplet {

public void init( ) {

JRadioButton jb1 = new JRadioButton("red" ); JRadioButton jb2 = new JRadioButton("blue" ); JRadioButton jb3 = new JRadioButton("pink" ); ButtonGroup group = new ButtonGroup( ); group.add( jb1 ); group.add( jb2 ); group.add( jb3 );

setLayout(new FlowLayout()); add( jb1 );add( jb2 ); add( jb3 );

} }

How can you add another set of

selections for “Favorite Beverage”, such that user can select one

color and one beverage?

Page 33: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JRadioButton - constructors

• Constructors– JRadioButton rb = new JRadioButton( imgIcon);

• Creates a radio button with an image• Useful if you set two images: one for selected, one for unselected

(otherwise can’t tell the difference!)

– JRadioButton rb = new JRadioButton( “red” );• Creates a radio button with the text “red”

– JRadioButton rb = new JRadioButton( “blue”, true );• Creates a radio button with the text “blue” and checked

– ButtonGroup group = new ButtonGroup( );• Creates a group that radio buttons can be associated together

Page 34: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JRadioButton - methods

• Useful Methods– String getText( )

• returns the text for the radio buttonString text = radioBut.getText( );

– void setEnabled( boolean b )• enables or disables the radio button

radioBut.setEnabled( false );

Page 35: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

List Choices

JComboBox

Page 36: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JComboBox

• Drop-down box

• Can be:– User-editable:

• allows user to type in a value in the field similar to a JTextField

• list.setEditable( true );

– User-un-editable: • user must select an entry from the drop-down list• default

Page 37: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JComboBox - exampleimport javax.swing.*; import java.awt.*; public class JComboEx extends JApplet {

JComboBox majors = new JComboBox( );

public void init( ) {

majors.addItem( "CS" ); majors.addItem( "Math" ); majors.addItem( "History" ); majors.addItem( "Leisure Studies" ); majors.addItem( "Psych" );

setLayout( new FlowLayout( ) ); add( majors );

} }

Page 38: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JComboBox - constructors

• Constructors– JComboBox droplist = new JComboBox( );

• creates an optional combo box

Page 39: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JComboBox - methods• Useful Methods

– int getItemCount( )• returns the number of items in the list• int numItemsInList = combolist.getItemCount( );

– int getSelectedIndex( )• returns the index of the selected item• int selectedIndex = combolist.getSelectedIndex( );

– int getSelectedItem( )• returns the selected item• String selectedItem = combolist.getSelectedItem( );

– void removeItem( Object obj )• removes the object from the list• combolist.remove( “sad” );

– void removeItemAt( int index )• removes the object at the specified index• combolist.remove( 2 );

Page 40: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

JComboBox – methods (con’t)

• useful methods con’t– void setEditable( boolean flag )

• determines whether the combo box is editable• combolist.setEditable( true );

– void setEnabled( boolean flag )• enables or disables the combo box• combolist.setEnabled( false );

Page 41: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Components

Page 42: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Component

• These widgets inherit from the Component class

Page 43: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

Components Manipulation

• The Component class has additional methods we can use on most components:– setForeground( Color )– setBackground( Color )– setFont( Font )– setEnabled( boolean )– setBorder( Border )– setToolTipText( String )– setBounds

Page 44: Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com

setBackground, setOpaqueimport java.awt.*;import javax.swing.*;public class OpaqueEx extends JApplet{ JCheckBox cb1, cb2, cb3; JPanel pane; public void init( ) { setLayout( new FlowLayout( ) ); pane = new JPanel( ); pane.setBackground( Color.ORANGE ); cb1 = new JCheckBox( "periwinkle" ); cb2 = new JCheckBox( "snowflake" ); cb2.setBackground( Color.ORANGE ); cb3 = new JCheckBox( "magenta" ); cb3.setOpaque(false); pane.add( cb1 ); pane.add( cb2 ); pane.add( cb3 ); add( pane ); }}

Default background color of components is grey, like

cb1. We set cb2’s background to orange to

match the color of the JPanel. The other way is to setOpaque(false) like cb3,

which allows the component to be transparent and allow the background color of the component below to show

through.