• 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

table is not redrawn

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi abhik
Thanks a lot.
This line give me what I was looking for.


pane.setViewportView(jTable1);




Thanks One again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic