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 8502523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:30:33+00:00 2026-06-11T01:30:33+00:00

I have a really hard design problem in my swing application. Generally Speaking, I

  • 0

I have a really hard design problem in my swing application. Generally Speaking, I have a JTable and a JLabel to display. The label is right below the table.

  • When the height of these two components doesn’t exceed a threshold, then the table and label displays as normal. Then height of these two components will increase as the number of records filled in the table increases.
  • When the height of these two components exceeds a threshold, I want there is a scroll bar in the table and the height of these two components will be the threshold.

Is this design Possible?

  • 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-11T01:30:35+00:00Added an answer on June 11, 2026 at 1:30 am

    Swing layout don’t automatically re-layout when a preferred size changes, you’ll have to call revalidate (best probably with a TableModelListener which checks if the row count has changed). You also need a layout which sizes the component according to its preferred size, BorderLayout does that for the component in the borders (but not for the one in the center. For a different manager you’ll need GridBag or better Mig-/Form- or Table (third party) Layout

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class TestTableLayout {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    final JPanel panel = new JPanel(new BorderLayout());
                    panel.setPreferredSize(new Dimension(400, 400));
    
                    final DefaultTableModel model = new DefaultTableModel(0, 1);
                    JTable table = new JTable(model) {
                        @Override
                        public Dimension getPreferredScrollableViewportSize() {
    // view port size should be the same as the preferred size
    // limited to the height threshold
                            Dimension size = super.getPreferredSize();
                            size.height = Math.min(size.height, 100);
                            return size;
                        }
                    };
    
                    new Timer(1000, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            model.addRow(new Object[] { "Extra row!" });
    // trigger a relayout of the panel
                            panel.revalidate();
                            panel.repaint();
                        }
                    }).start();
    
                    panel.add(new JScrollPane(table), BorderLayout.PAGE_START);
    
                    JLabel label = new JLabel("my label");
                    label.setVerticalAlignment(JLabel.TOP);
                    panel.add(label, BorderLayout.CENTER);
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(panel);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really hard time searching for this, because I have no idea
I have a really hard time understanding routes and I hope someone can help
In software companies I have seen it's really hard to work on very large
I am having a really hard time getting this to work. I have two
I am really having a hard time here. I need to design a Desktop
Yes, I know that UndecidableInstances can be bad. I really tried hard to design
I have already created quite a nice android application. I worked hard on the
My question is related class design. It is really hard to explain everything here
I'm going through a really hard time finding the answer to the question below,
I'm having a really hard time with the FlowLayoutPanel. I have a Winforms app

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.