Sharing a Table Model between JTable Components

In this section, you will learn how to share a table model between JTable components.

Sharing a Table Model between JTable Components


In this section, you will learn how to share a table model between JTable components. Whenever, you want to do for sharing the resources between the JTable components, a table model will essential. To share the table model between two table components means sharing all resources to each other. When you change the values in the table model, it  will be changed in both the table components. But any changes in the  visible columns of one table component can not affect the columns of other table component.

Description of program:

This program helps you to do for sharing a table model between JTable components. For this, first of all you will need two tables that are joint  with the same table model. Both the tables have some data and columns with column headers. In this program, you must have to use the JSplitPane that is used to divide two components. These components are graphically divided that are based on look and feel implementation and resized by the user. Here the two components are split in left to right using the HORIZONTAL_SPLIT. Both components are added in the object of JSplitPane and it is also added in the frame's panel. Finally, both components attaches in the frame. If you want to change in any component,  there will be same changes in both the tables. But if any component is removed or added in any table, there will be no changes in any table.

Description of code:

This is the constructor of JSplitPane class that creates a new JSplitPane and arranged the components like: horizontal and vertical. The HORIZONTAL_SPLIT means arrange the components from left to right  and VERTICAL_SPLIT arranges from top to bottom.

This is a static integer type that splits the components on the x - axis.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class ShareTableModel{
  public static void main(String[] args) {
  new ShareTableModel();
  public ShareTableModel(){
  JFrame frame = new JFrame

"Sharing a Table Model Between JTable Components!");
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","programmer","5000"},
  String col[] {"Emp_name","Emp_depart","Emp_sal"};
  DefaultTableModel model = new DefaultTableModel(data, col);
  JTable table1 = new JTable(model){
  public Dimension getPreferredScrollableViewportSize() {
  return getPreferredSize();
//  table1.getColumnModel().removeColumn

  JTableHeader header1 = table1.getTableHeader();
  JTable table2 = new JTable(model){
  public Dimension getPreferredScrollableViewportSize() {
  return getPreferredSize();
  JTableHeader header2 = table2.getTableHeader();
  JSplitPane spane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  spane.add(new JScrollPane(table1));
  spane.add(new JScrollPane(table2));

Download this example.

Output of program:



We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Sharing a Table Model between JTable Components

Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Sharing a Table Model between JTable Components   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 2, 2011
sharing table model between 2 frames

I got the concept of sharing the table model in the same frame between 2 components, but how can i share the table model on 2 frames? say i want table1 to be displayed on JFrame1 and table 2 with different data to be displayed on JFrame2. Any help will be greatly appreciated. Thanks!