Overlay Transparency

I sent this question initially to the MigLayout forum, as it is, to some extent, specific to this particular layout manager, I would say. Unfortunately, this is already a week, without any comments, so I would like to answer this question here, in the hope that it is not considered an X-wiring.

Well, I have the following problem: In general, I would like to “gray out” some content (more precisely, the content of JPanel). I almost managed to achieve the desired effect by setting another JPanel to “flow” over the desired area with transparent color. There remained one serious problem: After I click the overlap panel, the components below will not immediately draw correctly. Instead, they will initially be drawn with some white color until, for example, I hide and enlarge the frame that could make the correct effect transparent.

As an example, I attached a screenshot of the buggy. Pay attention to the switches and checkboxes with their white appearance. In addition, I added code to play the effect. I am currently working on Windows 7, 32 bit with Java 1.5. (Java 1.6 does not matter).

I played a lot with various colors () / repaint () / (in / re) validate () / etc. methods. Nothing worked to draw the floor panel correctly right after its visibility.

Any ideas?

BR, Chris

EDIT1: Regarding the sample code - if you run it, you have to click the "Check" button to bring up the overlap panel. :) Another thing I noticed is that appearance problems vary depending on the UI L used & F. In my case, I used Windows once and once my own Java interface (is this called metal?), And they showed different results, although none of them worked properly: P

Error screenshot link: Screenshot

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class MigTest {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new TestLayeredPane());
    f.pack();
    f.setVisible(true);
  }
  public static class TestLayeredPane extends JPanel {
    private 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);
}
  }
}
+3
source share
1

, . , , , , , , .

, , . :

static void setChildrenEnabled(Container root, boolean enable)
{
    Component children[] = root.getComponents();
    for(int i = 0; i < children.length; i++)
    {
        children[i].setEnabled(enable);
        if(children[i] instanceof Container)
        {
            setChildrenEnabled((Container) children[i], enable);
        }          
    }
}

, , , , : http://tips4java.wordpress.com/2009/08/02/disabled-panel/

, - .

+1

Source: https://habr.com/ru/post/1764397/


All Articles