Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

                         

Facing Programming Problem?
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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.