Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:39:23+00:00 2026-06-08T11:39:23+00:00

Title explains the question. How can I easily do that?

  • 0

Title explains the question. How can I easily do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:39:26+00:00Added an answer on June 8, 2026 at 11:39 am

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import java.util.List;
    import java.util.Arrays;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableHeaderHeightTest {
      private static int HEADER_HEIGHT = 32;
      private JTable makeTable() {
        JTable table = new JTable(new DefaultTableModel(2, 20));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        return table;
      }
      public JComponent makeUI() {
        JPanel p = new JPanel(new GridLayout(2,1));
    
        JTable table1 = makeTable();
        //Bad: >>>>
        JTableHeader header = table1.getTableHeader();
        //Dimension d = header.getPreferredSize();
        //d.height = HEADER_HEIGHT;
        //header.setPreferredSize(d); //addColumn case test
        header.setPreferredSize(new Dimension(100, HEADER_HEIGHT));
        p.add(makeTitledPanel("Bad: JTableHeader#setPreferredSize(...)", new JScrollPane(table1)));
        //<<<<
    
        JTable table2 = makeTable();
        JScrollPane scroll = new JScrollPane(table2);
        scroll.setColumnHeader(new JViewport() {
          @Override public Dimension getPreferredSize() {
            Dimension d = super.getPreferredSize();
            d.height = HEADER_HEIGHT;
            return d;
          }
        });
    //         //or
    //         table2.setTableHeader(new JTableHeader(table2.getColumnModel()) {
    //             @Override public Dimension getPreferredSize() {
    //                 Dimension d = super.getPreferredSize();
    //                 d.height = HEADER_HEIGHT;
    //                 return d;
    //             }
    //         });
        p.add(makeTitledPanel("Override getPreferredSize()", scroll));
    
        final List<JTable> list = Arrays.asList(table1, table2);
    
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(p);
        panel.add(new JButton(new AbstractAction("addColumn") {
          @Override public void actionPerformed(ActionEvent e) {
            for(JTable t: list) {
              t.getColumnModel().addColumn(new TableColumn());
              JTableHeader h = t.getTableHeader();
              Dimension d = h.getPreferredSize();
              System.out.println(d);
            }
          }
        }), BorderLayout.SOUTH);
        panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        return panel;
      }
      private static JComponent makeTitledPanel(String title, JComponent c) {
        JPanel p = new JPanel(new BorderLayout());
        p.add(c);
        p.setBorder(BorderFactory.createTitledBorder(title));
        return p;
      }
      public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            createAndShowGUI();
          }
        });
      }
      public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new TableHeaderHeightTest().makeUI());
        f.setSize(320, 320);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title pretty much explains the meat of the question. I'm debating on starting
The title pretty much explains the question. I have a user control loaded into
The question is basically contained in the title. Say you have an application that
Essentially the title of this question explains the essense of what I am trying
Well the title explains my whole question. What i want to archive, is a
The question title is a bit obscure so let me explain. A requirement that
First off I'm not sure that's the proper title for this question but hopefully
when I google the title of my question I get another stackoverflow question that
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
I think the question title seems to explain eveything. I want to detect whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.