Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2007

Tip: Looking for answers? Try searching our database.

ScollPanes in jtable

Thread view: 
Daniel - 06 Jan 2007 02:30 GMT
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;
   }

    }
   
   
   
}
hiwa - 07 Jan 2007 23:20 GMT
> I can not click on the scrollpane to scroll the text.
Renderer does not make real components.
They are just snap shots or rubber stamps.
You have to implements a custom cell editor instead.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.