ScollPanes in jtable
To: comp.lang.java.gui
Hello all,
I have a minor problem with a Jtable and a custom renderer.
In short, I have a table with approx 700 rows and 4 columns.
The model contains only strings.
The third colulumn displays strings that can be quite long. To solve
that I have created a custom renderer that will use a JTextArea and a
JScrollPane if the text is long. This all works just fine, except that
I can not click on the scrollpane to scroll the text.
I figure that the table consumes the mouseclicks. But I am a little at
loss as to how to dispatch the mouse clicked/pressed/released to the
scollpane to make it scroll properly.
I tried adding a mouselistener to the table, and then figure out what
row/column was clicked. I got that far, but once I know what
row/column was clicked I am unsure how to inform the scollpane of the
click. I tried the dispatchEvent and passed the MouseEvent but that
threw an IllegalStateException stating that "The component must be
visible to determine it's location" The code does not show this
problem, as I am unsure that it is a good solution to the problem
any ideas are welcome!
thanks in advance
daniel
compilable demo of the problem. The rendering does not look good
because I removed all the styling of the renderers.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class TableDemo extends JFrame {
private JTable table;
public TableDemo() {
super. setDefaultCloseOperation(super.EXIT_ON_CLOSE
);
super.getContentPane().setLayout(new BorderLayout());
table=new JTable(new MyModel());
super.getContentPane().add(table,BorderLayout.CENTER);
table.getColumnModel().getColumn(2).setCellRenderer(new
MyCellRenderer());
}
public static void main(String args[]) {
System.out.println("Starting TableDemo...");
TableDemo mainFrame = new TableDemo();
mainFrame.setSize(400, 400);
mainFrame.setTitle("TableDemo");
mainFrame.setVisible(true);
}
private class MyModel extends AbstractTableModel{
public Object getValueAt(int rowIndex, int
columnIndex) {
if(columnIndex==0){
return Integer.toString(rowIndex);
}
if(columnIndex==1){
return "Second column text on row " +rowIndex;
}else if(columnIndex==2){
if(rowIndex%3==0){ // simple way to give some
variation, so not all rows have scrollbars
return "A rather long text to show make sure we get a
panel with a scrollbar on the side to try to fix this problem A rather
long text to show make sure we get a panel with a scrollbar on the
side to try to fix this problem";
}else{
return "A shorter string";
}
}else if(columnIndex==3){
return "Shorter editable text";
}
return "not valid";
}
public boolean isCellEditable(int rowIndex,int columnIndex){
if(columnIndex!=3){ // just the last column is editable
return false;
}
return true;
}
public int getColumnCount() {
return 4;
}
public int getRowCount() {
return 10;
}
}
private class MyCellRenderer implements TableCellRenderer {
private DefaultTableCellRenderer oneline; // use the default for
shorter strings
private JTextArea multiline; // used for long strings
private JScrollPane sp; // use to give scrollbars on the multiline
version
public MyCellRenderer(){
oneline= new DefaultTableCellRenderer();
multiline = new JTextArea();
multiline.setEditable(false);
multiline.setLineWrap(true);
multiline.setWrapStyleWord(true);
oneline.setOpaque(true);
sp= new JScrollPane(multiline);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int
column) {
JComponent c=null;
if(value.toString().length()>50){ //this value is arbitrary,
c=multiline;
}else{
c=oneline;
}
/**
* styling code omitted
*/
setValue(value,c);
return returnComponent(c,table,row);
}
protected void setValue(Object value,JComponent c) {
if(c==oneline){
oneline.setText((value == null) ? "" : value.toString());
}else{
multiline.setText((value == null) ? "" :
value.toString());
}
}
private Component returnComponent(Component c,JTable table,int
row){
if(c==oneline){
return c;
}
table.setRowHeight(row,60);
return sp;
}
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24