JTable Row Height Problem

Discussion in 'Java' started by Edsoncv, Feb 19, 2008.

  1. Edsoncv

    Edsoncv Guest

    I'm having the following problem while configuring a JTable row
    height.
    The fact is when my row height does not have the exactly heigth of
    "row header", exactly figure bellow:

    http://www.crionics.com/products/opensource/faq/swing_ex/images/table4.gif

    The line height seems almost the same but this little difference
    increases as the # of rows increase or the look and feel is changed.

    My code is very similar like this:

    /* (swing1.1beta3) */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;


    /**
    * @version 1.0 11/09/98
    */

    class RowHeaderRenderer extends JLabel implements ListCellRenderer {

    RowHeaderRenderer(JTable table) {
    JTableHeader header = table.getTableHeader();
    setOpaque(true);
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    setHorizontalAlignment(CENTER);
    setForeground(header.getForeground());
    setBackground(header.getBackground());
    setFont(header.getFont());
    }

    public Component getListCellRendererComponent( JList list,
    Object value, int index, boolean isSelected, boolean
    cellHasFocus) {
    setText((value == null) ? "" : value.toString());
    return this;
    }
    }

    public class RowHeaderExample extends JFrame {

    public RowHeaderExample() {
    super( "Row Header Example" );
    setSize( 300, 150 );

    ListModel lm = new AbstractListModel() {
    String headers[] = {"a", "b", "c", "d", "e", "f", "g", "h",
    "i"};
    public int getSize() { return headers.length; }
    public Object getElementAt(int index) {
    return headers[index];
    }
    };

    DefaultTableModel dm = new DefaultTableModel(lm.getSize(),10);
    JTable table = new JTable( dm );
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JList rowHeader = new JList(lm);
    rowHeader.setFixedCellWidth(50);

    rowHeader.setFixedCellHeight(table.getRowHeight()
    + table.getRowMargin());
    // + table.getIntercellSpacing().height);
    rowHeader.setCellRenderer(new RowHeaderRenderer(table));

    JScrollPane scroll = new JScrollPane( table );
    scroll.setRowHeaderView(rowHeader);
    getContentPane().add(scroll, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
    RowHeaderExample frame = new RowHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    System.exit(0);
    }
    });
    frame.setVisible(true);
    }
    }
     
    Edsoncv, Feb 19, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,349
    manusa
    Apr 12, 2007
  2. Scott Steiner

    jtable: how to set row height to zero?

    Scott Steiner, Jun 28, 2005, in forum: Java
    Replies:
    0
    Views:
    674
    Scott Steiner
    Jun 28, 2005
  3. W@cek
    Replies:
    0
    Views:
    1,625
    W@cek
    Mar 31, 2005
  4. Tintin92
    Replies:
    1
    Views:
    1,716
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    332
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page