Button2.java
| Code: |
/**
* Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
* All Rights Reserved.
*
* ===================================================
* This program contains code from the book "Swing"
* 2nd Edition by Matthew Robinson and Pavel Vorobiev
* http://www.spindoczine.com/sbe
* ===================================================
*
* The above paragraph must be included in full, unmodified
* and completely intact in the beginning of any source code
* file that references, copies or uses (in any way, shape
* or form) code contained in this file.
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import malachite.*;
public class Button2 extends JFrame
{
protected Hashtable m_lfs;
public Button2()
{
super("Look and Feel [Custom UI Delegates]");
setSize(400, 300);
getContentPane().setLayout(new FlowLayout());
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
JPanel p = new JPanel();
JButton bt1 = new JButton("Click Me");
p.add(bt1);
JButton bt2 = new JButton("Don't Touch Me");
p.add(bt2);
getContentPane().add(p);
p = new JPanel();
JCheckBox chk1 = new JCheckBox("I'm checked");
chk1.setSelected(true);
p.add(chk1);
JCheckBox chk2 = new JCheckBox("I'm unchecked");
chk2.setSelected(false);
p.add(chk2);
getContentPane().add(p);
p = new JPanel();
ButtonGroup grp = new ButtonGroup();
JRadioButton rd1 = new JRadioButton("Option 1");
rd1.setSelected(true);
p.add(rd1);
grp.add(rd1);
JRadioButton rd2 = new JRadioButton("Option 2");
p.add(rd2);
grp.add(rd2);
JRadioButton rd3 = new JRadioButton("Option 3");
p.add(rd3);
grp.add(rd3);
getContentPane().add(p);
JTextArea txt = new JTextArea(5, 30);
JScrollPane sp = new JScrollPane(txt);
getContentPane().add(sp);
}
protected JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File");
mFile.setMnemonic('f');
JMenuItem mItem = new JMenuItem("Exit");
mItem.setMnemonic('x');
ActionListener lstExit = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
mItem.addActionListener(lstExit);
mFile.add(mItem);
menuBar.add(mFile);
ActionListener lst = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
Object obj = m_lfs.get(str);
if (obj != null)
try
{
String className = (String)obj;
Class lnfClass = Class.forName(className);
UIManager.setLookAndFeel(
(LookAndFeel)(lnfClass.newInstance()));
SwingUtilities.updateComponentTreeUI(
Button2.this);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println(ex.toString());
}
}
};
m_lfs = new Hashtable();
UIManager.LookAndFeelInfo lfs[] =
UIManager.getInstalledLookAndFeels();
JMenu mLF = new JMenu("Look&Feel");
mLF.setMnemonic('l');
for (int k = 0; k < lfs.length; k++ )
{
String name = lfs[k].getName();
JMenuItem lf = new JMenuItem(name);
m_lfs.put(name, lfs[k].getClassName());
lf.addActionListener(lst);
mLF.add(lf);
}
menuBar.add(mLF);
return menuBar;
}
public static void main(String argv[])
{
try
{
LookAndFeel malachite = new malachite.MalachiteLF();
UIManager.LookAndFeelInfo info =
new UIManager.LookAndFeelInfo(malachite.getName(),
malachite.getClass().getName());
UIManager.installLookAndFeel(info);
UIManager.setLookAndFeel(malachite);
}
catch (Exception ex)
{
ex.printStackTrace();
}
Button2 frame = new Button2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
|
MalachiteBorder.java
| Code: |
/**
* Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
* All Rights Reserved.
*
* ===================================================
* This program contains code from the book "Swing"
* 2nd Edition by Matthew Robinson and Pavel Vorobiev
* http://www.spindoczine.com/sbe
* ===================================================
*
* The above paragraph must be included in full, unmodified
* and completely intact in the beginning of any source code
* file that references, copies or uses (in any way, shape
* or form) code contained in this file.
*/
package malachite;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class MalachiteBorder
implements Border
{
public static final int RAISED = 0;
public static final int LOWERED = 1;
static final String IMAGE_DIR = "malachite"+java.io.File.separator;
static final ImageIcon IMAGE_NW = new ImageIcon(
IMAGE_DIR+"nw.gif");
static final ImageIcon IMAGE_N = new ImageIcon(
IMAGE_DIR+"n.gif");
static final ImageIcon IMAGE_NE = new ImageIcon(
IMAGE_DIR+"ne.gif");
static final ImageIcon IMAGE_E = new ImageIcon(
IMAGE_DIR+"e.gif");
static final ImageIcon IMAGE_SE = new ImageIcon(
IMAGE_DIR+"se.gif");
static final ImageIcon IMAGE_S = new ImageIcon(
IMAGE_DIR+"s.gif");
static final ImageIcon IMAGE_SW = new ImageIcon(
IMAGE_DIR+"sw.gif");
static final ImageIcon IMAGE_W = new ImageIcon(
IMAGE_DIR+"w.gif");
static final ImageIcon IMAGE_L_NW = new ImageIcon(
IMAGE_DIR+"l_nw.gif");
static final ImageIcon IMAGE_L_N = new ImageIcon(
IMAGE_DIR+"l_n.gif");
static final ImageIcon IMAGE_L_NE = new ImageIcon(
IMAGE_DIR+"l_ne.gif");
static final ImageIcon IMAGE_L_E = new ImageIcon(
IMAGE_DIR+"l_e.gif");
static final ImageIcon IMAGE_L_SE = new ImageIcon(
IMAGE_DIR+"l_se.gif");
static final ImageIcon IMAGE_L_S = new ImageIcon(
IMAGE_DIR+"l_s.gif");
static final ImageIcon IMAGE_L_SW = new ImageIcon(
IMAGE_DIR+"l_sw.gif");
static final ImageIcon IMAGE_L_W = new ImageIcon(
IMAGE_DIR+"l_w.gif");
protected int m_w = 7;
protected int m_h = 7;
protected boolean m_isRaised = true;
public MalachiteBorder()
{
}
public MalachiteBorder(int type)
{
if (type != RAISED && type != LOWERED)
throw new IllegalArgumentException(
"Type must be RAISED or LOWERED");
m_isRaised = (type == RAISED);
}
public Insets getBorderInsets(Component c)
{
return new Insets(m_h, m_w, m_h, m_w);
}
public boolean isBorderOpaque()
{
return true;
}
public void paintBorder(Component c, Graphics g,
int x, int y, int w, int h)
{
int x1 = x+m_w;
int x2 = x+w-m_w;
int y1 = y+m_h;
int y2 = y+h-m_h;
int xx, yy;
if (m_isRaised)
{
for (xx=x1; xx<=x2; xx += IMAGE_N.getIconWidth())
g.drawImage(IMAGE_N.getImage(), xx, y, c);
for (yy=y1; yy<=y2; yy += IMAGE_E.getIconHeight())
g.drawImage(IMAGE_E.getImage(), x2, yy, c);
for (xx=x1; xx<=x2; xx += IMAGE_S.getIconWidth())
g.drawImage(IMAGE_S.getImage(), xx, y2, c);
for (yy=y1; yy<=y2; yy += IMAGE_W.getIconHeight())
g.drawImage(IMAGE_W.getImage(), x, yy, c);
g.drawImage(IMAGE_NW.getImage(), x, y, c);
g.drawImage(IMAGE_NE.getImage(), x2, y, c);
g.drawImage(IMAGE_SE.getImage(), x2, y2, c);
g.drawImage(IMAGE_SW.getImage(), x, y2, c);
}
else
{
for (xx=x1; xx<=x2; xx += IMAGE_L_N.getIconWidth())
g.drawImage(IMAGE_L_N.getImage(), xx, y, c);
for (yy=y1; yy<=y2; yy += IMAGE_L_E.getIconHeight())
g.drawImage(IMAGE_L_E.getImage(), x2, yy, c);
for (xx=x1; xx<=x2; xx += IMAGE_L_S.getIconWidth())
g.drawImage(IMAGE_L_S.getImage(), xx, y2, c);
for (yy=y1; yy<=y2; yy += IMAGE_L_W.getIconHeight())
g.drawImage(IMAGE_L_W.getImage(), x, yy, c);
g.drawImage(IMAGE_L_NW.getImage(), x, y, c);
g.drawImage(IMAGE_L_NE.getImage(), x2, y, c);
g.drawImage(IMAGE_L_SE.getImage(), x2, y2, c);
g.drawImage(IMAGE_L_SW.getImage(), x, y2, c);
}
}
}
|
MalachiteButtonUI.java
| Code: |
/**
* Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
* All Rights Reserved.
*
* ===================================================
* This program contains code from the book "Swing"
* 2nd Edition by Matthew Robinson and Pavel Vorobiev
* http://www.spindoczine.com/sbe
* ===================================================
*
* The above paragraph must be included in full, unmodified
* and completely intact in the beginning of any source code
* file that references, copies or uses (in any way, shape
* or form) code contained in this file.
*/
package malachite;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
public class MalachiteButtonUI
extends BasicButtonUI
implements java.io.Serializable, MouseListener, KeyListener
{
private final static MalachiteButtonUI m_buttonUI =
new MalachiteButtonUI();
protected Border m_borderRaised = null;
protected Border m_borderLowered = null;
protected Color m_backgroundNormal = null;
protected Color m_backgroundPressed = null;
protected Color m_foregroundNormal = null;
protected Color m_foregroundActive = null;
protected Color m_focusBorder = null;
public MalachiteButtonUI() {}
public static ComponentUI createUI( JComponent c )
{
return m_buttonUI;
}
public void installUI(JComponent c)
{
super.installUI(c);
m_borderRaised = UIManager.getBorder(
"Button.border");
m_borderLowered = UIManager.getBorder(
"Button.borderPressed");
m_backgroundNormal = UIManager.getColor(
"Button.background");
m_backgroundPressed = UIManager.getColor(
"Button.pressedBackground");
m_foregroundNormal = UIManager.getColor(
"Button.foreground");
m_foregroundActive = UIManager.getColor(
"Button.activeForeground");
m_focusBorder = UIManager.getColor(
"Button.focusBorder");
c.addMouseListener(this);
c.addKeyListener(this);
}
public void uninstallUI(JComponent c)
{
super.uninstallUI(c);
c.removeMouseListener(this);
c.removeKeyListener(this);
}
public void paint(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton) c;
Dimension d = b.getSize();
g.setFont(c.getFont());
FontMetrics fm = g.getFontMetrics();
g.setColor(b.getForeground());
String caption = b.getText();
int x = (d.width - fm.stringWidth(caption))/2;
int y = (d.height + fm.getAscent())/2;
g.drawString(caption, x, y);
if (b.isFocusPainted() && b.hasFocus())
{
g.setColor(m_focusBorder);
Insets bi = b.getBorder().getBorderInsets(b);
g.drawRect(bi.left, bi.top, d.width-bi.left-bi.right-1,
d.height-bi.top-bi.bottom-1);
}
}
public Dimension getPreferredSize(JComponent c)
{
Dimension d = super.getPreferredSize(c);
if (m_borderRaised != null)
{
Insets ins = m_borderRaised.getBorderInsets(c);
d.setSize(d.width+ins.left+ins.right,
d.height+ins.top+ins.bottom);
}
return d;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setBorder(m_borderLowered);
c.setBackground(m_backgroundPressed);
}
public void mouseReleased(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setBorder(m_borderRaised);
c.setBackground(m_backgroundNormal);
}
public void mouseEntered(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setForeground(m_foregroundActive);
c.repaint();
}
public void mouseExited(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setForeground(m_foregroundNormal);
c.repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_SPACE)
{
JComponent c = (JComponent)e.getComponent();
c.setBorder(m_borderLowered);
c.setBackground(m_backgroundPressed);
}
}
public void keyReleased(KeyEvent e)
{
int code = e.getKeyCode();
if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_SPACE)
{
JComponent c = (JComponent)e.getComponent();
c.setBorder(m_borderRaised);
c.setBackground(m_backgroundNormal);
}
}
}
|
MalachiteCheckBoxUI.java
| Code: |
/**
* Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
* All Rights Reserved.
*
* ===================================================
* This program contains code from the book "Swing"
* 2nd Edition by Matthew Robinson and Pavel Vorobiev
* http://www.spindoczine.com/sbe
* ===================================================
*
* The above paragraph must be included in full, unmodified
* and completely intact in the beginning of any source code
* file that references, copies or uses (in any way, shape
* or form) code contained in this file.
*/
package malachite;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
public class MalachiteCheckBoxUI
extends BasicCheckBoxUI
implements java.io.Serializable, MouseListener
{
private final static MalachiteCheckBoxUI m_buttonUI =
new MalachiteCheckBoxUI();
protected Color m_backgroundNormal = null;
protected Color m_foregroundNormal = null;
protected Color m_foregroundActive = null;
protected Icon m_checkedIcon = null;
protected Icon m_uncheckedIcon = null;
protected Icon m_pressedCheckedIcon = null;
protected Icon m_pressedUncheckedIcon = null;
protected Color m_focusBorder = null;
protected int m_textIconGap = -1;
public MalachiteCheckBoxUI() {}
public static ComponentUI createUI( JComponent c )
{
return m_buttonUI;
}
public void installUI(JComponent c)
{
super.installUI(c);
m_backgroundNormal = UIManager.getColor(
"CheckBox.background");
m_foregroundNormal = UIManager.getColor(
"CheckBox.foreground");
m_foregroundActive = UIManager.getColor(
"CheckBox.activeForeground");
m_checkedIcon = UIManager.getIcon(
"CheckBox.iconChecked");
m_uncheckedIcon = UIManager.getIcon(
"CheckBox.icon");
m_pressedCheckedIcon = UIManager.getIcon(
"CheckBox.iconPressedChecked");
m_pressedUncheckedIcon = UIManager.getIcon(
"CheckBox.iconPressed");
m_focusBorder = UIManager.getColor(
"CheckBox.focusBorder");
m_textIconGap = UIManager.getInt(
"CheckBox.textIconGap");
c.setBackground(m_backgroundNormal);
c.addMouseListener(this);
}
public void uninstallUI(JComponent c)
{
super.uninstallUI(c);
c.removeMouseListener(this);
}
public void paint(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
Dimension d = b.getSize();
g.setFont(c.getFont());
FontMetrics fm = g.getFontMetrics();
Icon icon = m_uncheckedIcon;
if (model.isPressed() && model.isSelected())
icon = m_pressedCheckedIcon;
else if (model.isPressed() && !model.isSelected())
icon = m_pressedUncheckedIcon;
else if (!model.isPressed() && model.isSelected())
icon = m_checkedIcon;
g.setColor(b.getForeground());
int x = 0;
int y = (d.height - icon.getIconHeight())/2;
icon.paintIcon(c, g, x, y);
String caption = b.getText();
x = icon.getIconWidth() + m_textIconGap;
y = (d.height + fm.getAscent())/2;
g.drawString(caption, x, y);
if (b.isFocusPainted() && b.hasFocus())
{
g.setColor(m_focusBorder);
Insets bi = b.getBorder().getBorderInsets(b);
g.drawRect(x-2, y-fm.getAscent()-2, d.width-x,
fm.getAscent()+fm.getDescent()+4);
}
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setForeground(m_foregroundActive);
c.repaint();
}
public void mouseExited(MouseEvent e)
{
JComponent c = (JComponent)e.getComponent();
c.setForeground(m_foregroundNormal);
c.repaint();
}
}
|
MalachiteLF.java
| Code: |
/**
* Copyright 1999-2002 Matthew Robinson and Pavel Vorobiev.
* All Rights Reserved.
*
* ===================================================
* This program contains code from the book "Swing"
* 2nd Edition by Matthew Robinson and Pavel Vorobiev
* http://www.spindoczine.com/sbe
* ===================================================
*
* The above paragraph must be included in full, unmodified
* and completely intact in the beginning of any source code
* file that references, copies or uses (in any way, shape
* or form) code contained in this file.
*/
package malachite;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
public class MalachiteLF
extends BasicLookAndFeel
implements java.io.Serializable
{
static final String IMAGE_DIR = "malachite"+java.io.File.separator;
public String getID()
{
return "Malachite";
}
public String getName()
{
return "Malachite";
}
public String getDescription()
{
return "Sample L&F from Swing";
}
public boolean isNativeLookAndFeel()
{
return false;
}
public boolean isSupportedLookAndFeel()
{
return true;
}
// NEW
protected void initClassDefaults(UIDefaults table)
{
super.initClassDefaults(table);
putDefault(table, "ButtonUI");
putDefault(table, "CheckBoxUI");
putDefault(table, "RadioButtonUI");
}
protected void putDefault(UIDefaults table, String uiKey)
{
try
{
String className = "malachite.Malachite"+uiKey;
Class buttonClass = Class.forName(className);
table.put(uiKey, className);
table.put(className, buttonClass);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
protected void initComponentDefaults(UIDefaults table)
{
super.initComponentDefaults(table);
ColorUIResource commonBackground =
new ColorUIResource(152, 208, 128);
ColorUIResource commonForeground =
new ColorUIResource(0, 0, 0);
ColorUIResource buttonBackground =
new ColorUIResource(4, 108, 2);
ColorUIResource buttonForeground =
new ColorUIResource(236, 236, 0);
ColorUIResource menuBackground =
new ColorUIResource(128, 192, 128);
ColorUIResource activeForeground =
new ColorUIResource(0, 128, 0);
ColorUIResource focusBorder =
new ColorUIResource(255, 255, 255);
BorderUIResource borderRaised = new
BorderUIResource(new MalachiteBorder(
MalachiteBorder.RAISED));
BorderUIResource borderLowered = new
BorderUIResource(new MalachiteBorder(
MalachiteBorder.LOWERED));
FontUIResource commonFont = new
FontUIResource("Arial", Font.BOLD, 12 );
Icon ubox = new ImageIcon(IMAGE_DIR+"ubox.gif");
Icon ubull = new ImageIcon(IMAGE_DIR+"ubull.gif");
// NEW
Icon cbox = new ImageIcon(IMAGE_DIR+"cbox.gif");
Icon pcbox = new ImageIcon(IMAGE_DIR+"p_cbox.gif");
Icon pubox = new ImageIcon(IMAGE_DIR+"p_ubox.gif");
Icon cbull = new ImageIcon(IMAGE_DIR+"cbull.gif");
Icon pcbull = new ImageIcon(IMAGE_DIR+"p_cbull.gif");
Icon pubull = new ImageIcon(IMAGE_DIR+"p_ubull.gif");
Object[] defaults =
{
"Button.font", commonFont,
"Button.background", buttonBackground,
"Button.foreground", buttonForeground,
"Button.border", borderRaised,
"Button.margin", new InsetsUIResource(8, 8, 8, 8),
"Button.textIconGap", new Integer(4),
"Button.textShiftOffset", new Integer(2),
// NEW
"Button.focusBorder", focusBorder,
"Button.borderPressed", borderLowered,
"Button.activeForeground", new
ColorUIResource(255, 255, 255),
"Button.pressedBackground", new
ColorUIResource(0, 96, 0),
"CheckBox.font", commonFont,
"CheckBox.background", commonBackground,
"CheckBox.foreground", commonForeground,
"CheckBox.icon", new IconUIResource(ubox),
// NEW
"CheckBox.focusBorder", focusBorder,
"CheckBox.activeForeground", activeForeground,
"CheckBox.iconPressed", new IconUIResource(pubox),
"CheckBox.iconChecked", new IconUIResource(cbox),
"CheckBox.iconPressedChecked", new IconUIResource(pcbox),
"CheckBox.textIconGap", new Integer(4),
"MenuBar.font", commonFont,
"MenuBar.background", menuBackground,
"MenuBar.foreground", commonForeground,
"Menu.font", commonFont,
"Menu.background", menuBackground,
"Menu.foreground", commonForeground,
"Menu.selectionBackground", buttonBackground,
"Menu.selectionForeground", buttonForeground,
"MenuItem.font", commonFont,
"MenuItem.background", menuBackground,
"MenuItem.foreground", commonForeground,
"MenuItem.selectionBackground", buttonBackground,
"MenuItem.selectionForeground", buttonForeground,
"MenuItem.margin", new InsetsUIResource(2, 2, 2, 2),
"Panel.background", commonBackground,
"Panel.foreground", commonForeground,
"RadioButton.font", commonFont,
"RadioButton.background", commonBackground,
"RadioButton.foreground", commonForeground,
"RadioButton.icon", new IconUIResource(ubull),
// NEW
"RadioButton.focusBorder", focusBorder,
"RadioButton.activeForeground", activeForeground,
"RadioButton.iconPressed", new IconUIResource(pubull),
"RadioButton.iconChecked", new IconUIResource(cbull),
"RadioButton.iconPressedChecked", new IconUIResource(pcbull),
"RadioButton.textIconGap", new Integer(4),
"ScrollPane.margin", new InsetsUIResource(8, 8, 8, 8),
"ScrollPane.border", borderLowered,
"ScrollPane.background", commonBackground,
"ScrollBar.track", menuBackground,
"ScrollBar.thumb", buttonBackground
};
table.putDefaults( defaults );
}
}
|
compile.bat
| Code: |
javac *.java -deprecation
if errorlevel 1 goto error:
javac Malachite\*.java -deprecation
if errorlevel 1 goto error:
run.bat
:error
pause
|
run.bat
| Code: |
java Button2
if errorlevel 1 pause
|
All the code from this book and all image files are here:
http://javafaq.nu/free-swing-book/examples.zip |
|