swing enhancements in java 7

17
Swing Enhancements in Java 7 No reason to wait

Upload: berdanika

Post on 04-Mar-2015

105 views

Category:

Documents


4 download

TRANSCRIPT

Swing Enhancements in Java 7

No reason to wait

Agenda

• Decoration with JLayer• Translucent and shaped windows• Mixing heavy and lightweight components

JLayer

• Component decorator• Based on JxLayer http://java.net/projects/jxlayer project

JFrame f = new JFrame(); JPanel panel = createPanel(); LayerUI<JPanel> ui = new CustomLayerUI(); JLayer<JPanel> layer = new JLayer<JPanel>(panel, ui); f.getContentPane().add (layer);

JLayer in action

public class CustomLayerUI extends LayerUI { @Override public void paint(Graphics g, JComponent c) { // paint the component itself super.paint(g, c); // decorate component … }}

JLayer: Event Handling

• Register to receive events

• Override corresponding protected “process<AWTEvent>” method

@Overridepublic void installUI(JComponent c) { super.installUI(c); JLayer jlayer = (JLayer)c; jlayer.setLayerEventMask( AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK );}

JLayer: How to

• Requires repaint on update of underlying components– Not recommended for frequently updated components

• May be used instead of GlassPane to grey out selected UI component and suppress events– The only UI component is freezed on error instead of the

whole frame

Window translucency types

• Uniform translucency– all of the pixels of the window have the same

alpha value between or including 0.0 and 1.0• Per-Pixel translucency

– window might contain pixels with arbitrary alpha values between and including 0.0 and 1.0

• Per-Pixel transparency– each pixel is either completely opaque or

completely transparent

Window translucensy

• Check whether the feature is supported

• Window must NOT be in full screen mode• In Java 6 the translucency might not be supported for

decorated windows

GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsDevice.WindowTranslucency type = … gd.isWindowTranslucencySupported(type)// Java 6:// AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency)

Uniform translucency: TRANSLUCENT

• Invoke Window.setOpacity(float)• Java 6: AWTUtilities.setWindowOpacity(win, float)

Works for decorated windows!

Per-pixel: PERPIXEL_TRANSLUCENT

• JFrame.setBackground(new Color(R, G, B, alpha)), 0 < alpha < 255

• Java 6: AWTUtilities.setWindowOpaque(win, false); same as alpha = 0

Throws exception if window is decorated!

Shaped windows: PERPIXEL_TRANSPARENT

• Invoke Window.setShape(Shape)• Java 6: AWTUtilities.setShape(window, Shape) Silently does not work on decorated windows

Mixing heavy- and lightweight

• Heavyweight component – Has its own native screen resource, a peer– Located in java.awt package: Button, Label, etc– Popular third-party components as embedded

browsers are heavyweight• Lightweight component

– Reuses native window of its closest heavyweight ancestor

– Located in java.swing package: JButton, JLabel

Just works since JDK 6.12

Java 5 Java 6.12

No difference in source code

If just does not work…

Revalidate component hierarchy // make sure component is revalidated lightComponent.revalidate(); // if lightComponent.isValidateRoot(), revalidate parent Container parent = lightComponent.getParent(); if (parent instanceof JComponent) { ((JComponent)parent).revalidate(); }

//if Java 6, might need to call validate() on the top-level window Window window = SwingUtilities.getWindowAncestor(heavyComponent); if (window != null) { window.validate(); }

Mixing Tips

• Use the SIMPLE_SCROLL_MODE mode if JScrollPane contains heavyweight component

• For mixing a non-rectangular lightweight component with a heavyweight component– AWTUtilities.setComponentMixingCutoutShape(

Component component, Shape shape)• Disable it using sun.awt.disableMixing system

property

Mixing limitations

• Translucent lightweight components are not supported. Heavyweight components will NOT be visible through translucent pixels

• Anti-aliased (smooth) shapes are not supported• Heavyweight components must have valid peer, i.e.

must be created by the process which have created the frame

• Key events of InputMap might not work correctly when components are overlapped

Thank you!

Questions?