tags:

views:

957

answers:

6

I am currently desigining a calculator panel using Java Swing. However, it's an extreme PAIN to line up all of the buttons because they are always resizing and repositioning themseleves whenever I add a new button or change the size of a button.

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

Thanks, Bob

A: 

You could use GridBagLayout to keep them in a grid, but allow them to resize

+1  A: 

I suggest instead of adding and removing buttons, you change their visibility property (setVisible). Some layout manager ignore non-visible components, so you may need to add a non-opqaue (or matching background) component in the same position that follows the preferred/minimum/maximum sizes of the original component. More simple you may want to use OverlayLayout.

Tom Hawtin - tackline
+1  A: 

Extending what Tom said...

To allow a component to become invisible yet hold its place, you can place it in a CardLayout along with an empty label and just swap which is visible.

You can create a class to do this for you as follows The main just shows an example where if you click a button it's deleted while retaining its position. I put in showComponent/hideComponent and setVisible(t/f) - depends on the style you like.

This might not exactly answer what you're looking for, but might be a useful piece 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);
    }
}
Scott Stanchfield
A: 

Extending what the others said...

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

Beside the above mentioned layouts, there are some freely available ones, like TableLayout and the versatile MigLayout.

PhiLho
A: 

It probably depends on the development environment you are using.

In Netbeans you can right-click the control container (form/panel) and select from a number of layout options. 'Absolute Layout' will let you position the controls without them all being resized automatically, although it won't let you place controls on top of each other.

A: 

I suggest you to check A Visual Guide to Layout Managers. Depending on the look and functionality you want there are different layouts that could work for you. If the main problem is the resize operation (of the parent JFrame o JPanel I assume), the Spring Layout allows you to establish constraints regarding the relative place of components.

Daniel H.