Issue with Initializing JPanel Components in IntelliJ's GUI Designer
I am using IntelliJ's GUI Designer to create a form with the following component hierarchy:
- JPanel (name: "loginPanel")
- JPanel (with additional components)
- JPanel (with additional components)
As per my understanding, the GUI Designer should automatically generate initialization code for these components in the associated .java file. However, when I run the application, the loginPanel (the topmost JPanel in the hierarchy) appears to be null. I have attempted to manually initialize the component, only to receive the following warning: "Assignment to UI-bound field will overwrite field generated by UI-Designer."
I'm relatively new to Swing, and I've been searching online for a solution to no avail. Could someone help me understand what might be going wrong in my approach, and how I can correctly initialize the JPanel components without encountering the warning?
package gui;
import ExceptionHandler.Exceptions.DatabaseConnectionException;
import Users.loggedUser;
import ExceptionHandler.ExceptionHandler;
import databaseHandlers.loginFormDatabaseHandlers;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
public class loginForm extends JDialog {
private JTextField txtEmail;
private JPasswordField txtPassword;
private JButton btnSubmit;
private JPanel loginPanel;
private JButton btnExit;
private loginFormDatabaseHandlers dbHandler = new loginFormDatabaseHandlers();
public loginForm(JFrame parent) {
super(parent);
setTitle("Login");
loginPanel = new JPanel();
setContentPane(loginPanel);
setMinimumSize(new Dimension(450, 474));
setModal(true);
setLocationRelativeTo(parent);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getRootPane().setDefaultButton(btnSubmit);
btnSubmit.addActionListener(e -> {
try {
String email = txtEmail.getText();
String password = String.valueOf(txtPassword.getPassword());
loggedUser user = dbHandler.authenticateUser(email, password); //!!!!!!
// exists and is active
if (user.status == 1) {
// open up homepage for budget-tracking app
homeForm hForm = new homeForm(null, user);
setVisible(false);
hForm.setVisible(true);
}
//exists but is inactive (ask if user wants to re-activate account)
if (user.status == 0) {
int result = JOptionPane.showConfirmDialog(null,
"Your account has been temporarily deactivated. Would you like to reactivate it now?",
"Confirmation",
JOptionPane.YES_NO_OPTION
);
//yes, reactivate
if (result == 0) {
boolean reactivated = dbHandler.reactivateUser(email, password); //!!!!
if (reactivated) {
JOptionPane.showMessageDialog(null, "Your account has been reactivated. Please try logging in again.");
}
}
}
//does not exist
if (user.status == -1) {
throw new RuntimeException("This account does not exist.");
}
} catch (DatabaseConnectionException dex) {
SQLException originalException = (SQLException) dex.getCause();
ExceptionHandler.unableToConnectToDb(originalException);
} catch (RuntimeException ex) {
ExceptionHandler.userDoesNotExist(ex);
}
});
btnExit.addActionListener(e -> {
dispose();
System.exit(0);
});
setVisible(true);
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
loginPanel = new JPanel();
loginPanel.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
final JPanel panel1 = new JPanel();
panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(4, 1, new Insets(10, 10, 10, 10), -1, -1));
panel1.setBackground(new Color(-10767293));
loginPanel.add(panel1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 1, false));
final JLabel label1 = new JLabel();
Font label1Font = this.$$$getFont$$$("Candara", Font.BOLD, 18, label1.getFont());
if (label1Font != null) label1.setFont(label1Font);
label1.setForeground(new Color(-16777216));
label1.setText("Welcome to the");
panel1.add(label1, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JLabel label2 = new JLabel();
Font label2Font = this.$$$getFont$$$("Candara", Font.BOLD, 18, label2.getFont());
if (label2Font != null) label2.setFont(label2Font);
label2.setForeground(new Color(-16777216));
label2.setText("Login Form");
panel1.add(label2, new com.intellij.uiDesigner.core.GridConstraints(2, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
panel1.add(spacer1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
final com.intellij.uiDesigner.core.Spacer spacer2 = new com.intellij.uiDesigner.core.Spacer();
panel1.add(spacer2, new com.intellij.uiDesigner.core.GridConstraints(3, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
final JPanel panel2 = new JPanel();
panel2.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(10, 1, new Insets(10, 10, 10, 10), -1, -1));
panel2.setBackground(new Color(-1));
loginPanel.add(panel2, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
final JLabel label3 = new JLabel();
Font label3Font = this.$$$getFont$$$("Candara", Font.BOLD, 12, label3.getFont());
if (label3Font != null) label3.setFont(label3Font);
label3.setForeground(new Color(-16777216));
label3.setHorizontalAlignment(0);
label3.setHorizontalTextPosition(2);
label3.setText("Email");
panel2.add(label3, new com.intellij.uiDesigner.core.GridConstraints(2, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
txtEmail = new JTextField();
txtEmail.setBackground(new Color(-1444097));
panel2.add(txtEmail, new com.intellij.uiDesigner.core.GridConstraints(3, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
final JLabel label4 = new JLabel();
Font label4Font = this.$$$getFont$$$("Candara", Font.BOLD, 12, label4.getFont());
if (label4Font != null) label4.setFont(label4Font);
label4.setForeground(new Color(-16777216));
label4.setHorizontalAlignment(0);
label4.setHorizontalTextPosition(2);
label4.setText("Password");
panel2.add(label4, new com.intellij.uiDesigner.core.GridConstraints(4, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
txtPassword = new JPasswordField();
txtPassword.setBackground(new Color(-1444097));
panel2.add(txtPassword, new com.intellij.uiDesigner.core.GridConstraints(5, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
btnSubmit = new JButton();
btnSubmit.setBackground(new Color(-1));
Font btnSubmitFont = this.$$$getFont$$$(null, Font.BOLD, 16, btnSubmit.getFont());
if (btnSubmitFont != null) btnSubmit.setFont(btnSubmitFont);
btnSubmit.setForeground(new Color(-16777216));
btnSubmit.setText("Login");
panel2.add(btnSubmit, new com.intellij.uiDesigner.core.GridConstraints(7, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
btnExit = new JButton();
btnExit.setBackground(new Color(-1));
Font btnExitFont = this.$$$getFont$$$(null, Font.BOLD, 16, btnExit.getFont());
if (btnExitFont != null) btnExit.setFont(btnExitFont);
btnExit.setForeground(new Color(-16777216));
btnExit.setText("Exit");
panel2.add(btnExit, new com.intellij.uiDesigner.core.GridConstraints(8, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final com.intellij.uiDesigner.core.Spacer spacer3 = new com.intellij.uiDesigner.core.Spacer();
panel2.add(spacer3, new com.intellij.uiDesigner.core.GridConstraints(6, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
final com.intellij.uiDesigner.core.Spacer spacer4 = new com.intellij.uiDesigner.core.Spacer();
panel2.add(spacer4, new com.intellij.uiDesigner.core.GridConstraints(9, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
final JLabel label5 = new JLabel();
label5.setText("");
panel2.add(label5, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final com.intellij.uiDesigner.core.Spacer spacer5 = new com.intellij.uiDesigner.core.Spacer();
panel2.add(spacer5, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_VERTICAL, 1, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
}
/**
* @noinspection ALL
*/
private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
if (currentFont == null) return null;
String resultName;
if (fontName == null) {
resultName = currentFont.getName();
} else {
Font testFont = new Font(fontName, Font.PLAIN, 10);
if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
resultName = fontName;
} else {
resultName = currentFont.getName();
}
}
Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return loginPanel;
}
}
I am using IntelliJ's GUI Designer to create a form with the following component hierarchy:
- JPanel (name: "loginPanel")
- JPanel (with additional components)
- JPanel (with additional components)
As per my understanding, the GUI Designer should automatically generate initialization code for these components in the associated .java file. However, when I run the application, the loginPanel (the topmost JPanel in the hierarchy) appears to be null. I have attempted to manually initialize the component, only to receive the following warning: "Assignment to UI-bound field will overwrite field generated by UI-Designer."
I'm relatively new to Swing, and I've been searching online for a solution to no avail. Could someone help me understand what might be going wrong in my approach, and how I can correctly initialize the JPanel components without encountering the warning?
I've tried to manually initialize the JPanel, "loginPanel", but this just simply overwrites whatever it is the UI-Designer's generated.
I've also tried to "custom create" the component, simply to initialize it within the method that gets created within the .java file, but this just overwrites the design properties I've set for it using the UI-designer within the .form file.
Please sign in to leave a comment.
File | Settings | Editor | GUI Designer, select the radio button Generate GUI into Java source files, then share the full source code of the loginForm ?
Sure thing. Just editted the code block in the original post to show the code in its entirety.
Hello,
I've fixed the issue:
public static void main(String[] args) {
JFrame jFrame = new JFrame("Test");
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
loginForm loginForm = new loginForm(jFrame);
}
public loginForm(JFrame parent) {
super(parent);
setTitle("Login");
// loginPanel = new JPanel(); // Don't init it by your self
If you still have problems, please share the project file to me via upload it to https://uploads.jetbrains.com/ and paste the upload id here. None JetBrains employees will not able to access your file in this case.
Thanks for the help, however I'm running into more of these initialization issues with some of the other componenets. I wanted to upload my full project, but seeing as how there's a 20 folder limit, I sent in what I considered the main features. The upload ID is as follows: 2024_01_19_gdAjynhxzKHvsxRA9iXBYf
Thanks again for the assistance!
ChartPanel
loggedUser
homeForm
etc.
You may compress the project folder as a zip and upload it again.
2024_01_22_22gtVNVVmoL4GTMRRyxuBj
I compressed my project and have uploaded it. Again, apologies for the late reply.
./initialRun.sh
then enter y
then the error is:
./initialRun.sh: line 3: ${AWS_USE_ANSWER,,}: bad substitution
Any idea about this?
yeah, I have yet to implement that feature and if you input “N” and hit enter for that first prompt, the script will prompt you with further questions to set up an on-premise database. Apologies for not clarifying this beforehand.
To clarify, for the first prompt, enter “N” and then “Y” when it asks, “Would you like to use an on-premise database? (Y/N)” and “Y” again when given this prompt, “May we seek your authorization to proceed with this configuration? (Y/N) ”
My project makes use of a MySQL database and the process explained above should create a (local) db with the necessary tables.
Okay, I'm wondering if you've figured out the root cause to all these initialization issues?
Hello Jimbom0219, sorry for the late reply. I've try setup database and running the program well:
Can you tell me which version of the IDEA you are using? I'm using IDEA 2023.3.4 to run it.
Do you mean that every form designed by IDEA throws NullPointerException?
Please provide IDE logs for analysis (entire archive): Help | Collect Logs and Diagnostic Data. I will search for exceptions for it.
Logs can be uploaded privately to https://uploads.jetbrains.com. You need to provide the id of the upload here.