Reputation: 8786
I am trying to add a model to a JTable, which was created using IntelliJ Forms. As of right now, the main method has to be static, and if I make the JTable static as well, then IntelliJ says it cannot bind the JTable. I am confused on how I can add the model in this case.
public class DisplaySettings {
private JTable resolutionsTable;
private JPanel displaySettings;
public static void main(String[] args) {
JFrame frame = new JFrame("Display Settings");
frame.setContentPane(new DisplaySettings().displaySettings);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
String[] columns = {"Resolution Size"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
resolutionsTable.setModel(model);
}
}
Upvotes: 0
Views: 2349
Reputation: 8786
When you are dealing with IntelliJ Forms, they are automatically handled and allocated for by IntelliJ, by default. If you select the component you are working with in the ComponentTree
, in the .form
GUI Editor, there is an option called Custom Create
. Check that.
Once that is checked, IntelliJ will automatically create a method called createUIComponents()
. There you can allocate your JTable
and set the model, since this method is not in a static context. This method will be automatically called when creating the UI.
Upvotes: 1
Reputation: 31968
Using the following works for me -
JTable resolutionsTable = new JTable(); // instances of both JTable and JPanel
JPanel displaySettings = new JPanel();
... // you can set the above component with diff attributes
frame.setContentPane(displaySettings);
... // and use them further
resolutionsTable.setModel(model);
Upvotes: 0