Reputation: 12803
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 ?
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
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:
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:
"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