Google Tag Manager

2012/06/21

Height of JTableHeader

Code

JScrollPane scroll = new JScrollPane(table);
scroll.setColumnHeader(new JViewport() {
  @Override public Dimension getPreferredSize() {
    Dimension d = super.getPreferredSize();
    d.height = 32;
    return d;
  }
});
//or
table.setTableHeader(new JTableHeader(table.getColumnModel()) {
  @Override public Dimension getPreferredSize() {
    Dimension d = super.getPreferredSize();
    d.height = 32;
    return d;
  }
});

References

8 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. The original message:
      > final TableCellRenderer tcrOs = myTable.getTableHeader().getDefaultRenderer();
      > myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {

      > @Override
      > public Component getTableCellRendererComponent(JTable table,
      > Object value, boolean isSelected, boolean hasFocus,
      > int row, int column) {
      > JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(table,
      > value, isSelected, hasFocus, row, column);
      > lbl.setForeground(AppVariables.textColor);
      > lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(),
      > BorderFactory.createEmptyBorder(0, 5, 0, 0)));
      > lbl.setHorizontalAlignment(SwingConstants.LEFT);
      > if (isSelected) {
      > lbl.setForeground(Color.red);
      > lbl.setBackground(Color.lightGray);
      > } else {
      > lbl.setForeground(Color.blue);
      > lbl.setBackground(Color.black);
      > }
      > return lbl;
      > }
      > });

      > I have tried this code for JTable header for alignment. may be helpful for you.

      I just removed the link. thanks.

      Delete
  2. @Fred, thanks for the info.
    If there is no possibility to change the Look and Feel, it's a simple way(NullPointerException occurs when you change the LnF).

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. java hello teacher, thank you for your contributions to all the world who want aprener over java, I want to ask something, is it possible to apply the rounded edge effect to a JComboBox? you can please post thank you very much

    ReplyDelete
  5. @HANNIBAL T, Thank you for your request. I post Java Swing Tips: Rounded corner JComboBox border(Only tested BasicLookAndFeel, WindowsLookAndFeel).

    ReplyDelete
  6. Awesome.... its work. thanks

    ReplyDelete