30-07-2012, 10:06 AM
Java Swing
Java Swing.ppt (Size: 657.5 KB / Downloads: 150)
Note - this presentation..
often needs to refer to source code which is too big to put on a slide
So the source code is in a separate Word document
And is also given in within this presentation in the notes
What is Swing?
A group of 14 packages to do with the UI
451 classes as at 1.4 (!)
Part of JFC Java Foundation Classes (compare now defunct MFC)
Swing and the AWT
AWT = abstract windows toolkit (cross platform)
AWT = earliest version of Java GUI
eg Frame AWT not JFrame Swing
Most Swing components are 'lightweight'
Do not mix AWT and Swing
Use Swing
Swing and threads
A thread is a lightweight process
Most Swing components are not thread-safe
Solution is to make sure all code that creates and modifies Swing components executes in the same 'event-dispatching' thread
Start a Swing application using the following code..
Swing and Threads - starting up
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI(); // << method to start it
}
});
}
createAndShowGUI
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Hi..");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add a label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Layout Managers
Most Swing UIs utilise a LayoutManager to control positioning of items
There is a choice of these which work in different ways
Initially we do without one, and position items ourselves:
frame.setLayout(null);
Responding to user actions
Based on an event-handling model
New component eg a button should have a Listener specified
The Listener object is programmed to respond to Event objects coming from the component
The Listener object needs to implement the appropriate interface
Interfaces
An interface is a set of methods
eg the ActionListener interface has just one method -
public void actionPerformed(ActionEvent e)
A class can declare that it implements it eg
public class Main implements ActionListener
Then it must actually define the methods in that interface
Or the compiler will complain
Classes can implement multiple interfaces
Button click demo
See source code in Word
JButton and JLabel
clickCounts remembers the number of clicks
Class implements ActionListener
Make JFrame, JButton and JLabel
Instantiate application object
Set to be the listener of the button
Which button?
If have several buttons, all must link to actionPerformed
How to know which button was clicked?
Use the .getSource method of the ActionEvent object
Example which button
butt1=new JButton("Button 1");
..
butt2 = new JButton("Button 2");
..
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==butt1)
label.setText("Butt1 clicked");
else
label.setText("Butt2 clicked");
}
JPanel ( in createAndShowGUI)
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("I am a JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(20,30,300,100);
frame.setLayout(null);
//Create a panel
JPanel myPanel = new JPanel();
myPanel.setBackground(new Color(255,3,25));
myPanel.setOpaque(true);
//Make it the content pane.
frame.setContentPane(myPanel);
frame.setVisible(true);
JPanel
Is a subclass of JComponent
So are all the other Swing components except the top-level containers
You can add a border
And a tool-tip
Tooltip and border
..
myPanel.setOpaque(true);
myPanel.setToolTipText("I'm a JPanel");
myPanel.setBorder(BorderFactory.createLineBorder(Color.white));
frame.setContentPane(myPanel);
..
JSplitPane
..
setLayout(null);
//Create a split pane
JSplitPane myPane = new JSplitPane();
myPane.setOpaque(true);
frame.setContentPane(myPane);
frame.setVisible(true);
JSplitPane with JPanels
//Create a split pane
JSplitPane myPane = new JSplitPane();
myPane.setOpaque(true);
myPane.setDividerLocation(150);
// make two panels
JPanel right = new JPanel();
right.setBackground(new Color(255,0,0));
JPanel left = new JPanel();
left.setBackground(new Color(0,255,0));
// set as left and right in split
myPane.setRightComponent(right);
myPane.setLeftComponent(left);
JTextField
For single-line text input
Methods getText, setText
Can use ActionListener, triggered when Enter pressed
Example of JTextField
See source in Word doc
Check Main object fields for label and textfield
Make a panel, set as content pane
Make and add text field
Add actionlistener
Make and add a label
Program actionPerformed
JTextArea
JPanel myPanel = new JPanel();
app.textArea = new JTextArea("Type here",5, 20);
myPanel.add(app.textArea);
TextArea expands rows and columns as needed
JScrollPane
JTextArea textArea = new JTextArea("Type here",5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.setContentPane(scrollPane);
Exercise
Program this
Use the selectAll and cut methods of JTextComponent, which JTextArea inherits
Timer
..
Timer t = new Timer(1000, app);
t.start();
app.label = new JLabel("Time");
app.label.setBounds(20,20,200,20);
frame.getContentPane().add(app.label);
..
public void actionPerformed(ActionEvent e)
{
String now = (new java.util.Date()).toString();
label.setText(now);
}
Images
JFrame frame = new JFrame("I am Celsius");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(20,30,200,200);
frame.getContentPane().setLayout(null);
ImageIcon icon = new ImageIcon("c:/celsius.jpg", "Celsius");
JLabel label = new JLabel(icon);
label.setBounds(20,20,150,150);
frame.getContentPane().add(label);
frame.setVisible(true);
JScrollBar
See source code
JScrollBar and JLabel
Constructor arguments
implements AdjustmentListener
adjustmentValueChanged
e.getValue()
Exercise
Program this
The scroll bars determine the red, green and blue components of the background of the panel
JCheckBox
See source code
implements ActionListener
isSelected()
Exercise
Program this
The checkbox determines if the text in the label is left or right aligned
RadioButton
Come in groups – only 1 selected per group
See demo code
Make radiobuttons
Make group
Add radiobuttons to group
ActionListener