Can you preserve the position of objects in the Java Swing panel?

I am currently planning a calculator bar using Java Swing. However, it is an extreme PAIN to align all buttons because they always resize and move when I add a new button or resize the button.

Is there a layout type or something that can "lock" the buttons in position so that they are not affected when I move / resize / add other buttons?

Thanks Bob

+1


a source to share


6 answers


Continuing what Tom said ...

To allow a component to become invisible, but keep its place, you can put it in a CardLayout along with an empty label and simply swap what is visible.

You can create a class to do it for you like this. Basically an example is shown where if you click on the button it removed while retaining its position. I insert showComponent / hideComponent and setVisible (t / f) - depends on the style you like.



This may not exactly answer what you are looking for, but it can be useful for part of your application.

public class Placeholder extends JPanel {
    private static final long serialVersionUID = 1L;
    private CardLayout cardLayout_;
    public Placeholder(JComponent component) {
        cardLayout_ = new CardLayout();
        setLayout(cardLayout_);
        add(component, "visible"); // the component
        add(new JLabel(), "hidden"); // empty label
    }
    public void showComponent() {
        cardLayout_.show(this, "visible"); 
    }
    public void hideComponent() {
        cardLayout_.show(this, "hidden"); 
    }
    public void setComponentVisible(boolean visible) {
        if (visible)
            showComponent();
        else
            hideComponent();
    }
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setLayout(new GridLayout(2,0));
        for (int n = 1; n < 10; n++) {
            JButton b = new JButton(n + "");
            final Placeholder placeholder = new Placeholder(b);
            f.add(placeholder);
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    placeholder.hideComponent();
                }
            });
        }
        f.pack();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

      

+3


a source


I suggest changing the visibility ( setVisible

) property instead of adding and removing buttons . Some layout managers ignore invisible components, so you may need to add a non-opqaue component (or a matching background) at the same position following the preferred / minimum / maximum dimensions of the original component. It could be easier to use OverlayLayout

.



+1


a source


You can use GridBagLayout to keep them in the grid, but allow them to be resized

0


a source


Expanding on what others have said ...

Instead of making components invisible, you can also disable them ...
Of course, this depends on your application and your preferences.

Besides the aforementioned layouts, there are some freely available such as TableLayout and the generic MigLayout.

0


a source


It probably depends on the development environment you are using.

In Netbeans, you can right click on a control container (form / panel) and choose from several layout options. Absolute Layout will allow you to position controls without automatically changing them, although it will prevent you from stacking controls on top of each other.

0


a source


I suggest you check out The Visual Guide for Layout Managers . Depending on the look and functionality you want, there are different layouts that might work for you. If the main issue is the resize operation (from the original JFrame o JPanel I assume), Spring Layout allows you to set constraints regarding the relative position of the components.

0


a source







All Articles