views:

301

answers:

3

I'm trying to set the text in a label dynamically by calling the setText method whenever a button is clicked. Here is my code:

import java.awt.*;
import java.awt.event.*;

class Date {
    public static void main(String[] args) {
        new MainWindow();
    }
}

class MainWindow {
    static Label month = new Label();
    static Label day = new Label();
    static Button submit = new Button("Submit");

    MainWindow() {
        Frame myFrame = new Frame("Date Window");
        myFrame.setLayout(new FlowLayout());
        myFrame.add(month);
        myFrame.add(day);
        myFrame.add(submit);
        submit.addActionListener(new ButtonListener());

        myFrame.addWindowListener(new WindowListener());        
        myFrame.setSize(200, 200);
        myFrame.setVisible(true);
    }
}

class WindowListener extends WindowAdapter {
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

class ButtonListener implements ActionListener  {
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == MainWindow.submit) {
            MainWindow.month.setText("12");
            MainWindow.day.setText("31");
        } 
    }
}

When I initialize the two Label objects without any arguments, the strings "12" and "31" that are passed to the setText method aren't visible on the screen when the submit button is clicked until I click on the window and drag to resize it. I've noticed this on a Mac only. On a PC, the strings are are visible but obscured until I resize the window. However, if I initialize the labels like this:

    static Label month = new Label("0");
    static Label day = new Label("0");

On the Mac, the strings appear as intended, however, they're obscured until the window is resized. What am I missing?

A: 

Try repainting the frame or/and set enough space(setPreferredSize, setMininumSize)

anonymous
How would I do that?
A: 

Well, most of your posting are over a year old so I'll give you the benefit of the doubt. I never use AWT so I don't know what the problem is, but I'll suggest:

1) Name you classes properly. "Date" is already a class in the JDK. Choose a better name.

2) Try using Swing components instead of AWT.

3) Get rid of static variables from your class.

4) Get rid of the WindowListener to close the frame.

The code example you posted here is 10-15 years old. Try something newer. Start with the Swing tutorial for more recent examples.

camickr
Yet again it amazes me that people down vote a suggestion without adding a comment. Everything I said was constructive even if some of the comments don't directly answer the posted code. I always recommend Swing because sometime people don't even know the difference between AWT and Swing.
camickr
This is not a useful answer as it doesn't address the problem. Replacing the framework and performing two refactorings will not solve the problem. Clearly the WindowListener is unrelated as it only handles closing the window.
Paul Lammertsma
+1  A: 

Calling validate() on the Frame as mentioned here solved the problem.