Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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:

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:

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

hello;(nice ..exelle your cite..)
pleas i need to know who to use boutton..
whene I click it i have the executable of my program..pleas if you like the others(the sol by exe executable)
thanck's

Posted by etud on Thursday, 04.26.07 @ 23:56pm | #14993

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.