Professional Documents
Culture Documents
Lecture 9 GUI JTable, JList, JScrollbar
Lecture 9 GUI JTable, JList, JScrollbar
Lecture # 9
1
Course Books
◼ Text Book:
◼ Herbert Schildt, Java: The Complete Reference, McGraw-Hill
Education, Eleventh Edition
◼ Craig Larman, Applying UML & patterns, 2 edition
◼ Reference Books:
◼ Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
◼ Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
◼ Umm-e-Laila ulaila@ssuet.edu.pk
Assistant Professor, CED
Room Number: BS-04
Tel: 111-994-994, Ext. 536
◼ http://sites.google.com/site/ulaila206
4
REVISION
JList
◼ The list of text items can be set up so that the user can
choose either one item or multiple items.
Constructor Description
Creates a JList with an empty, read-only,
JList()
model.
Creates a JList that displays the elements
JList(ary[] listData)
in the specified array.
Creates a JList that displays elements from
JList(ListModel<ary> dataModel)
the specified, non-null, model.
Jlist is based on 2 Models
◼ ListModel:-
◼ ListSelectionModel:-
Method Description
Void It is used to add a listener to the list, to
addListSelectionListener(ListSelection be notified each time a change to the
Listener listener) selection occurs.
It is used to return the smallest
int getSelectedIndex()
selected cell index.
It is used to return the data model that
ListModel getModel() holds a list of items displayed by the
JList component.
It is used to create a read-only
void setListData(Object[] listData)
ListModel from an array of objects.
Java JList Example 1
import javax.swing.*;
public class ListExample
{
ListExample(){
JFrame f= new JFrame();
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}}
Java JList Example 1
Java JList Example 2
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame
{
//frame
static JFrame f;
//lists
static JList b;
//main class
public static void main(String[] args)
{
//create a new frame
f = new JFrame("frame");
//create a object
solve s=new solve();
//create a panel
JPanel p =new JPanel();
Java JList Example 2
//create a new label
JLabel l= new JLabel("select the day of the week");
//create list
b= new JList(week);
f.add(p);
f.show();
} }
Java JList Example 2
JTable
◼ The JTable class is used to display data in
tabular form. It is composed of rows and
columns.
◼ You can drag the cursor on column
boundaries to resize the column. You can
also drag a column to a new position.
◼ Depending on its configuration, it is also
possible to select a row, column, or cell with
in the table, and to change the data with in
the cell.
JTable
Constructor Description
JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
Method Description
// Constructor
JTableExamples()
{
// Frame initiallization
f = new JFrame();
// Frame Title
f.setTitle("JTable Example");
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
// Frame Size
f.setSize(500, 200);
// Frame Visible = true
f.setVisible(true);
}
// Driver method
public static void main(String[] args)
{
new JTableExamples();
}
}
JTable Example 2
Java JScrollBar
◼ The class JScrollBar is an implementation of scrollbar.
◼ The object of JScrollbar class is used to add horizontal
and vertical scrollbar.
◼ It is an implementation of a scrollbar.
◼ It inherits JComponent class.
Constructor Description
Creates a vertical scrollbar with the initial
JScrollBar()
values.
Creates a scrollbar with the specified
JScrollBar(int orientation)
orientation and the initial values.
Creates a scrollbar with the specified
JScrollBar(int orientation, int value, int
orientation, value, extent, minimum, and
extent, int min, int max)
maximum.
Java JScrollBar Example 1
import javax.swing.*;
class ScrollBarExample
{
ScrollBarExample(){
JFrame f= new JFrame("Scrollbar Example");
JScrollBar s=new JScrollBar();
s.setBounds(100,100, 50,100);
f.add(s);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ScrollBarExample();
}}
Java JScrollBar Example 1
Scroll Bars and Scroll Panes
◼ JScrollBars are added to JFrames
❑ When you instantiate a scroll bar you specify its
orientation (JScrollBar.HORIZONTAL or
JScrollBar.VERTICAL)
❑ You can also specify the starting value, its min
and max values (if not specified, starting value by
default is in the middle)
Scroll Bars and Scroll Panes
◼ The problem with a scroll bar added to the
JFrame is moving the view within the JFrame
upon changing the scroll bar’s “knob”
Scroll Bars and Scroll Panes
◼ Instead, we can also place our JPanel inside
a scrollable component called a JScrollPane
– this will be easier
❑ Instantiate a JPanel for instance MyPanel p1 =
new MyPanel( );
❑ Instantiate a JScrollPane using p1, JScrollPane
jsp=new JScrollPane(p1);
❑ Now the JPanel will have its own scroll bars if
needed
Scroll Bars and Scroll Panes
import java.awt.*;
import javax.swing.*;
public class ScrollExample1 {
public static void main(String[] args) {
// JFrame code omitted
ScrollPanel p1=new ScrollPanel();
p1.setPreferredSize(new Dimension(250,100));
JScrollPane jsp=new JScrollPane(p1);
frame.add(jsp);
}
public static class ScrollPanel extends JPanel {
// code goes here for the JPanel
// for instance, maybe we want to draw something: