开发者

Java Box (BoxLayout) does not work as expected

开发者 https://www.devze.com 2023-01-15 15:10 出处:网络
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

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) {
    开发者_C百科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 :)


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();
    }
}


Normally a JTable's direct parent should be a JScrollPane, you should add the table this way:

DropDownPanel ddp = new DropDownPanel("Title", new JScrollPane(table));
0

精彩评论

暂无评论...
验证码 换一张
取 消