I am having some issues trying to format JComponent locations inside of a dialog. I feel like this is a question that has an answer already but I had some issues finding information.
I've tried using setLocation on the elements, using a custom JOptionPane/JDialog (not sure if I did those correctly though) but they always ignore the formatting of the positions. and all the elements come out in a horizontal line
Ideally I would like the class names at the top, the 3 member lists in the middle and the button on the bottom. I am creating the dialog like so:
JPanel createClass = new NewClass();
int result = JOptionPane.showConfirmDialog(GUI.this,
createClass, "Create a class",JOptionPane.OK_CANCEL_OPTION);
and NewClass()(extends JPanel) looks like this:
//initializes the values
className = new JTextField(10);
superName = new JTextField(10);
publicMem = new DefaultListModel<String>();
protectedMem = new DefaultListModel<String>();
privateMem = new DefaultListModel<String>();
publicMem.addElement("Test");
publicMem.addElement("Test");
pubMemList = new JList(publicMem);
protMemList = new JList(protectedMem);
privMemList = new JList(privateMem);
pubMemList.setLocation(0,0);
newMember = new ButtonController(memberCommand);
//add Components to the JPanel
add(new JLabel("Class Name:"));
add(className);
add(Box.createHorizontalStrut(15));
add(new JLabel("Super Class Name:"));
add(superName);
add(new JLabel("\n"));
add(new JLabel("Public Members :"));
add(pubMemList);
add(new JLabel("Protected Members :"));
add(protMemList);
add(new JLabel("Private Members :"));
add(privMemList);
add(newMember);
So if anyone could point me in the right direction or to another similar post that would be greatly appreciated.
"Ideally I would like the class names at the top, the 3 member lists in the middle and the button on the bottom"
You could use a BorderLayout
to achieve it. The following snippet may help you to get started:
setLayout(new BorderLayout());
//now add 3 panel as containers for top, center and bottom content
JPanel top = new JPanel();
add(top, BorderLayout.NORTH);
JPanel center = new JPanel();
add(center, BorderLayout.CENTER);
JPanel bottom = new JPanel();
add(bottom, BorderLayout.SOUTH);
//manage layout and add content to top container
top.setLayout(new FlowLayout());//actually it the default
top.add(new JLabel("Class Name:"));
JTextField className = new JTextField(10);
top.add(className);
top.add(new JLabel("Super Class Name:"));
JTextField superName = new JTextField(10);
top.add(superName);
//todo manage layout and add content to center container
//todo manage layout and add content to bottom container