Java GUI component display problem
I am writing an application using Java / Swing and I also encountered a problem: When displaying a JFrame, the components are not displayed until you hover the mouse over them. JFrame stores the Main_GUI class, which is inherited from JPanel, the button is still in the class itself, and the AllPosts_GUI class, which contains the JTextField.
Main class:
public class Client
{
public static void main(String[] ar)
{
JFrame jpClientGUI = new JFrame();
jpClientGUI.setSize(800, 500);
jpClientGUI.setTitle("Daily Poster");
jpClientGUI.setLayout(new BorderLayout());
jpClientGUI.add(new Main_GUI());
jpClientGUI.setVisible(true);
}
}
Main_GUI class:
public class Main_GUI extends JPanel{
public Main_GUI(){
this.setBackground(new Color(100,230,240));
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
AllPosts_GUI allPosts = new AllPosts_GUI();
add(allPosts, new GridBagConstraints(0,0,1,1,0.0,1,
GridBagConstraints.FIRST_LINE_START,GridBagConstraints.HORIZONTAL,
new Insets(100,0,0,0), 0,0));
JButton button = new JButton("Add new Post");
add(button, new GridBagConstraints(1,0,1,1,0.0,1,
GridBagConstraints.FIRST_LINE_END,GridBagConstraints.HORIZONTAL,
new Insets(100,150,0,0), 0,0));
this.setVisible(true);
this.validate();
}
public void paint(Graphics g)
{
Font font;
g = (Graphics2D)g;
g.setColor(new Color(200,100,100));
font = new Font(null, Font.ITALIC | Font.BOLD, 30);
g.setFont(font);
g.drawString("Welcome to \"Daily Poster\"", 220, 50);
}
}
AllPosts_GUI class:
public class AllPosts_GUI extends JPanel{
public AllPosts_GUI(){
JTextArea ma = new JTextArea();
ma.setMinimumSize(new Dimension(200,200));
ma.setText("texttexttexttexttexttexttexttext\n"
+ "texttexttexttexttexttexttexttext\n"
+ "texttexttexttexttexttexttexttext\n");
add(ma);
this.setVisible(true);
this.validate();
}
public void paint(Graphics g)
{
}
}
In all constructors, I specifically called the methods this.setVisible (true) and this.validate() , etc. but it didn't help.
Modified main code:
public static void main(String[] ar)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowMainGUI();
}
});
}
private static void createAndShowMainGUI() {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrame.setSize(850, 500);
jFrame.setTitle("Daily Poster");
jFrame.setBackground(new Color(100,230,240));
GridBagLayout gbl = new GridBagLayout();
jFrame.setLayout(gbl);
AllPosts_GUI apg = new AllPosts_GUI();
jFrame.getContentPane().add(apg);
jFrame.pack();
jFrame.setVisible(true);
}
}
3 answers
You are overriding paint()
, and so the drawing of components is not handled by default. To call it, add super.paint(g);
to the top of the paint()
method.
I recommend that you familiarize yourself with the basic techniques of working with Swing. See Java Tutorials Code Sample-HelloWorldSwing.java. Note how the Swing window generation method is called in the main method.
Update
Unfortunately, there is no time to understand what is wrong with you, but if the initialization code JTextArea
is transferred from the AllPosts_GUI
class to the createAndShowMainGUI
method, then everything works fine. See the code below. Since the size of the comment is limited, some things had to be reduced, left only the new one.
private static void createAndShowMainGUI() {
... JPanel apg = new JPanel();
apg.setBorder(new EmptyBorder(5, 5, 5, 5));
apg.setLayout(new BorderLayout(0, 0));
jFrame.setContentPane(apg);
JScrollPane scrollPane = new JScrollPane();
apg.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea();
textArea.setText("..");
scrollPane.setViewportView(textArea);
jFrame.pack();
jFrame.setVisible(true);
}
Why call validate
so often? And why call it manually at all? Rely on pack
before calling setVisible
, just call setVisible
from the frame, unless you force setVisible(false)
anywhere.