Hide a button from Layout in Java Swing

前端 未结 4 1848
小蘑菇
小蘑菇 2020-12-18 15:04

I am trying something very basic: I have a list of 5 buttons. They are in a FlowLayout and the general idea should be that once I click one it should disappear and the other

相关标签:
4条回答
  • 2020-12-18 15:27

    Works fine for me.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FlowLayoutInvisible extends JFrame
        implements ActionListener
    {
        JPanel north;
        int i;
    
        public FlowLayoutInvisible()
        {
    
            north = new JPanel();
    
            for (int i = 0; i < 5; i++)
            {
                JButton button = new JButton("North - " + i);
                button.addActionListener(this);
                north.add(button);
            }
    
            getContentPane().add(north, BorderLayout.NORTH);
            }
    
        public void actionPerformed(ActionEvent e)
        {
            Component c = (Component)e.getSource();
            c.setVisible(false);
        ((JPanel)c.getParent()).revalidate();
        }
    
        public static void main(String[] args)
        {
            FlowLayoutInvisible frame = new FlowLayoutInvisible();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }
    

    If you need more help post your SSCCE.

    Update: I don't know if the revalidate() is required. I seemed to have a problem once but now I can't duplicate the problem.

    0 讨论(0)
  • 2020-12-18 15:39

    You could override each button's getPreferredSize() methods (and possibly getMinimumSize() as well to return 0,0 when the component is invisible; and you need to call, I think, invalidate() (or revalidate or validate, I can never keep them straight) on the container.

    0 讨论(0)
  • 2020-12-18 15:49

    Just remove it:

     panel.remove( button );
    

    What's wrong with this option?

    Layout managers are thought precisely to avoid having the "user" to make tricks in order to have each component it the right place ( although it seems to provoke the opposite effect )

    Removing the button from the panel will have the effect of laying out again all the remaining components. That's why it's name is "Layout manager" it manages to layout the components for you.

    0 讨论(0)
  • 2020-12-18 15:51

    I see two possibilities:

    • Write your own layout manager that listens for changes to its children's visible property - shouldn't be too hard, you can probably subclass FlowLayout to do it.
    • actually remove the clicked-button from the panel and, if necessary, re-add it later.
    0 讨论(0)
提交回复
热议问题