7 Replies Latest reply: Apr 24, 2012 11:43 PM by DarrylBurke RSS

    JTable: Making Table-Cell display text vertical

    930622
      Hi Forum.

      I need som help by making a CellRenderer that Rotates the Text making it vertical instead of horisontal.

      Have been trying defferent approaches with only limited success.

      At first I tried formatting into HTML, eg.
      private String getHtmlFormattedString(String value) {
        f (value == null) {
          return null;
        }
      
        StringBuffer buf = new StringBuffer("<html>");
        char[] chars = value.toCharArray();
      
        for (int i = 0; i < chars.length; i++) {
          char c = chars;
      buf.append(c).append("<br/>");
      }

      buf.append("</html>");
      return buf.toString();

      }
      This works fairly well - but the Orientation of the single Char is wrong, and it does not work well when the value is a Time.
      Here 09:12 is translated into:
      0
      9
      :
      1
      2
      
      Edited by: 927619 on 2012-04-14 00:16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: JTable: Making Table-Cell display text vertical
          camickr
          Check out Vertical Table Header Cell Renderer:

          http://tips4java.wordpress.com/2009/03/06/vertical-table-header-cell-renderer
          • 2. Re: JTable: Making Table-Cell display text vertical
            930622
            Regarding the link: http://tips4java.wordpress.com/2009/03/06/vertical-table-header-cell-renderer

            This works fine for TableHeaders but not for TableCells.

            Are there any other suggestings - have been googlet for some time now.
            • 3. Re: JTable: Making Table-Cell display text vertical
              DarrylBurke
              Download the class and its dependencies and develop your own VerticalTableCellRenderer using the same concepts.

              You might have to walk the model and set the row heights for the table (unless you set a common row height that can definitely accommodate all data that you might throw into the cell).

              Make an effort and if you can't get things to work, post a SSCCE with a clear description of your problem.

              db
              • 4. Re: JTable: Making Table-Cell display text vertical
                847034
                Hello,
                in cell renderer, try to use the below code,


                BufferedImage bi = new BufferedImage(maxHeight, maxWidth,
                BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = (Graphics2D) bi.getGraphics();


                if (clockwise) {
                g.rotate(Math.PI / 2);
                } else {
                g.rotate(-Math.PI / 2);
                g.translate(-bi.getHeight(), bi.getWidth());
                }
                g.drawString(stringToBeAddedInCell, 5, -5);


                try this in the renderer. and as said before you want to maintain the rowHeight value.

                Thanks.
                • 5. Re: JTable: Making Table-Cell display text vertical
                  DarrylBurke
                  cool wrote:
                  Hello,
                  in cell renderer, try to use the below code,


                  BufferedImage bi = new BufferedImage(maxHeight, maxWidth,
                  BufferedImage.TYPE_INT_ARGB);
                  Graphics2D g = (Graphics2D) bi.getGraphics();


                  if (clockwise) {
                  g.rotate(Math.PI / 2);
                  } else {
                  g.rotate(-Math.PI / 2);
                  g.translate(-bi.getHeight(), bi.getWidth());
                  }
                  g.drawString(stringToBeAddedInCell, 5, -5);


                  try this in the renderer. and as said before you want to maintain the rowHeight value.

                  Thanks.
                  1. Announcements at the top of the forum listing are meant to be read. Clearly, you didn't bother.
                  2. Where does a BufferedImage come into it?

                  db
                  • 6. Re: JTable: Making Table-Cell display text vertical
                    930622
                    Found the solution using VerticalLabelUI. Really simple actually.

                    Also you have to set the rowHeight of the row(s) where this render is applied to.
                    public class VerticalTableCellRenderer extends DefaultTableCellRenderer {
                    
                         public VerticalTableCellRenderer() {
                              setUI(new VerticalLabelUI());
                         }
                    
                         @Override
                         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                              Component def = table.getDefaultRenderer(String.class).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                              Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                              c.setBackground(def.getBackground());
                              return c;
                         }
                    
                    }
                    • 7. Re: JTable: Making Table-Cell display text vertical
                      DarrylBurke
                      Glad you got it to work. The call to<tt> Component def = ... </tt> is dangerously susceptible to infinite recursion, and shouldn't be necessary. The background should be correctly set by the super invocation, did you find that it isn't?

                      db

                      edit Somewhere in the interaction of DefaultTableCellRenderer and VerticalLabelUI, the initial background setting is being lost. Couldn't trace it out, but setting it explicitly works.

                      This is a better solution than the one you presently have. Note that no classes need be extended with this approach.
                          // make the second column vertical
                          final int COL = 1;
                          DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                          renderer.setUI(new VerticalLabelUI());
                          renderer.setBackground(table.getBackground());
                          renderer.setForeground(table.getForeground());
                          table.getColumnModel().getColumn(COL).setCellRenderer(renderer);
                          int height = 0;
                          for (int i = 0; i < table.getRowCount(); i++) {
                            int h = renderer.getTableCellRendererComponent(table, table.getValueAt(i, COL),
                                    true, true, i, COL).getPreferredSize().height;
                            height = h > height ? h : height;
                          }
                          table.setRowHeight(height);
                      edit2: DefaultTableCellRenderer#updateUI() has these two lines:
                           setForeground(null);
                           setBackground(null);
                      In view of this, I've added a setForeground(...) call in the code above.

                      Edited by: Darryl Burke -- added code sample and explanation