Ho un JPanel che contiene una barra di JTool (compresi alcuni pulsanti senza testo) e una tabella di controllo e devo abilitare/disabilitare (rendere i widget interni non selezionabili). Ho provato questo:Java Swing: attivazione/disattivazione di tutti i componenti in JPanel
JPanel panel = ....;
for (Component c : panel.getComponents()) c.setEnabled(enabled);
ma non funziona. Esiste una soluzione migliore e più generica per abilitare/disabilitare tutti i componenti interni in un JPanel?
ho parzialmente risolto il problema utilizzando JLayer partendo dall'esempio qui 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();
}
}
}
ho ancora 2 problemi:
1) nel link qui eventi sono relativi solo mouse. Come posso gestire gli eventi della tastiera?
2) Come posso creare un effetto "grigio" al posto della sfocatura?
È possibile impostare la visibilità del pannello direttamente utilizzando 'panel.setVisible (false)' –
setVisible (false) rende la componente " invisibile "ma in realtà ne ho bisogno ancora visibile ma in grigio. – Randomize
questo thread non aiuta affatto? http://stackoverflow.com/a/305551/1417974 –