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

Problem with JTable

Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi I want to display a table for which I have created a table and put this table in the ScrollPane.
The problem is that table width is too large to fit properly in the Scroll Pane. and only few column are displaying and rest of the columns are missing.
can anybody tell me what is the correct solution for this problem.


Raghuveer Rawat<br />SCJP2
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Take a look at setPreferredScrollableViewportSize method in JTable. Set the size and you should be ok.
Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
hi paul
I tried this solution. but It did not worked if I applied it correctly.
Let me elaborate more, problem is Horizontal Scroll is missing but verical Scroll is there, I can see all records but not all columns.
This code is too large to post here but I am putting some code here.

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();
System.out.println("selected table:"+selectedTable);
// 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));
JTableHeader header = jTable1.getTableHeader();
header.setDefaultRenderer(new MyHeaderRenderer());
for(int i=0;i<tableColumn.length;i++)
{
//System.out.println("column: "+tableColumn.length);
TableColumn column = (TableColumn) jTable1.getColumn(tableColumn[i]);
header.setResizingColumn(column);
column.setResizable(true);
int width= column.getWidth();
column.setMinWidth(width);
column.setCellRenderer(new MyTableCellRenderer());
}
jTable1.setPreferredScrollableViewportSize(new Dimension(900,500));
pane.setViewportView(jTable1);
}
}

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 boolean isCellEditable(int row, int col) {
return true;
}
public Object getValueAt(int row, int col)
{
// System.out.print("get valued called");
return data[row][col];
}
public void setValueAt(Object value, int row, int col)
{
data[row][col]=value;
this.fireTableCellUpdated(row, col);
this.fireTableDataChanged();
//System.out.print("cell updated");
}
}
class MyTableCellRenderer extends DefaultTableCellRenderer//implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int col)
{
Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
//setBackground((Color)value);
if(isSelected)
{
c.setBackground(Color.GRAY);
}
else
{
c.setBackground(Color.cyan);
}
return c;
}
}

class MyHeaderRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int col)
{
JTextArea area = new JTextArea();
area.setEditable(false);
area.setLineWrap(true);
//area.setAutoscrolls(true);
area.setBackground(Color.BLUE);
area.setForeground(Color.white);
area.setText(value.toString());
return area;
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with JTable