KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA table list

Best books about JAVA programming:

JAVA JTable union Jtables

The following code union 2 tables input into the third table dynamically.

//Example modified from http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html
//Original example doesn't work
/**
* @version 1.0 12/03/98
*/


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

/**
* @version 1.0 11/09/98
*/
public class UnionDataExample extends JFrame {

UnionDataExample() {
super( "UnionData Example" );
setSize( 300, 200 );

final String[] headers = {"A", "B", "C", "D"};
final String[][] data = new String[6][headers.length];

final AbstractTableModel dmAB = new AbstractTableModel() {
public int getRowCount() { return data.length; }
public int getColumnCount() { return headers.length; }
public String getColumnName(int col) { return headers[col]; }
public Object getValueAt(int row, int col) {
return data[row][col];
}
};

final int rowCountA = 3;
DefaultTableModel dmA = new DefaultTableModel(data, headers) {
public int getRowCount() { return rowCountA; }
public Object getValueAt(int row, int col) {

Vector rowVector = (Vector)dataVector.elementAt(row);

return rowVector.elementAt(col);
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = (String)value;
Vector rowVector = (Vector)dataVector.elementAt(row);
rowVector.setElementAt(value, col);
TableModelEvent event = new TableModelEvent(this, row, row, col);
fireTableChanged(event);
dmAB.fireTableChanged(event);
}
};

DefaultTableModel dmB = new DefaultTableModel(data, headers) {
public int getRowCount() { return rowCountA; }

public void setValueAt(Object value, int row, int col) {
int sRow = row + rowCountA;
data[sRow][col] = (String)value;
Vector rowVector = (Vector)dataVector.elementAt(row);
rowVector.setElementAt(value, col);
TableModelEvent event = new TableModelEvent(this, sRow, sRow, col);
fireTableChanged(event);
dmAB.fireTableChanged(event);
}
public Object getValueAt(int row, int col) {
Vector rowVector = (Vector)dataVector.elementAt(row);
return rowVector.elementAt(col);
}
};

JScrollPane scrollA = new JScrollPane(new JTable( dmA ));
JScrollPane scrollB = new JScrollPane(new JTable( dmB ));
JScrollPane scrollAB = new JScrollPane(new JTable( dmAB ));

Box box = new Box(BoxLayout.Y_AXIS);
box.add(new JLabel("Table A"));
box.add(scrollA);
box.add(new JSeparator(SwingConstants.VERTICAL));
box.add(new JLabel("Table B"));
box.add(scrollB);
Box boxAB = new Box(BoxLayout.Y_AXIS);
boxAB.add(new JLabel("Table A + B"));
boxAB.add(scrollAB);
Container content = getContentPane();
content.setLayout(new BoxLayout(content,BoxLayout.X_AXIS));
content.add(box);
content.add(new JSeparator(SwingConstants.HORIZONTAL));
content.add(boxAB);
}

public static void main(String[] args) {
UnionDataExample frame = new UnionDataExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.setVisible(true);
}
}

 


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer