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 / November 2006

Tip: Looking for answers? Try searching our database.

jtable row height problem

Thread view: 
Daniel - 18 Nov 2006 03:02 GMT
Hello all,
I have a problem with a jtable. I have written a program to simplify
the translation of the texts in my main application. So basically what
happen is that my editor reads the .properties file and lets the user
change the values of them.

The problem comes when the user is trying to view a value with html
text in it. What happened was that the html formatting contained
linebreaks and the default table cell renderer did the linebreaks BUT
the height of the cell did not update so all you could see was the
bottom of the first line and the top of the second.

I tried to fix this by using my own cell renderer that adjusted the
height if the preferred height of the renderer was greater than the
height of the row.

This almost worked, the row now becomes heigh enough to hold the whole
text BUT the cell is always rendered with an empty line at the top,
which means the last line is not visible. Any suggestions on how to
fix this?

I will try to make a minimal code example during the weekend.
I post the code for the cell renderer as it is short and possibly
(even probably) the cause of the problem

public class CellRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {

       Component c=
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);

      if(table.getRowHeight(row)<c.getPreferredSize().height){
            table.setRowHeight(row,    

c.getPreferredSize().height+table.getRowMargin());
        }

       return
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
   }
}

cheers
Daniel
alex@sibr.ru - 24 Nov 2006 04:14 GMT
> This almost worked, the row now becomes heigh enough to hold the whole
> text BUT the cell is always rendered with an empty line at the top,
> which means the last line is not visible. Any suggestions on how to
> fix this?

I'v faced the similar problem. When i put the html-text into table cell
which is using the DefaultTableCellRenderer (or my own JLabel-based
renderer), the displayed text begins from the the center of the cell,
with an empty space above it. Further, when the user manually resize
the column, the text aligns correctly. I've made an assumption, that
when JTable renders its cells for the first time, it doesn't take into
account the height of the cell. Row height is set in my program by
using the JTable.setRowHeight(int) method and is equal to all rows of
the table.

I spend three hours trying to solve a problem, and finally solve it by
put a JTable.setRowHeight(int) command into a
getTableCellRendererComponent() method of my renderer. So I think that
doing so is forcing the table to set row height at each render
operation. Looks ugly, but it works. So my renderer is looks in the
followng way:

   private class MyRenderer  extends DefaultTableCellRenderer {
       public Component getTableCellRendererComponent(...) {
           super.getTableCellRendererComponent(...);
           table.setRowHeight(55);
           return this;
       }
   }
alex@sibr.ru - 24 Nov 2006 05:25 GMT
sorry, in the code above there should be a condition:

if (table.getRowHeight()!=55) table.setRowHeight(55);


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.