Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Sharing a Table Model between JTable Components

Related Tutorials


 
 

Share on Google+Share on Google+

Sharing a Table Model between JTable Components

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

JSplitPane():
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.

HORIZONTAL_SPLIT:
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!");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","programmer","5000"},
   {
"Deepak","Writer","20000"},
   {
"Noor","Writer","30000"},
   {
"Rinku","programar","25000"}};
  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
(table1.getColumnModel().getColumn(0));

  JTableHeader header1 = table1.getTableHeader();
  header1.setBackground(Color.yellow);
  JTable table2 = new JTable(model){
  public Dimension getPreferredScrollableViewportSize() {
  return getPreferredSize();
  }
  };  
  JTableHeader header2 = table2.getTableHeader();
  header2.setBackground(Color.yellow);
  JSplitPane spane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  spane.add(new JScrollPane(table1));
  spane.add(new JScrollPane(table2));
  panel.add(spane);
  frame.add(panel);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setSize(500,150);
  frame.setVisible(true);  
  }
}

Download this example.

Output of program:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Rekha
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!
DMCA.com