• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing--JTable in JInternalFrame

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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("--------------------------");
}

}
 
reply
    Bookmark Topic Watch Topic
  • New Topic