32
Pertemuan ke 4 JTable

Pertemuan ke 4 JTable. Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components Model The model

Embed Size (px)

Citation preview

Page 1: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Pertemuan ke 4JTable

Page 2: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components

ModelThe model encompasses the state data for each component. There are different models for different types of components. For example, the model of a menu might contain a list of the menu items the user can select from. This information remains the same no matter how the component is painted on the screen; model data always exists independent of the component's visual representation.

ViewThe view refers to how you see the component on the screen.

ControllerThe controller is the portion of the user interface that dictates how the component interacts with events. Events come in many forms - a mouse click, gaining or losing focus, a keyboard event, etc. The controller decides how each component will react to the event - if it reacts at all.

The Model-View-Controller Architecture

Page 3: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

The Model-View-Controller Architecture

Model

Controller

View

The view determineswhich events are passedto the controller

The controllerupdates the modelbased on the eventsreceived

The model passesits data to the viewfor rendering

Model

Controller

View

Component UI-Delegate

With Swing, the view and the controller are combined into a UI-Delegate object

Page 4: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Tables are used to display data in a spreadsheet fashion

The JFC JTable is oriented toward displaying database records in which each row displays a row in the database, and each column displays different record’s values for the same field

Tables

Page 5: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

JTable is a user-interface component that presents data in a two-dimensional table format

The JTable has many features that make it possible to customize its rendering and editing but provides defaults for these features.

A JTable consists of:◦ Rows of data◦ Columns of data◦ Column headers◦ An editor, if you want cells to be editable◦ A TableModel, usually a subclass of AbstractTableModel, which stores the table’s data◦ A TableColumnModel, usually DefaultTableColumnModel, which controls the behavior of

the table’s columns and gives access to the TableColumns◦ A ListSelectionModel, usually DefaultListSelectionModel, which keeps track of the

JTable’s currently selected row(s)◦ A TableCellRenderer, usually an instance of DefaultCellRenderer◦ Multiple TableColumns, which store graphical information about each column◦ A JTableHeader which displays column headers

Class JTable

Page 6: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Steps in creating and using JTable◦ Create a JTable (there are 7 different constructors)◦ Create a JScrollPane that can be used to scroll around the JTable via createScrollPaneForTable()

◦ Place the JTable within a container◦ Control whether grid lines should be drawn via setShowGrid()◦ Specify a default value for a cell via setValueAt()◦ Get the value for a cell via getValueAt()◦ Make individual cells selectable via setCellSelectionEnabled()

◦ Find out which cells are selected via the JTable’s ListSelectionModel and the TableColumnModel’s ListSelectionModel

◦ Add new rows and columns via the JTable’s TableModel

Class JTable

Page 7: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

AbstractTableModel is an abstract class that implements most of the TableModel interface

The TableModel methods that are not implemented are getRowCount() , getColumnCount() , and getValueAt()

Steps in creating and using AbstractTableModel◦ Create an AbstractTableModel subclass◦ Implement the getRowCount() , getColumnCount() , and getValueAt() methods

◦ Instantiate an instance of the subclass◦ Create a JTable using the subclass via new JTable( model )

Class AbstractTableModel

Page 8: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

To set up a table with 10 rows and 10 columns of numbers:

TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return 10; } public int getRowCount() { return 10;} public Object getValueAt(int row, int col) { return new Integer(row*col); }

};JTable table = new JTable(dataModel);JScrollPane scrollpane = new JScrollPane(table);

Class AbstractTableModel

Page 9: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

DefaultTableModel is the JFC’s default subclass of the abstract AbstractTableModel class

If a JTable is created and no TableModel is specified, the JTable creates an instance of DefaultJTableModel and uses it to hold the table’s data

If you have complex data, you may prefer to extend the AbstractTableModel yourself

Steps in creating and using DefaultTableModel◦ Create a DefaultTableModel (there are 6 different constructors)

DefaultTableModel( Vector data, Vector columnIDs)

◦ Create a JTable using the DefaultTableModel via new JTable(model)

Class DefaultTableModel

Page 10: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Steps in creating and using DefaultTableModel◦ Define a TableModelListener to receive TableModelEvents when the model changes, or when one or more cell’s contents change

◦ Add a row to the DefaultTableModel via addRow()◦ Add a column to the DefaultTableModel via addColumn()◦ Get the current value of a cell in a DefaultTableModel via getValueAt()

◦ Move one or more rows via moveRow()◦ Load a new set of data into a DefaultTableModel via setDataVector()

◦ Get the number of rows or columns in a DefaultTableModel via getRowCount() and getColumnCount()

Class DefaultTableModel

Page 11: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

A TableColumn contains the graphical attributes for a single column of data in a JTable’s model

It stores information about the column header, the column height and width, and how cells in the column should be drawn and edited

Steps in creating and using TableColumn◦ TableColumns are created automatically when columns are added to the

table model. They are accessed via the table column model via getColumn()

◦ Specify the TableCellEditor to use when editing the TableColumn’s cellsJCheckBox cbox = new JCheckBox()

DefaultCellEditor editor = new DefaultCellEditor(cbox)

tableColumn.setCellEditor(editor)

◦ Change the column header via setHeaderValue()

Class TableColumn

Page 12: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

DefaultTableColumnModel is the JFC’s default implementation of the TableColumnModel interface

This class is used to keep track of information about table columns. It gives access to TableColumns and keeps track of general characteristics of columns, like column margins and widths. It also contains a ListSelectionModel that it uses to keep track of which columns are currently selected

Steps in creating and using DefaultTableColumnModel◦ You will usually let the JTable create it◦ Specify the selection mode for the DefaultTableColumnModel via setSelectionMode()

Class DefaultTableColumnModel

Page 13: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Steps in creating and using DefaultTableColumnModel◦ Define a ColumnModelListener to receive TableColumnModelEvents when a column is added, removed, moved, margins are changed, or the column selection state changes

◦ Get the currently selected columns via getSelectedColumns()

Class DefaultTableColumnModel

Page 14: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

A JTableHeader is a companion to JTable and contains the graphical representation of the table’s column headers

A JTableHeader does not display by default but will display if you place a JTable into a JScrollPane created using the createScrollPaneForTable() method

A JTableHeader draws itself using information from the TableColumnModel associated with the JTable

Steps in creating and using JTableHeader◦ You will usually let the JTable create it◦ Change the TableCellRenderer used to draw a column’s header via

setHeaderRenderer()◦ Enable/Disable column reordering via

setReorderingAllowed()

Class JTableHeader

Page 15: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

DefaultCellEditor is an editor that can be used with a JTable or a JTree to edit table cells and tree nodes

It can edit in one of three ways: as a text field, as a check box, or as a combo box

Steps in creating and using a DefaultCellEditor◦ Create a component to be used by the DefaultCellEditor and set its

properties◦ Create a DefaultCellEditor using the component you just created◦ Specify how many mouse clicks it takes to start the editor via setClickCountToStart() (default is 2)

◦ Define a CellEditorListener to receive ChangeEvents when a cell editing session ends

Class DefaultCellEditor

Page 16: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

JTableMethod Description void addTableModelListener( TableModelListener listener )

Add a TableModelListener to the TableModel. The TableModel will notify the TableModelListener of changes in the TableModel.

void removeTableModelListener( TableModelListener listener )

Remove a previously added TableModelListener from the TableModel.

Class getColumnClass( int columnIndex )

Get the Class object for values in the column with specified columnIndex.

int getColumnCount() Get the number of columns in the TableModel. String getColumnName( int columnIndex )

Get the name of the column with the given columnIndex.

int getRowCount() Get the number of rows in the TableModel.

TableModel interface methods and descriptions.

Page 17: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

JTableObject getValueAt( int rowIndex, int columnIndex )

Get an Object reference to the value stored in the TableModel at the given row and column indices.

void setValueAt( Object value, int rowIndex, int columnIndex )

