aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes table is not redrawn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "table is not redrawn" Watch "table is not redrawn" New topic
Author

table is not redrawn

Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi everyone
I want to draw a table dynamically based on user selection in the combo box.
so I have two components One Combo Box: which lists table name and Second a JTable which displays table data.
My Problem is that :
when page load first time based on the selected value of the combo box table is drawn but when I make another selection in the combo box table is not redrawn even though data is fetched from the database.
second problem is that I have put my JTable in the JScrollPane. But tables are wider and lengthy enough to contain in the ScrollPane properly.
Here Column Name and cell data is not displaying properly.
code I am writing is:
//This will redraw the table when user changes selection in the combo box

void jComboBox1_actionPerformed(ActionEvent e) {
Object combo= e.getSource();
String selectedTable="";
if(combo instanceof JComboBox)
{
JComboBox jcb = (JComboBox) combo;
//this will return selected table in the combo box
selectedTable = (String)jcb.getSelectedItem();
// this will return array list containg table column array and values array
ArrayList tablaData = new Customer().getTableData(selectedTable);
String tableColumn[] = (String[]) tablaData.get(0);
//System.out.print("column list:"+tableColumn.length);
Object data[][] = (Object[][]) tablaData.get(1);
//System.out.print("row list:"+data.length);
jTable1 = new JTable(data, tableColumn);
// jTable1.repaint();
pane = new JScrollPane(jTable1);
//pane.repaint();
System.out.println("table redrawn");
}
}
and I am adding this ScollPane to panel:
jPanel2.add(pane, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0
,GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
Any Swing guru who can help me in redrawing the Table and Displaying the Column and Cell Data Properly.


Raghuveer Rawat<br />SCJP2
Abhik Sarkar
Ranch Hand

Joined: Jun 14, 2003
Posts: 61
Hi Raghuveer,
1. Instead of

try

2. You could also have a look at the recent thread "Changing Table contents - Urgent" for ideas on the approaching the same problem from another angle.
Hope this helps,
Abhik.
[ June 30, 2003: Message edited by: Abhik Sarkar ]
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi abhik
I am doing now this way but still I am not able to redraw the table.
Can you look at this code and will suggest what mistake I am making.
void jComboBox1_actionPerformed(ActionEvent e) {
Object combo= e.getSource();
String selectedTable="";
//This will redraw the table when user changes selection in the combo box
if(combo instanceof JComboBox)
{
JComboBox jcb = (JComboBox) combo;
//this will return selected table in the combo box
selectedTable = (String)jcb.getSelectedItem();
// this will return array list containg table column array and values array
ArrayList tablaData = new Customer().getTableData(selectedTable);
String tableColumn[] = (String[]) tablaData.get(0);
//System.out.print("column list:"+tableColumn.length);
Object data[][] = (Object[][]) tablaData.get(1);
//System.out.print("row list:"+data.length);
jTable1 = new JTable(new MyTableModel(data, tableColumn));
new MyTableModel(data, tableColumn).fireTableDataChanged();
// jTable1.repaint();
pane = new JScrollPane(jTable1);
//stem.out.println("table redrawn");
}
}

class MyTableModel extends AbstractTableModel
{
Object[][] data=null;
String [] column=null;
MyTableModel()
{
}
MyTableModel(Object[][] data, String [] column)
{
this.data=data;
this.column=column;
}
public int getColumnCount()
{
return column.length;
}
public String getColumnName(int col)
{
return column[col];
}
public int getRowCount()
{
return data.length;
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
public void setValueAt(Object value, int row, int col)
{
data[row][col]=value;
//new MyTableModel(data, column).fireTableDataChanged();
}
}
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
Even I tried this way too it too is not working.
I changed This method in MyTableModel Class
but this is not working.
public void setValueAt(Object value, int row, int col)
{
data[row][col]=value;
this.fireTableCellUpdated(row, col);
this.fireTableDataChanged();
//System.out.print("cell updated");
}
and in the void jComboBox1_actionPerformed(ActionEvent e) method
i am doint this:
jTable1 = new JTable(new MyTableModel(data, tableColumn));
// TableModel model=new MyTableModel(data, tableColumn);
//jTable1.setModel(model);
jTable1.repaint();
pane = new JScrollPane(jTable1);
Abhik Sarkar
Ranch Hand

Joined: Jun 14, 2003
Posts: 61
Hi,
I usually never create a new table or a model once I have created and displayed it initially. I always have extra methods in my table model to change the data inside the model or to filter data in the model.
Also, if I have to display a newly created table in an already visible JScrollPane, I always call instead of creating a new JScrollPane.
If you do create a new JScrollPane as well (like you are doing), I think you need to call the containing Panel's invalidate() or revalidate() method.
I am afraid, this is all the help I can provide.
Best regards,
Abhik.
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi abhik
Thanks a lot.
This line give me what I was looking for.


pane.setViewportView(jTable1);



Thanks One again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: table is not redrawn
 
Similar Threads
Right Align numbers in Jtable
Problem with JTable
JTable Cell Renderers
jtable
How to Set the JTable Rows Color??