John Joe
John Joe

Reputation: 12803

Need help in GUI

For button Back and button Delete, I have setBounds to (130, 120, 195,30); and (10, 190, 195,30); , but they still doesn't move to bottom.

What's wrong here ?

enter image description here

public deleteAdmin(int num)
    {
        super("Delete Admin");
        setBounds(100, 200, 340, 229);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        panel.setBounds(35, 19, 242, 146);
        contentPane.add(panel);

        JButton button = new JButton("Back");
        button.setBounds(130, 120, 195,30);
        panel.add(button);

        JButton bckButton = new JButton("Delete");
        bckButton.setBounds(10, 190, 195,30);
        panel.add(bckButton);

        adminAPI admin = new adminAPI();
        List<String>allName = null;
        try {
            allName= admin.displayName();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //System.out.println(allName);
        Object [] o1=allName.toArray();
        JCheckBox[] checkBoxList = new JCheckBox[num];
        System.out.println(allName);
         //JLabel[] names = new JLabel[num];
        for(int i = 0; i < num; i++) {   
            checkBoxList[i] = new JCheckBox(""+o1[i]);
            System.out.println(o1[i]);
           contentPane.add(checkBoxList[i]);

        }


    }

Upvotes: 2

Views: 109

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

The quick easy and wrong answer is that you're calling trying to do exact component placement into a container that uses a layout manager, that this only works if the component uses null layout, but again this is not a good solution as this leads to rigid GUI's that are very difficult to enhance, upgrade, and debug.

Your main problem is that you're trying to use setBounds(...) in the first place. Much better is to learn to use the layout managers and use them in smart ways to easily and efficiently place your components where you want them. Often you will want to nest JPanels, each using its own layout manager to help place things well.


For example this gui:

enter image description here

was created with this code:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class DeleteAdmin2 extends JPanel {
    private List<JCheckBox> checkBoxes = new ArrayList<>();

    public DeleteAdmin2() {
        JPanel topPanel = new JPanel(new GridLayout(1, 0, 5, 5));
        topPanel.add(new JButton("Back"));
        topPanel.add(new JButton("Delete"));

        String[] texts = { "A1", "B1", "C1", "D1", "E1", "A2", "B2", "C2", "D2", "E2" };
        JPanel checkBoxPanel = new JPanel(new GridLayout(0, 5, 5, 5));
        for (String text : texts) {
            JCheckBox checkBox = new JCheckBox(text);
            checkBoxes.add(checkBox);
            checkBoxPanel.add(checkBox);
        }

        setLayout(new BorderLayout(5, 5));
        add(topPanel, BorderLayout.PAGE_START);
        add(checkBoxPanel, BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Delete Admin");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DeleteAdmin2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

Some side recommendations:

  • It looks like you're wanting to pass a list of information into this class to display it and allow the user to delete administrators. If so, then the class's constructor parameter should be this list of administrators, not an int.
  • If this class is being used to display a JFrame then instead have it display a JDialog since the class looks to display an application sub-window, and in this situation dialog windows are much more appropriate. This also gives you the option of displaying the window in a modal fashion -- meaning you have the choice of displaying the window in such a way that the user must deal with the window before being allowed to interact with the main application window.
  • You'll find much more flexibility if you have your GUI classes either extend or create JPanels and not top-level windows such as JFrames.
  • If you are desiring to present this class with a list of administrators for possible deletion, consider displaying them in a JList rather than a collection of JCheckBoxes. The JList will make it easier for you to tell who has been selected, and its model can hold actual Administrator objects (if you have such a class).
  • Learn and follow Java naming conventions -- class names should start with upper-case letters and variable and method names with lower-case letters. Following this will make your code easier to read which will help those trying to help you should you ask future questions here.
  • Also, please improve your question title. The title should succinctly and informatively tell us what your problem is, and this: "Need help in GUI" tells us nothing that would help us to understand what's wrong. Instead use something like: "JButtons not being placed correctly in GUI", or something similar. Doing this will help get more eyeballs to your question which should lead to quicker and better answers.

Upvotes: 5

Related Questions