Swing Components Invisible as Startup
Hello everyone. I have done a program using swing GUI components, but whenever I run the program the jframe comes up empty. When I minimize and maximize the window or if I click at the bottom of the window (where you place your mouse to resize the frame), the components appear as normal. Even from command line it will start up like this. Has anyone else ever experienced this and can anyone advise me as to what I can do ro solve it?
Re: Swing Components Invisible as Startup
Without some code demonstrating the problem its anyone's guess. My guess is that you are adding components after you make the window visible. Add components before (make sure to call pack() as well) or - if you must add them after you make the window visible - call validate().
Re: Swing Components Invisible as Startup
Quote:
Originally Posted by
copeg
Without some code demonstrating the problem its anyone's guess. My guess is that you are adding components after you make the window visible. Add components before (make sure to call pack() as well) or - if you must add them after you make the window visible - call validate().
Thank you for replying. My code is just so long that I did not really want to post it until it was really necessary. And it is not necessary because you got it right. I had the frame made and its properties set before I added the components to it. So i just made the frame, added the components to it and set the frame properties. Thanks for you help again.
Re: Swing Components Invisible as Startup
Hi. I am still studying first year java. Sorry for posting on an old thread but it came up on a google search.
I have exactly the same problem as the original post but I was unable to resolve it as he did. In my case I add all the swing components in a class constructor and an instance of this class is instantiated first thing in the main method. I've never had a problem with this before. Here is the code. Appreciate!
public class WrdCount extends JFrame implements ActionListener
{
private StringBuffer sb;
private static JTextPane textPane = new JTextPane();
private static JButton cntBtn = new JButton("Count Words");
private static JLabel cntLbl = new JLabel("Word Count = ");
private static JTextField cntFld = new JTextField(4);
private static JPanel mainPnl = new JPanel();
private static JPanel topPnl = new JPanel();
private static JPanel bottomPnl = new JPanel();
private static JScrollPane scrollPane;
private WrdCount()
{
scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(400,200));
//layouts
topPnl.setLayout(new BorderLayout(50,50));
topPnl.add(scrollPane, BorderLayout.CENTER);
bottomPnl.setLayout(new FlowLayout(FlowLayout.CENTER,5,3));
bottomPnl.add(cntBtn);
cntBtn.addActionListener(this);
bottomPnl.add(cntLbl);
bottomPnl.add(cntFld);
cntFld.setEditable(false);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,50));
c.add(topPnl,BorderLayout.CENTER);
c.add(bottomPnl,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.M etalLookAndFeel");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Could not set the Look And Feel for this application"
, "Display Error", JOptionPane.WARNING_MESSAGE);
}
WrdCount f = new WrdCount();
f.setTitle("Word Counter");
f.setVisible(true);
f.setSize(600, 400);
f.setResizable(false);
f.setLocation(200,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Re: Swing Components Invisible as Startup
Quote:
Originally Posted by
BiteCruncher
Hi. I am still studying first year java. Sorry for posting on an old thread but it came up on a google search.
I have exactly the same problem as the original post but I was unable to resolve it as he did. In my case I add all the swing components in a class constructor and an instance of this class is instantiated first thing in the main method. I've never had a problem with this before. Here is the code. Appreciate!
public class WrdCount extends JFrame implements ActionListener
{
private StringBuffer sb;
private static JTextPane textPane = new JTextPane();
private static JButton cntBtn = new JButton("Count Words");
private static JLabel cntLbl = new JLabel("Word Count = ");
private static JTextField cntFld = new JTextField(4);
private static JPanel mainPnl = new JPanel();
private static JPanel topPnl = new JPanel();
private static JPanel bottomPnl = new JPanel();
private static JScrollPane scrollPane;
private WrdCount()
{
scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(400,200));
//layouts
topPnl.setLayout(new BorderLayout(50,50));
topPnl.add(scrollPane, BorderLayout.CENTER);
bottomPnl.setLayout(new FlowLayout(FlowLayout.CENTER,5,3));
bottomPnl.add(cntBtn);
cntBtn.addActionListener(this);
bottomPnl.add(cntLbl);
bottomPnl.add(cntFld);
cntFld.setEditable(false);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,50));
c.add(topPnl,BorderLayout.CENTER);
c.add(bottomPnl,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.M etalLookAndFeel");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Could not set the Look And Feel for this application"
, "Display Error", JOptionPane.WARNING_MESSAGE);
}
WrdCount f = new WrdCount();
f.setTitle("Word Counter");
f.setVisible(true);
f.setSize(600, 400);
f.setResizable(false);
f.setLocation(200,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Ok, try this. Comment the line f.setVisible(true). Make a JPanel named c to replace the container. Remove the line Container c = getcontentpane. So basicaly your top and bottom panels will be in another panel named c. The finally add c to f which is the wrdCount object. f.add(c). Then add the line f.setvisible = true.
So in other words you are not using the container.
Re: Swing Components Invisible as Startup
Not sure what you're expecting from the GUI, but for me, the GUI is visible from the offset.