tags:

views:

61

answers:

3

This is my first time using a JFrame. I can't get the window to display the text areas I've nested inside the JFrame. I am trying to get the text field with my name in it to display above the tabulated results, which I have omitted the formatting for until I can get the JFrame to work.

public void printResults(String[] names, int[] temp, int[][] scores, float[] averages, char[] letters){

    JTextArea outarea= new JTextArea(5,20);
    JTextArea name = new JTextArea(5,20);
    Font font = new Font("Tahoma", Font.BOLD, 48);
    name.setFont(font);
    name.setText("Made By Durka Durka");

    JFrame window = new JFrame();
    window.getContentPane().add(name);
    window.getContentPane().add(outarea);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.pack();
    window.getContentPane().setVisible(true)

    String out = "foo";

    outarea.setText(out);


    //JOptionPane.showMessageDialog(null,window);

}
+1  A: 

First things first - are you calling printResults on the Event Dispatch Thread using SwingUtilities.invokeLater(Runnable runnable); or SwingUtilities.invokeAndWait(Runnable runnable);? Remember that you need to do all GUI work on the EDT.

If so, try this:

JFrame window = new JFrame();
// CHANGES HERE
window.getContentPane().setLayout(new BorderLayout();
window.getContentPane().add(name, BorderLayout.NORTH);
window.getContentPane().add(outarea, BorderLayout.CENTER);
// END CHANGES
justkt
+2  A: 

You should use some of the layout managers. This link should help you:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

The standard java layout managers are sometimes really hard to handle. Maybe you should also look into the JGoodies: http://www.jgoodies.com/ "Framework". It is easier to use and you realize that even a java gui can look nice..

Prine
+1 for layout managers.
justkt
+3  A: 

The probable reason why the JFrame is not appearing is because of this line:

window.getContentPane().setVisible(true)

The above line is setting the visibility of the Container to which the JTextAreas have been added, but does not control the visibility of the JFrame itself -- therefore, the JFrame itself is not being displayed.

(To be more precise, the JFrame.getContentPane method returns a Container, so the above code is actually calling the Containter's setVisible method.)

Try the following instead:

window.setVisible(true);

This will set the visibility of the JFrame itself to be visible.

Also, as the other answers have suggested, try using Layout Managers to control the locations of where the components should be displayed. The following are links to using two of the several Layout Managers which could be used to layout components.

coobird
+1, good catch!
justkt
This fixed it. I did not realize that you could have visible invisible sections.
DasWood