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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:53:39+00:00 2026-05-16T18:53:39+00:00

I have a weird problem using the Java Box class. I am using JDK

  • 0

I have a weird problem using the Java Box class. I am using JDK 1.6.21.

I have an own class, DropDownPanel, that inherits from JPanel. The purpose of the DropDownPanel is to be able to hide and show another Component on it. If you click on the title of the DropDownPanel, the hosted Component (now it is a JTable) is set to visible or invisible. It works fine alone.

I am putting these DropDownPanels into a vertical Box class, then the Box into a JScrollPane. All X and Y alignments of the DropDownPanels are set to 0, so there should be no mis-alignment in the Box.

So I have the following logical tree: a JTable in a DropDownPanel in a Box in a JScrollPane.

The problem is that if the Box contains only the DropDownPanels, then I observe the following behaviour, when I add and remove rows from/to the table:

  • the JScrollPane is correctly shown after a lot of rows added, meaning that the sizes and the layouts are properly calculated. The scrollbar disappears after I remove enough rows, so the table fits the JFrame.
  • the JTable rows are refreshed properly, i.e. I see the correct number of rows.
  • But, JTable never seem to resize itself. Added rows are invisible, removed rows disappear, but the table background (with white color) is still visible. I would expect the JTable to be as compact as possible. It does not.

Now comes the weird thing. If I add an empty JPanel as the last component to the Box, everything works as I would expect. The JTable is always as compact as it can. When removing rows, the table size gets smaller and it is repainted correctly. Without the last JPanel however, the JTable resizes itself correclty (I know it from the appearance of the scrollbar), but never will be compact. Resizing the JFrame does not help. The only thing that forces the proper behaviour is to hide and show the JTable via the DropDownPanel. Then it is shown properly.

Since the DropDownPanel is an extended JPanel and the added JPanel is just the default JPanel, I have no clue, why it does not work without the extra JPanel at the end of the Box.

Finally here is the code. Note the commented line. If it is uncommented, the table behaves properly (better start with his setup), if not, then it is not. You need to click on the DropDownPanel title to show the table at all. Then you can add a lot of rows to see that the scrollpane appears after the number of rows do not fit into the JFrame and it disappears properly when they do. Still, the JTable is not as compact as it should be:

package tabletest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest {

  public class DropDownPanel extends JPanel implements ActionListener, MouseListener {
  private static final long serialVersionUID = 1L;
  protected JPanel header = new JPanel();
  protected JLabel titleLabel;
  protected Component content;
  protected boolean isExpanded = true;

  public DropDownPanel(String title, Component c) {
    content = c;
    setLayout(new BorderLayout());
    titleLabel = new JLabel(title);
    header.setLayout(new BorderLayout());
    header.add(titleLabel, BorderLayout.WEST);
    add(header, BorderLayout.NORTH);
    add(content, BorderLayout.CENTER);
    header.addMouseListener(this);
    titleLabel.addMouseListener(this);
    apply();
  }

  public void toggleExpanded() {
    isExpanded = !isExpanded;
    apply();
  }

  protected void apply() {
    titleLabel.setText("Drop state: " + (isExpanded ? "Expanded" : "Collapsed"));
    content.setVisible(isExpanded);
    setMaximumSize(new Dimension(1024, getPreferredSize().height));
    invalidate();
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    toggleExpanded();
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    toggleExpanded();
  }

  @Override
  public void mousePressed(MouseEvent e) {}
  @Override
  public void mouseReleased(MouseEvent e) {}
  @Override
  public void mouseEntered(MouseEvent e) {}
  @Override
  public void mouseExited(MouseEvent e) {}
  }

  public void run() {
  JFrame f = new JFrame();
  JPanel p = new JPanel();
  Box box = Box.createVerticalBox();
  p.setLayout(new BorderLayout());
  final JTable table = new JTable();
  table.setFocusable(false);
  table.setFillsViewportHeight(true);
  table.setBackground(Color.white);
  DefaultTableModel m = (DefaultTableModel) table.getModel();
  m.addColumn("Color");
  JButton b = new JButton("Remove row");
  b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    ((DefaultTableModel) table.getModel()).removeRow(0);
    table.invalidate();
    }
  });
  JButton b2 = new JButton("Add row");
  b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
    table.invalidate();
    }
  });
  DropDownPanel ddp = new DropDownPanel("Title", table);
  ddp.setAlignmentX(0);
  ddp.setAlignmentY(0);
  box.add(ddp);
  // ---------------------------------------------------------------
  // Without this line, it does not work; with this line, it is fine
  //box.add(new JPanel()); 
  // ---------------------------------------------------------------
  JLabel lll = new JLabel("End of Story");
  lll.setAlignmentX(0);
  lll.setAlignmentY(0);
  box.add(lll);
  p.add(new JScrollPane(box), BorderLayout.CENTER);
  p.add(b, BorderLayout.SOUTH);
  p.add(b2, BorderLayout.NORTH);
  f.add(p);
  ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
  ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
  ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
  ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
  ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
  f.pack();
  f.setVisible(true);
  f.setSize(new Dimension(600, 400));
  }

  public static void main(String[] args) {
  new TableTest().run();
  }
}

