开发者

AWT repaint issues

I am trying to make some small additions to some old java code that does not support swing. I need to add a small dialog that con开发者_开发问答tains a panel which has a checkbox and a couple text fields. When the user clicks on the checkbox I want to disable or enable the checkboxes. This part seems to work well but the text fields are not properly getting redrawn. When I click the checkbox the fields do not appear to become enabled but if I then click on the panel or the text field you see that they are enabled (the opposite is also true, when I un-check the checkbox the fields still look enabled until you try and click on them and they become ghosted and do not become selected). I use the setEnabled(boolean) to set the status of the fields. I have tried calling repaint and validate on both the fields and the panel after changing the status and this does not seem to work. I have also tried to have the fields request focus and this did not work. Anyone have any other ideas?

 //The class that contains all of this is of type Window
 //Declaration of the components
  private Panel _inputPanel;
  private TextField min , max;
 //This method adds to two text fields
 public void addMinMaxtextFields(String min, String max) {
    TextField minField = new TextField(min);
    TextField maxField = new TextField(max);

    this.min = minField;
    this.max = maxField;
    this.min.setEnabled(false);
    this.max.setEnabled(false);
    _inputPanel.add(minField);

    _inputPanel.add(maxField);


}
//listener for the checkbox
public void itemStateChanged(ItemEvent e) {
    Component[] components = _inputPanel.getComponents();

    min.setEnabled(!min.isEnabled());
    min.setVisible(true);
    min.validate();
    min.repaint();

    _inputPanel.validate();
    _inputPanel.repaint();
    this.pack();

    this.setSize(this.getWidth(), this.getHeight());

    this.validate();

    this.repaint();
    /* do nothing */
}


You will need to call update(Graphics g) on Panel after setEnabled(boolean) is called.

check : http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#update(java.awt.Graphics)

I tried following code (built from code you provided), Its working fine.

import java.awt.Checkbox;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class CheckUI extends Dialog implements ItemListener {

    // The class that contains all of this is of type Window
    // Declaration of the components
    private Panel _inputPanel;
    private TextField min, max;
    private Checkbox cb;

    public CheckUI(Frame owner, boolean modal) {
        super(owner, modal);
        _inputPanel = new Panel();

        this.add(_inputPanel);
        addMinMaxtextFields("min", "max");
    }


    // This method adds to two text fields
    public void addMinMaxtextFields(String min, String max) {
        cb = new Checkbox();
        cb.addItemListener(this);
        TextField minField = new TextField(min);
        TextField maxField = new TextField(max);

        this.min = minField;
        this.max = maxField;
        this.min.setEnabled(false);
        this.max.setEnabled(false);
        _inputPanel.add(minField);

        _inputPanel.add(maxField);
        _inputPanel.add(cb);

    }

    // listener for the checkbox
    public void itemStateChanged(ItemEvent e) {
        Component[] components = _inputPanel.getComponents();

        min.setEnabled(!min.isEnabled());
        min.setVisible(true);
        min.validate();
        min.repaint();

        _inputPanel.validate();
        _inputPanel.repaint();
        this.pack();

        this.setSize(this.getWidth(), this.getHeight());

        this.validate();

        this.repaint();
        /* do nothing */
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Frame parent = new Frame();
        parent.setVisible(true);
        parent.setExtendedState(Frame.MAXIMIZED_BOTH);
        parent.pack();

        CheckUI ui = new CheckUI(parent, true);
        ui.pack();
        ui.setVisible(true);

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