Forums Register Login

Problem with JTable

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Take a look at setPreferredScrollableViewportSize method in JTable. Set the size and you should be ok.
+Pie Number of slices to send: Send
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;
}
}
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1646 times.
Similar Threads
Is it possible or not?
javax.servlet.ServletException: java.lang.IndexOutOfBoundsException: Index: 6, Size: 0
Dynamically increasing row height in JTable
Problem with table row overlapping with table header
Hibernate criterion query with foreign key relation-ship
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:12:48.