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

Tip: Looking for answers? Try searching our database.

Setting table row height according the renderer component

Thread view: 
noah.drach@gmail.com - 20 Dec 2006 09:52 GMT
I have a table that all it's cells are rendered, I want to set the row
height accoding the height of the tallest component.
and I also want the component height to be set automatically after I
set it's width to the column width.

any ideas?
Andrew Thompson - 20 Dec 2006 10:18 GMT
> I have a table that all it's cells are rendered, I want to set the row
> height accoding the height of the tallest component.
> and I also want the component height to be set automatically after I
> set it's width to the column width.

Change the '.' to a comma in line 243 of the source,
that should sort the problem.  In case it does not,
you might also..

> any ideas?

Show us an SSCCE* that fails to correctly size the
column headers.
<http://www.physci.org/codes/sscce>

Andrew T.
noa - 20 Dec 2006 13:37 GMT
> > I have a table that all it's cells are rendered, I want to set the row
> > height accoding the height of the tallest component.
[quoted text clipped - 12 lines]
>
> Andrew T.

Here, notice that the end of a string is marked "end of text"
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TableDemo extends JPanel {

   public TableDemo() {
       super(new GridLayout(1,0));

       JTable table = new JTable(new MyTableModel());

       // Set renderers
       TableColumn col = table.getColumnModel().getColumn(0);
       col.setCellRenderer(new MyRenderer());

       col = table.getColumnModel().getColumn(1);
       col.setCellRenderer(new MyRenderer());

       col = table.getColumnModel().getColumn(2);
       col.setCellRenderer(new MyRenderer());

       table.setPreferredScrollableViewportSize(new Dimension(500,
70));

       //Create the scroll pane and add the table to it.
       JScrollPane scrollPane = new JScrollPane(table);

       //Add the scroll pane to this panel.
       add(scrollPane);
   }
   class MyRenderer extends  DefaultTableCellRenderer {
       private JTextPane component = new JTextPane();
       public MyRenderer() {
           component.setOpaque(true);
       }

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

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

           component.setText((String)value);

           // Set the size
           TableColumnModel columnModel = table.getColumnModel();

           component.setMinimumSize(new
Dimension(columnModel.getColumn(column).getWidth(),
(int)component.getPreferredSize().getHeight()));
           component.setMaximumSize(new
Dimension(columnModel.getColumn(column).getWidth(),
(int)component.getPreferredSize().getHeight()));

           if (component.getPreferredSize().getHeight() >
table.getRowHeight(row)){
               table.setRowHeight(row,
(int)component.getPreferredSize().getHeight());
           }

           return component;
       }
  }

   class MyTableModel extends AbstractTableModel {
       private String[] columnNames = {"First Name",
                                       "Last Name",
                                       "Sport",
                                       "# of Years",
                                       "Vegetarian"};
       private Object[][] data = {
           {"Mary\nMary end of text", "Campione blahblahblah
blahblahblahblah  blahblahblahblahblah  blahblahblahblahblah end of
text",
            "Snowboarding\nSnowboarding\nend of text", new Integer(5),
new Boolean(false)},
            {"Mary end of text", "Campione blahblahblah
blahblahblahblah  blahblahblahblahblah  blahblahblahblahblah
blahblahblahblahblah  blahblahblahblahblah  blahblahblahblahblah
blahblahblahblahblah  blahblahblahblahblah  blahblahblahblahblah
blahblahblahblahblah  blahblahblahblahblah  blahblahblahblahblah
blahblahblahblahblah end of text",

"Snowboarding\nSnowboarding\nSnowboarding\nSnowboarding\nSnowboarding\nSnowboarding\nSnowboarding\nend
of text", new Integer(5), new Boolean(false)}};

       public Object getValueAt(int row, int col) {
           return data[row][col];
       }

       public int getRowCount()  {
         return 2;
       }

       public int getColumnCount()  {
         return 5;
       }
  }

   private static void createAndShowGUI() {
       //Create and set up the window.
       JFrame frame = new JFrame("TableDemo");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Create and set up the content pane.
       TableDemo newContentPane = new TableDemo();
       newContentPane.setOpaque(true); //content panes must be opaque
       frame.setContentPane(newContentPane);

       //Display the window.
       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }
}
Oliver Wong - 20 Dec 2006 20:18 GMT
>> > I have a table that all it's cells are rendered, I want to set the row
>> > height accoding the height of the tallest component.
[quoted text clipped - 14 lines]
>
> Here, notice that the end of a string is marked "end of text"
[code snipped]

   Works for me. I'm using Java 1.6.0.

   - Oliver
noa - 21 Dec 2006 08:20 GMT
> >> noah.dr...@gmail.com wrote:
> >> > I have a table that all it's cells are rendered, I want to set the row
[quoted text clipped - 19 lines]
>
>     - Oliver- Hide quoted text -- Show quoted text -
1.I have to use 1.4*
2. are you sure it works? you saw the "end of text" string at the end
of every text cell?
noa - 21 Dec 2006 10:41 GMT
> > >> noah.dr...@gmail.com wrote:
> > >> > I have a table that all it's cells are rendered, I want to set the row
[quoted text clipped - 22 lines]
> 2. are you sure it works? you saw the "end of text" string at the end
> of every text cell?

I think the row height is set by the last rendered cell in the row,
which means that row height doesn't really change between the calls to
the cell renderers in that row.
Does it make sense?

Noa
Oliver Wong - 21 Dec 2006 15:36 GMT
>> > Here, notice that the end of a string is marked "end of text"[code
>> > snipped]
[quoted text clipped - 4 lines]
> 2. are you sure it works? you saw the "end of text" string at the end
> of every text cell?

   No, I only saw "end of text" for the cell with the snowboards, not the
cell with the "blah blah blah", so it doesn't actually work.

   The problem is that you're using getPreferredSize() to try to determine
the height of the cells. In the case of
"Snowboard\nSnowboard\nSnowboard\nend of text" (or whatever), your text is 4
lines long, and so the preferred size has a height such that it's possible
to display 4 lines. But your "Bla bla bla bla bla bla" is just 1 super long
line, and so the preferred size is something like "width = 500px, height =
20px", i.e. a super wide, but short, rectangle.

   Since in your code, you only keep the maximum height, the height for the
Snowboard text is kept, and thus the height is only big enough for 4 lines,
not for the 10 lines of your bla bla bla text.

   It looks like you might have to perform line wrapping calculations
manually to get the effect you want.

   - Oliver
noa - 21 Dec 2006 15:58 GMT
> >> > Here, notice that the end of a string is marked "end of text"[code
> >> > snipped]
[quoted text clipped - 22 lines]
>
>     - Oliver

I thought that a JTextPane wraps automatically, so if i limit it's
width the height is calculated automatically.
I was wrong?
Oliver Wong - 21 Dec 2006 19:01 GMT
> I thought that a JTextPane wraps automatically, so if i limit it's
> width the height is calculated automatically.
> I was wrong?

   You're confusing the actual width with the preferred width. There's a
certain size the control *prefers* to be, and in the case of the control you
chose, it's preferred size will be such that no word wrapping occurs. If you
manually set the width, it'll perform word wrapping, but you won't find out
what pixel height is required to display these words by querying the
preferred height.

   - Oliver
noa - 24 Dec 2006 08:32 GMT
> > I thought that a JTextPane wraps automatically, so if i limit it's
> > width the height is calculated automatically.
[quoted text clipped - 6 lines]
> what pixel height is required to display these words by querying the
> preferred height.

>     - Oliver

I don't understand, what am i supposed to make this work?

- Noa
RedGrittyBrick - 24 Dec 2006 12:12 GMT
>>> I thought that a JTextPane wraps automatically, so if i limit it's
>>> width the height is calculated automatically.
[quoted text clipped - 11 lines]
>
> - Noa

This looks as if it does what you want ...
http://www.roseindia.net/javatutorials/JTable_in_JDK.shtml
noa - 24 Dec 2006 15:11 GMT
On Dec 24, 2:12 pm, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
> >>> I thought that a JTextPane wraps automatically, so if i limit it's
> >>> width the height is calculated automatically.
[quoted text clipped - 11 lines]
>
> > - NoaThis looks as if it does what you want ...http://www.roseindia.net/javatutorials/JTable_in_JDK.shtml- Hide quoted text -- Show quoted text -

I know, I've worked with this solution but I need to work with the text
pane because I have styled text...
noa - 24 Dec 2006 15:14 GMT
> On Dec 24, 2:12 pm, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
> wrote:
[quoted text clipped - 15 lines]
> > > - NoaThis looks as if it does what you want ...http://www.roseindia.net/javatutorials/JTable_in_JDK.shtml-Hide quoted text -- Show quoted text -I know, I've worked with this solution but I need to work with the text
> pane because I have styled text...- Hide quoted text -- Show quoted text -

And it doesn't work the same

Noa
Oliver Wong - 27 Dec 2006 17:15 GMT
>> > I thought that a JTextPane wraps automatically, so if i limit it's
>> > width the height is calculated automatically.
[quoted text clipped - 11 lines]
>
> I don't understand, what am i supposed to make this work?

   I'd redesign the GUI so as not to need to do what you're trying to do.

   Failing that, I'd do custom text rendering myself, instead of using a
JTextPane. But you said you need style text, so that'll probably be
difficult. Good luck.

   - Oliver
noa - 28 Dec 2006 07:52 GMT
> >> > I thought that a JTextPane wraps automatically, so if i limit it's
> >> > width the height is calculated automatically.
[quoted text clipped - 19 lines]
>
>     - Oliver

10X.

I guess...

 - Noa


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.