I am sweating blood now, so any help is appreciated 🙂

  • 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-05-16T18:53:39+00:00Added an answer on May 16, 2026 at 6:53 pm

    Within DropDownPanel you have added component i.e. JTable to center, So it takes all space available.
    Following is quoted from API doc:

    The components are laid out according
    to their preferred sizes and the
    constraints of the container’s size.
    The NORTH and SOUTH components may be
    stretched horizontally; the EAST and
    WEST components may be stretched
    vertically; the CENTER component may
    stretch both horizontally and
    vertically to fill any space left
    over.

    Also have look at this guide : A Visual Guide to Layout Managers

    I have changed your code. Hope this is what you are looking for :

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    public class TableTest {
    
        public class DropDownPanel extends JPanel implements ActionListener,
                MouseListener {
            private static final long serialVersionUID = 1L;
            //protected JPanel header = new JPanel();
            protected JLabel titleLabel;
            protected Component content;
            protected boolean isExpanded = true;
    
            public DropDownPanel(String title, Component c) {
                content = c;
                setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                titleLabel = new JLabel(title);
                //header.setLayout(new BorderLayout());
                //header.add(titleLabel, BorderLayout.NORTH);
                add(titleLabel);
                add(content);
                //header.addMouseListener(this);
                titleLabel.addMouseListener(this);
                apply();
            }
    
            public void toggleExpanded() {
                isExpanded = !isExpanded;
                apply();
            }
    
            protected void apply() {
                titleLabel.setText("Drop state: "
                        + (isExpanded ? "Expanded" : "Collapsed"));
                content.setVisible(isExpanded);
                //setMaximumSize(new Dimension(1024, getPreferredSize().height));
                invalidate();
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleExpanded();
            }
    
            @Override
            public void mouseClicked(MouseEvent e) {
                toggleExpanded();
            }
    
            @Override
            public void mousePressed(MouseEvent e) {
            }
    
            @Override
            public void mouseReleased(MouseEvent e) {
            }
    
            @Override
            public void mouseEntered(MouseEvent e) {
            }
    
            @Override
            public void mouseExited(MouseEvent e) {
            }
        }
    
        public void run() {
            JFrame f = new JFrame();
            JPanel p = new JPanel();
            Box box = Box.createVerticalBox();
            p.setLayout(new BorderLayout());
            final JTable table = new JTable();
            table.setFocusable(false);
            table.setFillsViewportHeight(true);
            table.setBackground(Color.white);
            DefaultTableModel m = (DefaultTableModel) table.getModel();
            m.addColumn("Color");
            JButton b = new JButton("Remove row");
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    ((DefaultTableModel) table.getModel()).removeRow(0);
                    table.invalidate();
                }
            });
            JButton b2 = new JButton("Add row");
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    ((DefaultTableModel) table.getModel())
                            .addRow(new Object[] { "Red" });
                    table.invalidate();
                }
            });
            DropDownPanel ddp = new DropDownPanel("Title", table);
            ddp.setAlignmentX(0);
            ddp.setAlignmentY(0);
            box.add(ddp);
            // ---------------------------------------------------------------
            // Without this line, it does not work; with this line, it is fine
            // box.add(new JPanel());
            // ---------------------------------------------------------------
            JLabel lll = new JLabel("End of Story");
            lll.setAlignmentX(0);
            lll.setAlignmentY(0);
            box.add(lll);
            p.add(new JScrollPane(box), BorderLayout.CENTER);
            p.add(b, BorderLayout.SOUTH);
            p.add(b2, BorderLayout.NORTH);
            f.add(p);
            ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
            ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
            ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
            ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
            ((DefaultTableModel) table.getModel()).addRow(new Object[] { "Red" });
            f.pack();
            f.setVisible(true);
            f.setSize(new Dimension(600, 400));
        }
    
        public static void main(String[] args) {
            new TableTest().run();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird problem. I'm using pscp.exe from within a C# program (with
I have weird problem. I'm using Eclipse for writing J2EE (java, jsf, javascript) application
I have this weird problem in java when trying to fetch records from MYSql
I'm fairly new to using MSSQL and have run into a weird problem. Given
I'm using VS10 and C# and I've encountered a weird problem. I have 2
I have a weird problem with a Java Gregorian Calendar: SimpleDateFormat sdf = new
I have a weird problem, I am using print_r($obj) in Joomla YOOtheme ZOO extension
I have a class PanelFormes that extends JPanel, which I use as a container.
I have a very weird problem using OpenMP in my C++ code: void update(double
I have a weird problem. While running my Android application, I receive Exception: java.lang.ClassCastException:

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.