my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Swing--JTable in JInternalFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing--JTable in JInternalFrame" Watch "Swing--JTable in JInternalFrame" New topic
Author

Swing--JTable in JInternalFrame

Mel Ref
Greenhorn

Joined: Jun 29, 2006
Posts: 1
Hi everyone.
I am trying to show a jtable in an internalframe,
my jtables are created using AbstractTableModel
and I want the user to be able to open multiple tables(like in Excel) in my application.
I managed to show tables in frames but I have a problem that
"when I open a new table , the rows of the new table are added at the end of the rows of the table I opened before.
What should I do?
Here is some of my code:
--------------------
import javax.swing.JInternalFrame;

public class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;

public MyInternalFrame(String tablekey) {
super("Data Table #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable

MyTable myTable=new MyTable(tablekey);
JScrollPane scroll = new JScrollPane(myTable.table);

setContentPane(scroll);
setSize(500,300);
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
}

-------------------------------------------------
public class MyTable {
public Vector<String> Columns = new Vector<String>();
public DataTable Data = new DataTable();
String name="untitled";
JTable table;
MyTableModel mtm;

public MyTable()
{
//initialize constant columns
Columns.addElement("File");
Columns.addElement("Type");
Columns.addElement("Size");
Columns.addElement("Location");
Columns.addElement("Height");
Columns.addElement("Width");
Columns.addElement("Raster");
Columns.addElement("Color");

//initialize table data, at first table is empty
Data=new DataTable();

mtm = new MyTableModel(Columns, Data);
table = new JTable(mtm);
table.setAutoResizeMode(0);//disabling autoresize in order to make horizontal JScrollPane to appear
}

public MyTable(String tablename)
{
//initialize constant columns
Columns.addElement("File");
Columns.addElement("Type");
Columns.addElement("Size");
Columns.addElement("Location");
Columns.addElement("Height");
Columns.addElement("Width");
Columns.addElement("Raster");
Columns.addElement("Color");

//initialize table data, at first table is empty
Data=new DataTable(tablename);
mtm = new MyTableModel(Columns, Data);
table = new JTable(mtm);
table.setAutoResizeMode(0);//disabling autoresize in order to make horizontal JScrollPane to appear
}
}
-------------------------------------------------------------

public class MyTableModel extends AbstractTableModel implements TableModelListener {

private static final boolean DEBUG = false;
private Vector columnNames = null;
private DataTable data=null;

public MyTableModel (Vector<String> initialColumns, DataTable initialDataTable){

columnNames = initialColumns;
data = initialDataTable;
this.addTableModelListener(this);
}

public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
System.out.println("New value of data:");
printDebugData();
}

public int getColumnCount() {
return ((Vector)data.get(0)).size();
}

public int getRowCount() {
return data.size();
}

public String getColumnName(int col) {
return columnNames.get(col).toString();
}

public Object getValueAt(int row, int col) {
Vector tmp = (Vector)data.elementAt(row);
return tmp.elementAt(col);
}

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}

public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
Vector tmp = (Vector)data.elementAt(row);
tmp.setElementAt(value, col);
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + ((Vector)data.get(i)).get(j));
}
System.out.println();
}
System.out.println("--------------------------");
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing--JTable in JInternalFrame
 
Similar Threads
JTable
jtable
JScrollPane within JTable cell issue
unable to get row index in JTable
JCheckBox in JTable