Set the value stored in the TableModel at the given row and column indices.

boolean isCellEditable( int rowIndex, int columnIndex )

Return true if the cell at the given row and column indices is editable.

TableModel interface methods and descriptions.

Page 18: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model
Page 19: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model
Page 20: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Delete RowSisakan 1 row

Page 21: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Event btnTambah

private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {String[] isi = {"","","",""}; ((DefaultTableModel)jTable1.getModel()).addRow(isi) ; }

Untuk menambah Row

Page 22: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Runing Program

Klik Tambah Row Akan menghasilkan Row kosong

Page 23: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Latihan 2

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {String[] isi = {jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),jTextField4.getText()}; ((DefaultTableModel)jTable1.getModel()).addRow(isi) ; }

Jika Tombol Tambah Diklik maka akan menambah data sesuai dengan text yang ditulis

Page 24: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Running Program

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {String[] isi = {jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),jTextField4.getText()}; ((DefaultTableModel)jTable1.getModel()).addRow(isi) ;jTextField1.setText(“”);jTextField2.setText(“”);jTextField3.setText(“”);jTextField4.setText(“”); }

Page 25: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Latihan 3

private void jTable1FocusGained(java.awt.event.FocusEvent evt) {i =jTable1.getSelectedRow(); }

Pada event jTable1.FocusGainedCapture lokasi Row index ke variable i.

Kita jadikan variable i sebagai variable global yang bisa diakses di semua object

Pada saat ada pesan kesalahan klik lampu lalu pilih addfield

Untuk tombol hapus .. Click dahulu row yang hendak dihapus.. Lalu klik hapus

Page 26: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Event pada tombol hapus

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { ((DefaultTableModel)jTable1.getModel()).removeRow(i) ; }

Page 27: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Latihan 4

Page 28: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int i = Integer.parseInt(jTextField1.getText());int y = Integer.parseInt(jTextField2.getText());

for (int x=i; x<=y;x++) { int kwadrat = x*x; int pangkat3 = x*x*x; String[] baris =

{String.valueOf(x),String.valueOf(kwadrat),String.valueOf(pangkat3)}; ((DefaultTableModel)jTable1.getModel()).addRow(baris); } //akhir for }

Code BtnCari

*Kelemahannya apabila dicari kembali maka akan terus menambah row sebelumnya* Angka di textfield1 kiri harus lebih kecil dari yang textfield2 dikanan.

Page 29: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int i = Integer.parseInt(jTextField1.getText());

int y = Integer.parseInt(jTextField2.getText());

int j = jTable1.getModel().getRowCount() ;

//cek jika sudah ada isinya atau belum

if (j > 0)

{

((DefaultTableModel)jTable11.getModel()).setNumRows(0);

}

//Cek apakah i lebih besar dari y, jika iya maka tukar nilai y jadi i dan i jadi y

If (i > y)

{ int w = y; //utk menampung nilai y sementara

y = i;

i= w; }

for (int x=i; x<=y;x++)

{

int kwadrat = x*x;

int pangkat3 = x*x*x;

String[] baris = {String.valueOf(x),String.valueOf(kwadrat),String.valueOf(pangkat3)};

((DefaultTableModel)jTable1.getModel()).addRow(baris); } //end for

}

Code BtnCari-Edited

Page 30: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Bahasa Pemrograman , Chaerul Anwar, MTI

Latihan 5Buatlah form mencari Deret=====================================

JText Area

• Bersihkan nilai jTextArea terlebih dahulu untuk menghapus nilai sebelumnya (jika sudah pernah dijalankan ) • Gunakan if untuk menentukan nilai tertinggi dan terendah• Gunakan loop for ()• Gunakan method append untuk mengisi nilai Pada JTextArea jTextArea1.append(String.valueOf(i) + " ");

Page 31: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Bahasa Pemrograman , Chaerul Anwar, MTI

Latihan 5Buatlah form mencari Deret====================================

Page 32: Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model

Selesai