How can I generate labels and buttons in GUI?

Answered

Hello guys, Im new with Intellij and Java programming and I got a question.

Id like to generate number of i-checkboxes with different text each. How can i do that? I think that I just need a loop which would generate me those checkboxes but im not sure where to write in Intellij. I know that GUI is based on XML, and I have no idea where could i place such a loop to make everything visible etc. Also - if I want to create JLabel with text from other function, string... where can I do that? 

My code for this gui looks like this rn: 

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DeleteSubjectMenu extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;

public DeleteSubjectMenu() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}

public void main(String[] args) {
DeleteSubjectMenu dialog = new DeleteSubjectMenu();
dialog.pack();
dialog.setVisible(true);
System.exit(0);

}
}
0
1 comment

You will need to create the components manually like you would do in a plain Java Swing application that is not using any GUI designer, then you can add the dynamically created components to the container (JPanel) that was created by the GUI designer initialization code.

There is an example at ftp://ftp.intellij.net/pub/.idea/JTableSample.zip that shows how JTable can be created dynamically.

0

Please sign in to leave a comment.