Simple toolbar
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class SimpleToolbar extends JFrame {
public static final String FontNames[] = { "Serif", "SansSerif", "Courier" };
protected Font fonts[];
protected JFileChooser fileChooser = new JFileChooser();
protected JToolBar toolBar;
protected JComboBox cbFonts;
protected SmallToggleButton bBold;
protected SmallToggleButton bItalic;
public SimpleToolbar() {
super();
setSize(450, 350);
fonts = new Font[FontNames.length];
for (int k = 0; k < FontNames.length; k++)
fonts[k] = new Font(FontNames[k], Font.PLAIN, 12);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
createToolBar();
setVisible(true);
}
protected void createToolBar() {
ImageIcon iconNew = new ImageIcon("file_new.gif");
Action actionNew = new AbstractAction("New", iconNew) {
public void actionPerformed(ActionEvent e) {
;
}
};
ImageIcon iconOpen = new ImageIcon("file_open.gif");
Action actionOpen = new AbstractAction("Open...", iconOpen) {
public void actionPerformed(ActionEvent e) {
}
};
ImageIcon iconSave = new ImageIcon("file_save.gif");
Action actionSave = new AbstractAction("Save...", iconSave) {
public void actionPerformed(ActionEvent e) {
}
};
Action actionExit = new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
toolBar = new JToolBar();
JButton bNew = new SmallButton(actionNew, "New text");
toolBar.add(bNew);
JButton bOpen = new SmallButton(actionOpen, "Open text file");
toolBar.add(bOpen);
JButton bSave = new SmallButton(actionSave, "Save text file");
toolBar.add(bSave);
ActionListener fontListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
};
toolBar.addSeparator();
cbFonts = new JComboBox(FontNames);
cbFonts.setMaximumSize(cbFonts.getPreferredSize());
cbFonts.setToolTipText("Available fonts");
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = cbFonts.getSelectedIndex();
if (index < 0)
return;
}
};
cbFonts.addActionListener(lst);
toolBar.add(cbFonts);
toolBar.addSeparator();
ImageIcon img1 = new ImageIcon("font_bold1.gif");
ImageIcon img2 = new ImageIcon("font_bold2.gif");
bBold = new SmallToggleButton(false, img1, img2, "Bold font");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
};
bBold.addActionListener(lst);
toolBar.add(bBold);
img1 = new ImageIcon("font_italic1.gif");
img2 = new ImageIcon("font_italic2.gif");
bItalic = new SmallToggleButton(false, img1, img2, "Italic font");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
};
bItalic.addActionListener(lst);
toolBar.add(bItalic);
getContentPane().add(toolBar, BorderLayout.NORTH);
}
public static void main(String[] a){
new SimpleToolbar();
}
}
class SmallButton extends JButton implements MouseListener {
protected Border m_raised;
protected Border m_lowered;
protected Border m_inactive;
public SmallButton(Action act, String tip) {
super((Icon) act.getValue(Action.SMALL_ICON));
m_raised = new BevelBorder(BevelBorder.RAISED);
m_lowered = new BevelBorder(BevelBorder.LOWERED);
m_inactive = new EmptyBorder(2, 2, 2, 2);
setBorder(m_inactive);
setMargin(new Insets(1, 1, 1, 1));
setToolTipText(tip);
addActionListener(act);
addMouseListener(this);
setRequestFocusEnabled(false);
}
public float getAlignmentY() {
return 0.5f;
}
public void mousePressed(MouseEvent e) {
setBorder(m_lowered);
}
public void mouseReleased(MouseEvent e) {
setBorder(m_inactive);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
setBorder(m_raised);
}
public void mouseExited(MouseEvent e) {
setBorder(m_inactive);
}
}
class SmallToggleButton extends JToggleButton implements ItemListener {
protected Border raised;
protected Border lowered;
public SmallToggleButton(boolean selected, ImageIcon imgUnselected,
ImageIcon imgSelected, String tip) {
super(imgUnselected, selected);
setHorizontalAlignment(CENTER);
setBorderPainted(true);
raised = new BevelBorder(BevelBorder.RAISED);
lowered = new BevelBorder(BevelBorder.LOWERED);
setBorder(selected ? lowered : raised);
setMargin(new Insets(1, 1, 1, 1));
setToolTipText(tip);
setRequestFocusEnabled(false);
setSelectedIcon(imgSelected);
addItemListener(this);
}
public float getAlignmentY() {
return 0.5f;
}
public void itemStateChanged(ItemEvent e) {
setBorder(isSelected() ? lowered : raised);
}
}
Related examples in the same category