-1

i create a dialog box in java desktop application . But when i hide/show label and button by applying condition on checkbox .its produce graphical noise by showing some part of background application part(like red box on both label and buttons also checkbox layout causes problem). i write this condition on checkbox.

 checkbox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            if (checkbox.isSelected()) {
                baisvalue.setVisible(true); //label
                plusbais.setVisible(true);  //button
                minisbais.setVisible(true); //button

            }
            if (!checkbox.isSelected()) {
                minisbais.setVisible(false); //label
                plusbais.setVisible(false);  //button
                baisvalue.setVisible(false); //button
            }

        }
    });

note: i also call repaint(); & validate(); but same problem occurs.

adeel khalid
  • 65
  • 1
  • 10
  • 1
    will you please elaborate "*showing some part of background application part.*" – Harmeet Singh Aug 15 '12 at 15:58
  • 1
    Be certain to honor the [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) property; examples cited [here](http://stackoverflow.com/a/11967619/230513). – trashgod Aug 15 '12 at 16:00
  • dear Harmeet some red box appears on label and buttons .also on checkbox main application have some graphs and labels these are shown on label and buttons when i click on checkbox – adeel khalid Aug 15 '12 at 16:04
  • 1
    @trashgod how the hell did you see that!? I'm continually amazed 8O – MadProgrammer Aug 15 '12 at 20:21
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Aug 15 '12 at 22:31
  • @MadProgrammer: "If you do not honor the opaque property you will likely see visual artifacts."—[`paintComponent()`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29), not that _I_ would ever do that. :-) – trashgod Aug 15 '12 at 23:30
  • @trashgod That's an awesome leap from the example to that point. Nice memory :) – MadProgrammer Aug 15 '12 at 23:46

2 Answers2

2

I wasn't able to recreate your problem - I see no graphical noise. I've attached a sscce of what I tried - Can you reproduce your problem with this example? If so, can you provide us with more information about your java version/platform? If not, can you modify this example to recreate your problem (and edit your question with the code)?

import java.awt.event.*;
import javax.swing.*;

public class MainPanel extends Box{

    JCheckBox checkbox = new JCheckBox("Select Me");
    JLabel baisvalue = new JLabel("baisvalue");
    JButton plusbais = new JButton("plusbais");
    JButton minisbais = new JButton("minisbais");

    public MainPanel(){
        super(BoxLayout.Y_AXIS);
        ActionListener l = new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                if (checkbox.isSelected()) {
                    baisvalue.setVisible(true); //label
                    plusbais.setVisible(true);  //button
                    minisbais.setVisible(true); //button

                }
                if (!checkbox.isSelected()) {
                    minisbais.setVisible(false); //label
                    plusbais.setVisible(false);  //button
                    baisvalue.setVisible(false); //button
                }

            }
        };
        checkbox.addActionListener(l); 
        add(checkbox);
        add(baisvalue);
        add(plusbais);
        add(minisbais);

        //Performs the action on initialization
        l.actionPerformed(new ActionEvent(checkbox, ActionEvent.ACTION_PERFORMED, ""));
    }


    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MainPanel());
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30
0

Try to use this.setOpaque(false); in constructor.

IConfused
  • 714
  • 2
  • 8
  • 20