1

Here is a screenshot of my JFrame. This will be the main window to my application.

enter image description here

So the problem is that all the buttons are inline with each other, whereas I want them to be one under the other i.e. Add Contact under Show Contacts.

So how can I do that?

Here is my code for the JFrame.

public class CRUDFrame extends JFrame {
    public CRUDFrame(){
        super("AppCRUD");
        setLayout(new FlowLayout());
        JButton button1, button2, button3, button4;

        button1 = new JButton(" Show Contacts ");
        button2 = new JButton(" Add Contact ");
        button3 = new JButton(" Update Number in a Contact ");
        button4 = new JButton(" Delete a Contact ");
        add(button1);       
        add(button2);  
        add(button3); 
        add(button4);
    }
}
`
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
prometheuspk
  • 3,754
  • 11
  • 43
  • 58
  • I think the answer lies in the LayoutManagers. but i can't understand them at all. If there is a **really** good tutorial that you may know of, please post link. – prometheuspk Apr 25 '11 at 08:40

3 Answers3

5

There have been some good answers centered around 'use a layout'. This example espouses the same advice, but also introduces the concept of nesting one layout within another. E.G. the JPanel containing the JButtons has a GridLayout. That panel is placed in the NORTH of a panel that is then added to the WEST of the main 'gui' panel.

The other components are added in order to show how the column of buttons might go together with other components in the main user interface.

Contact.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class Contact {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(5,5));
                gui.setBorder( new EmptyBorder(3,3,3,3) );

                JPanel controls = new JPanel( new BorderLayout(5,5) );

                JPanel buttons = new JPanel(new GridLayout(0,1,4,4));
                buttons.add( new JButton("Show") );
                buttons.add( new JButton("Add") );
                buttons.add( new JButton("Update Number") );
                buttons.add( new JButton("Delete") );
                buttons.setBorder( new TitledBorder("Contact") );

                controls.add( buttons, BorderLayout.NORTH );

                controls.add(new JScrollPane(new JTree()), BorderLayout.CENTER);

                gui.add(controls, BorderLayout.WEST);

                gui.add(new JTextArea("CardLayout for CRUD components.",10,30));

                gui.add(new JLabel("Output label.."), BorderLayout.SOUTH);

                JToolBar toolbar = new JToolBar();
                toolbar.add(new JCheckBox("Auto save", true));
                toolbar.add(new JCheckBox("Always On Top"));
                gui.add(toolbar, BorderLayout.NORTH);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Screenshot

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • BTW - If you found that useful, you might also be interested in this other [example of a nested layout](http://stackoverflow.com/questions/5621338/about-swing-and-jtable/5630271#5630271). It allows the PLAF to be changed at run-time, and demonstrates dynamic addition of `JLabel` instances. – Andrew Thompson Apr 25 '11 at 13:23
  • Why did you use `JPanel` instead of `JFrame`? – prometheuspk Apr 26 '11 at 08:12
  • A `JPanel` can be put into a `JFrame`, or a `JApplet`, or a `JOptionPane`, or a `JInternalFrame`, or in a layout constraint within another `JPanel`, or.. It is all about choice, and leaving the options wide open. As to why I put the `JPanel` into a `JOptionPane` rather than a `JFrame` in that code, it comes down to the `JOptionPane` requiring less lines of code. ;) – Andrew Thompson Apr 26 '11 at 08:55
1

Use box layout and set dimension for each button. Check the below link.

http://download.oracle.com/javase/tutorial/uiswing/layout/box.html

AbhiRoczz...
  • 254
  • 2
  • 13
  • +1 good answer to the problem. Other alternatives standard Java are: GridLayout, GridBagLayout. Non-standard: TableLayout will help for sure, and I think that also MiGLayout might do the trick. – Boro Apr 25 '11 at 08:54
  • 1
    I'll realy prefer the standard `GridLayout`, because it will automatically **adjust** the size of each component to match them. This is better than manually setting the dimensions of each button. – Alba Mendez Apr 25 '11 at 10:19
1

Take a look at http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html which discusses the various layout managers that Swing offers. GridBagLayout is probably what you need.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118