This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Problem with JTable  RSS feed

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at setPreferredScrollableViewportSize method in JTable. Set the size and you should be ok.
 
Raghuveer Rawat
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!