GUI elements are not displayed until the window is resized

I experimented with creating a GUI in java, and not just using "static" all the time, and came across the SwingUtilities.invokeLater () method. I manage to configure all the settings, but when it comes to launching the application, nothing appears on JPanel until I resize the window. Is there a fix for this, or am I doing it wrong?

Here is my code:

public class main extends JPanel implements ActionListener{ public JLabel userLabel; public JLabel passLabel; public JTextField userField; public JTextField passField; public JButton login; public JButton closeLogin; public JButton help; public main(){ userLabel = new JLabel("Username: "); passLabel = new JLabel("Password: "); userField = new JTextField(16); passField = new JTextField(16); login = new JButton("Login"); login.setActionCommand("login"); login.setMnemonic(KeyEvent.VK_L); closeLogin = new JButton("Close"); closeLogin.setActionCommand("closeLogin"); closeLogin.setMnemonic(KeyEvent.VK_E); help = new JButton("Help"); help.setActionCommand("helpLogin"); help.setMnemonic(KeyEvent.VK_H); login.addActionListener(this); closeLogin.addActionListener(this); help.addActionListener(this); add(userLabel); add(userField); add(passLabel); add(passField); add(login); add(help); add(closeLogin); } public void actionPerformed(ActionEvent e){ } public static void initComponents(){ JFrame loginFrame = new JFrame("Encrypted Chat - Login"); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); main loginPanel = new main(); loginPanel.setLayout(new FlowLayout()); loginFrame.setSize(300, 125); loginFrame.setResizable(false); loginFrame.setVisible(true); } public static void main(String args[]){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ initComponents(); } }); } 

}

EDIT: I know the password JTextField is for JPasswordField .. so ignore it: P

+4
source share
3 answers

You never add your content to a JFrame. The minimum set of changes you need:

  public static void main(String args[]){ final main main = new main(); SwingUtilities.invokeLater(new Runnable(){ public void run(){ initComponents(main); } }); } 

And then modify initComponents to take the main object:

  public static void initComponents(main main){ JFrame loginFrame = new JFrame("Encrypted Chat - Login"); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); main loginPanel = new main(); loginPanel.setLayout(new FlowLayout()); loginFrame.setSize(300, 125); loginFrame.setResizable(false); loginFrame.setVisible(true); loginFrame.add(main); // <----- this line is added } 
+4
source

Two main tips:

1.) When you use swing, and the material is not displayed / updated, you should call JPanel.revalidate() and JPanel.repaint() . These two functions will update your panel. If you use JFrame and you have not added additional panels to it, you can get the content panel JFrame.getContentPane()

2.) When you finish adding components to the panel / frame, you must also call pack() in the frame, this ensures that all your components have a preferred size.

+4
source

for built_in FlowLayout (for JPanel ) I do not suggest using pack() for JFrame , the right way could be to use the correct and best LayoutManager for this job, GridBagLayout or SpringLayout

with JFrame#setSize() and without pack()

enter image description here

eg

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class MainLogin implements ActionListener { private JFrame loginFrame = new JFrame("Encrypted Chat - Login"); private JPanel pnl = new JPanel(); private JLabel userLabel; private JLabel passLabel; private JTextField userField; private JTextField passField; private JButton login; private JButton closeLogin; private JButton help; public MainLogin() { userLabel = new JLabel("Username: "); passLabel = new JLabel("Password: "); userField = new JTextField(16); passField = new JTextField(16); login = new JButton("Login"); login.setActionCommand("login"); login.setMnemonic(KeyEvent.VK_L); closeLogin = new JButton("Close"); closeLogin.setActionCommand("closeLogin"); closeLogin.setMnemonic(KeyEvent.VK_E); help = new JButton("Help"); help.setActionCommand("helpLogin"); help.setMnemonic(KeyEvent.VK_H); login.addActionListener(this); closeLogin.addActionListener(this); help.addActionListener(this); pnl.add(userLabel); pnl.add(userField); pnl.add(passLabel); pnl.add(passField); pnl.add(login); pnl.add(help); pnl.add(closeLogin); loginFrame.add(pnl); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); loginFrame.setSize(300, 125); loginFrame.setResizable(false); loginFrame.setVisible(true); } public void actionPerformed(ActionEvent e) { } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { MainLogin mainLogin = new MainLogin(); } }); } } 
+1
source

Source: https://habr.com/ru/post/1432654/


All Articles