Advertisement



< Prev
Next >



JTable class




JTable class is used to create a table with information displayed in multiple rows and columns. When a value is selected from JTable, a TableModelEvent is generated, which is handled by implementing TableModelListener interface. JTable is another lightweight component which extends JComponent class.





Constructors of JTable


Constructor Description
public JTable() Constructs a default JTable that is initialized with a default data model, a default column model, and a default selection model.
public JTable(int rows, int columns) Creates a JTable to display the values in rows and columns, using DefaultTableModel.
public JTable(TableModel tm) Creates a JTable that is initialized with tm as the data model, default column model, and a default selection model.





Methods of JTable class


Methods Description
public TableModel getModel() Gets the TableModel whose data is displayed by JTable.
public int getRowCount() Gets the current total number of the rows in the JTable.
public int getColumns() Gets the current total number of the columns in the JTable.
public void setDefaultRenderer(Class <?> class, TableCellRenderer renderer) Sets the default cell renderer to be used to set the values, alignment, background of a cell in JTable





An example of JTable


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.DefaultTableCellRenderer;

public class Table1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
String [] index;
JFrame jf;
JTable table1, table2; 
JLabel label1, label2;

A()
{
index= new String[]{"Rank", "Country", "GDP(millions of US$)"};


 
jf= new JFrame("JTable");
label1 = new JLabel("Top 10 economies in the world");


Object[][] rawData = new Object[] [] {
		{"1", "USA", "$19.42 Trillion"},
		{"2", "China","$11.8 Trillion"},
		{"3", "Japan", "$4.84 Trillion"},
		{"4", "Germany", "$3.42 Trillion"},
		{"5", "United Kingdom", "$2.91 Trillion"},
		{"6", "India", "$2.45 Trillion"},
		{"7", "France", "$2.42 Trillion"},
		{"8", "Brazil", "$2.14 Trillion"},
		{"9", "Italy", "$1.81 Trillion"},
		{"10", "Canada", "$1.6 Trillion"}
		};
	
table1 = new JTable(rawData, index);

JScrollPane scrollP = new JScrollPane(table1);

DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
tableRenderer.setHorizontalAlignment(JLabel.CENTER); //Aligning the table data centrally.
table1.setDefaultRenderer(Object.class, tableRenderer);


scrollP.setBorder(BorderFactory.createEmptyBorder()); //How to remove the border of JScrollPane.
scrollP.setPreferredSize(new Dimension(400, 180));		//Setting the size of JScrollPane

//Setting the label2 with message to show total number of rows and columns in JTable.
label2 = new JLabel("Rows : " + table1.getRowCount() + ", Columns : "+ table1.getColumnCount() );

jf.add(label1);
jf.add(scrollP);
jf.add(label2);

jf.setLayout(new FlowLayout());
jf.setSize(500,280);
jf.setVisible(true);
}

}
}

When you run the code, you are presented a window shown below -:

Figure 1



Advertisement




Creating and initializing a JTable from an existing TableModel


In the upcoming code, we are going to create a JTable by using its constructor that takes an object of TableModel object. We will create table using DefaultTableModel, which has implemented TableModel interface and we will use this table we will create our JTable.

import javax.swing.*;
import java.awt.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;



public class Table4
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A 
{
String [] index;
JFrame jf;
DefaultTableModel dTableModel;
JTable table1; 
JLabel label1;

A()
{
index= new String[]{"Rank", "Country", "Points"};

jf= new JFrame("JTable");
label1 = new JLabel("Top 10 rankings in Football");

Object[][] rawData = new Object[] [] {
		{"1", "Germany", "1609"},
		{"2", "Brazil","1603"},
		{"3", "Argentina", "1413"},
		{"4", "Portugal", "1332"},
		{"5", "Switzerland", "1329"},
		{"6", "Poland", "1319"},
		{"7", "Chile", "1250"},
		{"8", "Colombia", "1208"},
		{"9", "France", "1199"},
		{"10", "Belgium", "1194"}
		};

//creating a DeFaultTableModel object, which is subclass of TableModel
dTableModel = new DefaultTableModel(rawData, index);

//Initializing a JTable from DefaultTableModel.
table1 = new JTable(dTableModel);

//Adding JTable to JScrollPane to display it properly
JScrollPane scrollP = new JScrollPane(table1);

//Adjusting the contents of each cell of JTable in CENTER
DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
tableRenderer.setHorizontalAlignment(JLabel.CENTER); //Aligning the table data centrally.
table1.setDefaultRenderer(Object.class, tableRenderer);

//Removing the unnecessary border of JScrollPane
scrollP.setBorder(BorderFactory.createEmptyBorder()); //How to remove the border of JScrollPane.


jf.add(label1);
jf.add(scrollP);

jf.setLayout(new FlowLayout());
jf.setSize(500,280);
jf.setVisible(true);
}
}
When you run the code, you are presented a window shown in the Figure2. This window displays a JTable showing us the top 10 world rankings in Football -:

Figure 2





Handling JTable events by implementing TableModelListener


In the next code, we are going to handle the events occuring when a particular cell of JTable is selected or when it's value is changed, to handle such events, we will implement TableModelListener interface and will provide implementation of its method tablechanged().

import javax.swing.*;
import java.awt.*;
import javax.swing.table.TableModel;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;



public class Table5
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements TableModelListener
{
String [] index;
JFrame jf;
JPanel jp;
DefaultTableModel dTableModel;
JTable table1, table2; 
JLabel label1, label2;

A()
{
index= new String[]{"Rank", "Country", "Points"};


 
jf = new JFrame("JTable");
label1 = new JLabel("Top 10 rankings in Football");
label2 = new JLabel("");


Object[][] rawData = new Object[] [] {
		{"1", "Germany", "1609"},
		{"2", "Brazil","1603"},
		{"3", "Argentina", "1413"},
		{"4", "Portugal", "1332"},
		{"5", "Switzerland", "1329"},
		{"6", "Poland", "1319"},
		{"7", "Chile", "1250"},
		{"8", "Colombia", "1208"},
		{"9", "France", "1199"},
		{"10", "Belgium", "1194"}
		};

//creating a DeFaultTableModel object, which is subclass of TableModel
dTableModel = new DefaultTableModel(rawData, index);

//Initializing a JTable from DefaultTableModel.
table1 = new JTable(dTableModel);


JScrollPane scrollP = new JScrollPane(table1);

DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
tableRenderer.setHorizontalAlignment(JLabel.CENTER); //Aligning the table data centrally.
table1.setDefaultRenderer(Object.class, tableRenderer);


scrollP.setBorder(BorderFactory.createEmptyBorder()); //How to remove the border of JScrollPane.
scrollP.setPreferredSize(new Dimension(350, 180));	//Setting the size of JScrollPane

jf.add(label1);
jf.add(scrollP);
jf.add(label2);

//Registering our Jtable for event listening
table1.getModel().addTableModelListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(400,340);
jf.setVisible(true);
}


public void tableChanged(TableModelEvent e)
{
TableModel tabModel= (TableModel)e.getSource();
int row = e.getFirstRow();
int column = e.getColumn();

//Retrieving the value a specific row,column from the JTable and setting this value to JLabel, to show the selected or a new edited cell value.
label2.setText( (String)tabModel.getValueAt(row,column)); 
jf.setVisible(true);
}

}
When you run the code, you are presented a window shown in the Figure 3, showing you the top 10 rankings of international Football -:

Figure 3


Now we have selected and edited Poland to Polska and as soon as we finished editing it, we were notifed about the change in the window itself.

Figure 4




Please share this article -





< Prev
Next >
< JRadioButton
JList >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement