!!! Listings zum Artikel "Schoener wohnen" !!! von Thomas Kuenneth in iX 11/09, S. 140 !!! Listing 1: SubstanceDemo.java package com.thomaskuenneth.articles.ix; // diverse import-Statements public class SubstanceDemo extends JFrame { private JTextField textfield; private JCheckBox checkbox; public SubstanceDemo() { super("Substance-Demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField password = new JPasswordField(10); password.putClientProperty(LafWidget.PASSWORD_STRENGTH_CHECKER, new PasswordStrengthChecker() { public PasswordStrength getStrength(char[] password) { if (password == null) return PasswordStrength.WEAK; int length = password.length; if (length < 3) return PasswordStrength.WEAK; if (length < 6) return PasswordStrength.MEDIUM; return PasswordStrength.STRONG; } public String getDescription(PasswordStrength strength) { if (strength == PasswordStrength.WEAK) return "Das Passwort ist zu schwach."; if (strength == PasswordStrength.MEDIUM) return "Das Passwort ist akzeptabel."; if (strength == PasswordStrength.STRONG) return "Das Passwort ist gut."; return null; } }); textfield = new JTextField("Hallo, iX", 10); textfield.putClientProperty(LafWidget.TEXT_EDIT_CONTEXT_MENU, Boolean.TRUE); textfield.putClientProperty(LafWidget.TEXT_SELECT_ON_FOCUS, Boolean.TRUE); checkbox = new JCheckBox("editierbar"); checkbox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updateTextfield(); } }); updateTextfield(); JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 10)); p.add(password); p.add(checkbox); p.add(textfield); setContentPane(p); pack(); } private void updateTextfield() { boolean selected = checkbox.isSelected(); textfield.setEditable(selected); if (selected) { textfield.requestFocus(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame.setDefaultLookAndFeelDecorated(true); try { UIManager .setLookAndFeel("org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel"); } catch (Throwable thr) { thr.printStackTrace(); } UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS, Boolean.TRUE); new SubstanceDemo().setVisible(true); } }); } } !!! Listing 2: Erweitertes main() public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { LAFAdapter.startWidget(); new LAFWidgetDemo().setVisible(true); } }); } !!! Listing 3: DemoWidget.java package com.thomaskuenneth.articles.ix; // diverse import-Statements public class DemoWidget extends LafWidgetAdapter implements KeyListener { protected JTextComponent textComp; public boolean requiresCustomLafSupport() { return false; } @Override public void setComponent(JTextComponent jcomp) { super.setComponent(jcomp); this.textComp = jcomp; } @Override public void installListeners() { textComp.addKeyListener(this); textComp.setOpaque(true); } @Override public void uninstallListeners() { textComp.removeKeyListener(this); } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { Toolkit.getDefaultToolkit().beep(); } } !!! Listing 4: Erweitertes AbstractLayerUI private JXLayer createTextfield() { JTextField textfield = new JTextField(20); return new JXLayer(textfield, new AbstractLayerUI() { @Override protected void processMouseEvent(MouseEvent e, JXLayer l) { JTextComponent c = l.getView(); if ((c != null) && e.isPopupTrigger()) { c.requestFocus(true); JPopupMenu editMenu = new JPopupMenu(); editMenu.add(new CutAction()); editMenu.add(new CopyAction()); editMenu.add(new PasteAction()); Point pt = SwingUtilities.convertPoint(e .getComponent(), e.getPoint(), c); editMenu.show(c, pt.x, pt.y); e.consume(); } } }); } !!! Listing 5: CopyAction private class CopyAction extends AbstractAction { private final JTextComponent c; public CopyAction() { super("Kopieren"); c = textfield.getView(); } public void actionPerformed(ActionEvent e) { c.copy(); } @Override public boolean isEnabled() { return c.isEnabled() && (c.getSelectedText() != null); } } !!! Listing 6: Anonyme innere Klasse für Listing 4 @Override protected void paintLayer(Graphics2D g2, JXLayer l) { super.paintLayer(g2, l); String text = l.getView().getText().toLowerCase(); Color col = g2.getColor(); if ("rot".equals(text)) { g2.setColor(Color.red); } else if ("gelb".equals(text)) { g2.setColor(Color.yellow); } else if ("grün".equals(text)) { g2.setColor(Color.green); } g2.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_OVER, .3f)); g2.fillRect(0, 0, l.getWidth(), l.getHeight()); g2.setColor(col); }