Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Graphical User Interfaces Part 3

Advance Controls in GUI

Compiled By: Umm-e-Laila 9

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

◼ Aneeta Siddiqui aarshad@ssuet.edu.pk


Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,
Course Website

◼ http://sites.google.com/site/ulaila206

4
REVISION
JList

◼ The object of JList class represents a list of text items.

◼ The list of text items can be set up so that the user can
choose either one item or multiple items.

◼ It inherits JComponent class..


JList
◼ The JList is similar to the JComboBox except
that the List appears in a box (no drop down
action needed)
❑ The JList allows for multiple selections instead of
the single selection of a JComboBox
◼ the JList has an instance datum selectionMode which
can be one of SINGLE_SELECTION,
SINGLE_INTERVAL_SELECTION (you can select
multiple items as long as they are contiguous in the list)
and MULTIPLE_INTERVAL_SELECTION (multiple items
whether contiguous or not)
◼ the default is for multiple items (to select multiple items,
hold down the control key while clicking, the shift key
selects all items between last and current item in list)
JList
◼ If your JList is longer than the space in your
JPanel, add the JList to a JScrollPane and
insert the JScrollPane into your JPanel
instead of the JList
❑ This provides you a scroll bar as needed to view
the whole list
JList

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:-

❑ This interface defines how access to the list data is achieved

◼ ListSelectionModel:-

❑ Interface, which defines methods that determine what list item or


items are selected
List Selection Model
◼ SINGLE_SELECTION
◼ SINGLE_INTERVAL_SELECTION
◼ MULTIPLE_INTERVAL_SELECTION
More on Jlist
◼ The JList will use a ListSelectionListener
(defined in javax.swing.event instead of
java.awt.event) for any changes to the list
(selecting or unselecting items)
❑ Such a change generates a ListSelectionEvent
❑ The ListSelectionListener must implement a
valueChanged method
More on Jlist
◼ Since a JList can have multiple selected
items, there are methods that return single
Objects or ints (selected items, selected item
indices) or arrays
❑ single value methods: getMaxSelectionIndex,
getMinSelectionIndex, getLeadSelectionIndex,
getSelectedValue (returns the smallest index of
the selected)
❑ array methods: getSelectedIndices,
getSelectedValues
More on Jlist

public void valueChanged(ListSelectionEvent e) {


int[] x=list.getSelectedIndices();
for(int i=0;i<x.length;i++)
// do something with x[i]
}
JList

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");

//String array to store weekdays


String week[]= { "Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};

//create list
b= new JList(week);

//set a selected index


b.setSelectedIndex(2);

//add list to panel


p.add(b);

f.add(p);

//set the size of frame


f.setSize(400,400);

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() Creates a table with empty cells.

JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.

JTable(int rows, int cols) Creates a table of size rows * cols.


Functions in JTable

Method Description

addColumn(TableColumn []column) adds a column at the end of the JTable.

clearSelection() Selects all the selected rows and columns.

Edits the intersecting cell of the column


number col and row number row
editCellAt(int row, int col)
programmatically, if the given indices are
valid and the corresponding cell is editable.
Sets the cell value as ‘value’ for the
setValueAt(Object value, int row, int col)
position row, col in the JTable.
Jtable relies on 3 Model
◼ Table Model Interface:-
❑ This model defines those things related to
displaying data in two-dimensional format
◼ Table Column Model:-
❑ Specifies the characteristics of a column
◼ List Selection Model:-
❑ Determines how items are selected
JTable Example 1
public class TableExample {
JFrame f;
TableExample(){
f=new JFrame();
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args) {
new TableExample();
}
}
JTable Example 1
JTable Example 2
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class JTableExamples {


// frame
JFrame f;
// Table
JTable j;

// Constructor
JTableExamples()
{
// Frame initiallization
f = new JFrame();

// Frame Title
f.setTitle("JTable Example");

// Data to be displayed in the JTable


String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
};
JTable Example 2
// Column Names
String[] columnNames = { "Name", "Roll Number", "Department"

// Initializing the JTable


j = new JTable(data, columnNames);
j.setBounds(30, 40, 200, 300);

// 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:

public void paintComponent(Graphics g) {


// Graphics code goes here
}}}

You might also like