View unanswered posts | View active topics It is currently Tue Apr 25, 2017 11:32 am



Reply to topic  [ 1 post ] 
 Transparency issue with overlay panel 
Author Message

Joined: Mon Aug 30, 2010 10:13 pm
Posts: 6
Post Transparency issue with overlay panel
Hello.

I have the following problem which I couldn't get solved for quite some hours now...
In general I'd like to 'grey out' some content (the content of a JPanel more precisely). I nearly managed to achieve the desired effect via placing another JPanel 'flowing' over the desired area with a transparent colour. There's one major problem left though:
Once I set the overlay panel visible, the components below will not immediately paint correct. Instead they will initially take on some white colour, until for example I minimize and re-maximize the frame which would cause the transparent effect to be drawn correct.
As example I attached a screen-shot of the 'buggy' appearance. Additionally I attached code to reproduce the effect. Currently I'm working on Windows 7, 32bit with Java 1.5. (Java 1.6 didn't make any difference).

I played around quite a lot with the various paint()/repaint()/(in/re)validate()/etc. methods. Nothing worked to paint the overlay panel correct right after setting it visible.

Any ideas?

BR, Chris

Code:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Chrisi
 */
public class MigTest {

  public static void main(String[] args) {
    try {
      // Set System L&F
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (UnsupportedLookAndFeelException e) {
      // handle exception
    }
    catch (ClassNotFoundException e) {
      // handle exception
    }
    catch (InstantiationException e) {
      // handle exception
    }
    catch (IllegalAccessException e) {
      // handle exception
    }

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new TestLayeredPane());
    f.pack();
    f.setVisible(true);
  }

  public static class TestLayeredPane extends JLayeredPane {

    JPanel pnl_overlay_;

    public TestLayeredPane() {
      super();
      this.setLayout(new MigLayout("fill, wrap 2", "", ""));

      initLayout();
    }

    private void initLayout() {

      JPanel pnl_direction = new JPanel(new MigLayout("wrap 3", "", ""));
      JPanel pnl_settings = new JPanel(new MigLayout("wrap 1", "", ""));
      JPanel pnl_input = new JPanel(new MigLayout("wrap 3", "", ""));

      JLabel lbl_ask_for = new JLabel();
      JLabel lbl_translation = new JLabel();

      ButtonGroup btn_group_ = new ButtonGroup();
      JRadioButton radiobtn_ask_lang1_ = new JRadioButton();
      JRadioButton radiobtn_ask_lang2_ = new JRadioButton();
      JRadioButton radiobtn_ask_random_ = new JRadioButton();

      JCheckBox chkbox_loop_ = new JCheckBox();
      JCheckBox chkbox_repeat_false_ = new JCheckBox();
      JCheckBox chkbox_letter_count_ = new JCheckBox();
      JCheckBox chkbox_ask_all_ = new JCheckBox();

      JLabel lbl_progress_ = new JLabel();
      JLabel lbl_question_ = new JLabel();

      JButton btn_check_ = new JButton();
      JButton btn_push_back_ = new JButton();

      JTextField tfield_answer_ = new JTextField();

      /** Customize all elements of our layout **/
      lbl_ask_for.setText("Ask for:");

      lbl_translation.setText("translation");

      radiobtn_ask_lang1_.setText("1st language");
      radiobtn_ask_lang2_.setText("2nd language");
      radiobtn_ask_random_.setText("Random language");

      btn_group_.add(radiobtn_ask_lang1_);
      btn_group_.add(radiobtn_ask_lang2_);
      btn_group_.add(radiobtn_ask_random_);

      chkbox_loop_.setText("Loop");
      chkbox_repeat_false_.setText("Repeat false answers");
      chkbox_letter_count_.setText("Show letter count");
      chkbox_ask_all_.setText("Ask for 1st and 2nd language");

      btn_check_.setText("Check");
      btn_check_.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
          finishTraining();
        }
      });

      btn_push_back_.setText(" Push back");

      /** Add all elements to this panel **/
      pnl_direction.add(lbl_ask_for, "span 1 3");
      pnl_direction.add(radiobtn_ask_lang1_);
      pnl_direction.add(lbl_translation, "span 1 3");
      pnl_direction.add(radiobtn_ask_lang2_);
      pnl_direction.add(radiobtn_ask_random_);

      pnl_settings.add(chkbox_loop_);
      pnl_settings.add(chkbox_repeat_false_);
      pnl_settings.add(chkbox_letter_count_);
      pnl_settings.add(chkbox_ask_all_);

      pnl_input.add(lbl_question_, "align center");
      pnl_input.add(lbl_progress_, "span 2, align center");
      pnl_input.add(tfield_answer_, "span 1 2, align center, w 200!");
      pnl_input.add(btn_check_, "growy");
      pnl_input.add(btn_push_back_);

      pnl_overlay_ = new JPanel(new MigLayout("fill", "", ""));
      pnl_overlay_.setBackground(new Color(127, 127, 127, 100));
      this.add(pnl_overlay_, "pos (0%+2px) (0%+2px) (100%-2px) (100%-2px) ");
      pnl_overlay_.setVisible(false);

      this.add(pnl_direction, "gapbefore push");
      this.add(pnl_settings, "gapafter push");
      this.add(pnl_input, "span 2, gapbefore push, gapafter push");
    }

    private void finishTraining() {
      //disable all visible items in the content area
      for (Component comp : this.getComponents()) {

        if (comp instanceof JPanel) {
          for (Component comp2 : ((JPanel) comp).getComponents()) {
            comp2.setEnabled(false);
          }
        }
        else {
          comp.setEnabled(false);
        }
      }
      pnl_overlay_.setVisible(true);
    }
  }
}


Attachments:
screeny.PNG
screeny.PNG [ 56.91 KiB | Viewed 17076 times ]
Mon Sep 06, 2010 12:26 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.