Java and J2EE Swing
Java and J2EE Swing
Swing
[email protected]
Nagaraja Hebbar N
Department of CSE, Srinivas Institute of Technology, Mangalore
UNIT – 4:Swing
Swings:
The origins of Swing;
Two key Swing features;
Components and Containers;
The Swing Packages;
A simple Swing Application;
Create a Swing Applet;
JLabel and ImageIcon;
JTextField;
The Swing Buttons;
JTabbedpane;
JScrollPane;
JList;
JComboBox;
JTable.
What is Swing?
Why Swing?
Early Java’s approach to GUI design was using AWT (Abstract Window Tool kit)
Experienced with lot of deficiencies.
These are tackled in Swing classes.
AWT defines a basic set of controls , windows and dialog boxes that support a
usable , but limited graphical interface
AWT translates various visual components into their corresponding, platform
specific equivalents
Look and feel of a components is defined by the platform (not by java)
AWT uses native code resources –so refereed to heavyweight
Before Swing: Abstract Window Toolkit
AWT [Abstract Window Toolkit]is useful for developing simple user interface
Window Fundamental
Swing components are less dependent on target platform
Swing components are less of native GUI resource
Hence Swing are also known as lightweight components.
Key swing features
contents, such as the state of a button (pushed in or not), or the text in a text field;
visual appearance (color, size, and so on)
behaviour (reaction to events).
Observations:
JApplet, JFrame,Jdialog are not inherited
from JComponents
All are inherited from AWT
So AWT is base for all swing components
and container class
(this hierarchy is incomplete )
15 Containers
FlowLayout
Places components in rows from left to right.
Starts new row once previous row is filled.
BorderLayout
Places components in north,south,east,west, center.
GridLayout
Places in grid of cells in rows and columns.
CardLayout
The card layout manager. Card layouts emulate index cards. Only the one on top is
showing.
Grid bag layout
The grid bag layout manager. Grid bag layout displays components subject to the
constraints specified by GridBagConstraints.
add(Borderpanel);
}
}
Layout properties: GridLayout
28
public SwingDemo5(){
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3,1));
//contentPane.setLayout(new GridLayout(1,3));
jbtnOK=new JButton("Close");
JLabel lbl =new JLabel("Name:");
JTextField txt=new JTextField(25);
contentPane.add(lbl);
contentPane.add(txt);
contentPane.add(jbtnOK);
NH/Java and J233/7CS-A/2012
}
GridLayout Example 2
public SimpleFrameDemo()
SwingUtilities.invokeLater(new Runnable()
{
{ public void run() {new SwingDemo1();}} ); setTitle("User Interface...... ");
} setSize(200,200);
} }
class SwingDemo1 }
{
public SwingDemo1()
{
SimpleFrameDemo frame=new SimpleFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
About first Swing application
Use Javax.swing.* package to utilize all wing classes.
Create customized frame
Set default close operation
By default when top level windows is closed , the window is removed from screen.
But application still running, to terminate application when top level is closed use:
setDefaultCloseOperation. On exit of Close i.e JFrame.EXIT_ON_CLOSE
Other possible values are: DISPOSE_ON_CLOSE, HIDE_ON_CLOSE,DO_NOTHING_ON_CLOSE
{ }
SwingUtilities.invokeLater(new Runnable(){ }
public void run(){SwingDemoApplet();}});
}
catch(Exception e){}
}
Swing Application: JLabel
35
The icon and text associated with the label can be read
and written by the following methods:
Icon getIcon( ) //returns icon that was set
String getText( )//returns text
void setIcon(Icon i)//sets image
void setText(String s) //sets text
Here, i and s are the icon and text, respectively.
ImageIcon
37
To obtain image icon
The object of type ImageIcon can be passed as an
argument to the icon parameter of Jlabel’s
constructor.
Constructor: ImageIcon(String filename)
Icon and text associated with the label can be
obtained by: Icon getIcon()
String getText()
Icon and text associated with a label can be set by:
void setIcon(Icon icon)
void setText(String str)
Java and J2EE/NH/CS
JLabel and ImageIcon
38
imagebox=new ImageIcon(getClass().getResource("Image1.gif"));
setIconImage(imagebox.getImage());
JTextField
41 It is a text component of swing, used to edit one line of text
Constructors and Methods
JTextFields(int cols);
creates text field and sets size to cols
JTextFields(String str);
creates text field, and initializes text also
String getText();
Returns the text from the TextField
txt1=new JTextField("Source");
txt2=new JTextField();
txt2.setText(txt1.getText());
panel=new JPanel();
panel.add(lbl);
panel.add(txt1);
panel.add(txt2);
Complete Program:SwingTextFieldDemo.java
char[] getPassWord()
Returns the text contained in PasswordField
Overwrite the array after use
Methods:
void setDisabledIcon(Icon di)
void setPressedIcon(Icon di)
void setSelectedIcon(Icon si)
void setRolloverIcon(Icon ri)
String getText()
void setText(String str)
String Java
getActionCommand():This
and J2EE/NH/CS
identifies buttons
50
JButton
Events
When the button is pressed, an ActionEvent is generated.
Using ActionEvent object passed to the actionPerformed () method
of the registered ActionListener. You can obtain the action
command string associated with the button.
String getActionCommand()
This identifies the button among other buttons within the same application.
Complete example:SwingButtonDemo.java
Constructor:
JToggleButton(String str)
Method
boolean isSelected();
Events
Implements: ItemListener
Method to implemented:
itemStateChanged(ItemEvent ae){..}
Complete example:SwingToggleDemo.java
getselectedIndex(): returns index of item selected (index starts from 0) -1 means no item selected
Events:
ListSelectionEvent: generated when user makes or changes a selection, also when deselects handled by
ListSelectionListener:
The listener specifies only one method: Valuechanged(ListSelectionEvent e) method