views:

73

answers:

1
+1  Q: 

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 contains 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 */
}
+1  A: 

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);

    }

}
YoK
Hmm that did not seem to do the trick. Thanks for your advice though.
Mike2012
@Mike2012 Can you please share your code ?
YoK
@Mike2012 I dont think this is complete code. I also need to understand checkbox related code which is completely missing.
YoK
My windows was a dialog not a frame, when I switched it to a frame, it worked.
Mike2012
Unfortunately there is no way to make a frame modal, do you have any idea why this being a frame instead of a dialog would change the situation?
Mike2012
@Mike2012 you have still not uploaded your code :(.
YoK
@Mike2012 OK ... I have tried to make it modal dialog. updated my answer... it works :)
YoK