Java Swing:启用/禁用JPanel中的所有组件 [英] Java Swing: Enabling/Disabling all components in JPanel

查看:286
本文介绍了Java Swing:启用/禁用JPanel中的所有组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel,其中包含一个JToolbar(包括几个没有文本的按钮)和一个JTable,我需要启用/禁用(使内部小部件不可点击)。我试过这个:

I have a JPanel which contains a JToolbar (including few buttons without text) and a JTable and I need to enable/disable (make internal widgets not clickable). I tried this:

 JPanel panel = ....;
 for (Component c : panel.getComponents()) c.setEnabled(enabled);

但它不起作用。是否有一个更好,更通用的解决方案来启用/禁用JPanel中的所有内部组件?

but it doesn't work. Is there a better and more generic solution to enable/disable all internal components in a JPanel?

我已经从这里的示例开始使用JLayer部分解决了我的问题 http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html

I have partially solved my problem using JLayer starting from the example here http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html:

layer = new JLayer<JComponent>(myPanel, new BlurLayerUI(false));
.....
((BlurLayerUI)layer.getUI()).blur(...); // switch blur on/off


class BlurLayerUI extends LayerUI<JComponent> {
  private BufferedImage mOffscreenImage;
  private BufferedImageOp mOperation;

  private boolean blur;

  public BlurLayerUI(boolean blur) {
      this.blur = blur;
      float ninth = 1.0f / 9.0f;
        float[] blurKernel = {
          ninth, ninth, ninth,
          ninth, ninth, ninth,
          ninth, ninth, ninth
        };
        mOperation = new ConvolveOp(
                new Kernel(3, 3, blurKernel),
                ConvolveOp.EDGE_NO_OP, null);
        }

  public void blur(boolean blur) {
      this.blur=blur;
    firePropertyChange("blur", 0, 1);
   }

  @Override
  public void paint (Graphics g, JComponent c) {
      if (!blur) {
            super.paint (g, c);
            return;
        }

      int w = c.getWidth();
    int h = c.getHeight();



    if (w == 0 || h == 0) {
      return;
    }

    // Only create the offscreen image if the one we have
    // is the wrong size.
    if (mOffscreenImage == null ||
            mOffscreenImage.getWidth() != w ||
            mOffscreenImage.getHeight() != h) {
      mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D ig2 = mOffscreenImage.createGraphics();
    ig2.setClip(g.getClip());
    super.paint(ig2, c);
    ig2.dispose();

    Graphics2D g2 = (Graphics2D)g;
    g2.drawImage(mOffscreenImage, mOperation, 0, 0);
  }

  @Override
  public void applyPropertyChange(PropertyChangeEvent pce, JLayer l) {
    if ("blur".equals(pce.getPropertyName())) {
      l.repaint();
    }
  }

}

我仍然有两个问题:

1)在上面的链接中,事件仅与鼠标有关。如何管理键盘事件?

1) In the link above events are relative to mouse only. How can I manage the keyboard events?

2)如何创建灰色效果代替模糊?

2) How can I create a "gray out" effect in place of blur?

推荐答案

它需要递归调用。

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

public class DisableAllInContainer {

    public void enableComponents(Container container, boolean enable) {
        Component[] components = container.getComponents();
        for (Component component : components) {
            component.setEnabled(enable);
            if (component instanceof Container) {
                enableComponents((Container)component, enable);
            }
        }
    }

    DisableAllInContainer() {
        JPanel gui = new JPanel(new BorderLayout());

        final JPanel container = new JPanel(new BorderLayout());
        gui.add(container, BorderLayout.CENTER);

        JToolBar tb = new JToolBar();
        container.add(tb, BorderLayout.NORTH);
        for (int ii=0; ii<3; ii++) {
            tb.add(new JButton("Button"));
        }

        JTree tree = new JTree();
        tree.setVisibleRowCount(6);
        container.add(new JScrollPane(tree), BorderLayout.WEST);

        container.add(new JTextArea(5,20), BorderLayout.CENTER);

        final JCheckBox enable = new JCheckBox("Enable", true);
        enable.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                enableComponents(container, enable.isSelected());
            }
        });
        gui.add(enable, BorderLayout.SOUTH);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args)  {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new DisableAllInContainer();
            }
        });
    }}

这篇关于Java Swing:启用/禁用JPanel中的所有组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