Mnemonic Tabbed Pane Example : TabbedPane « Swing Components « Java






Mnemonic Tabbed Pane Example

Mnemonic Tabbed Pane Example
// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html

/* (swing1.1.1beta2) */


import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.metal.MetalTabbedPaneUI;

/**
 * @version 1.1 06/02/99
 */
public class MnemonicTabbedPaneExample extends JPanel {

  public MnemonicTabbedPaneExample() {
    setLayout(new BorderLayout());

    MnemonicTabbedPane tabbedPane = new MnemonicTabbedPane();
    String[] tabs = { "Zero", "One", "Two", "Three", "Four" };
    char[] ms = { 'Z', 'O', 'T', 'h', 'F' };
    int[] keys = { KeyEvent.VK_0, KeyEvent.VK_1, KeyEvent.VK_2,
        KeyEvent.VK_3, KeyEvent.VK_4, };

    for (int i = 0; i < tabs.length; i++) {
      tabbedPane.addTab(tabs[i], createPane(tabs[i]));
      tabbedPane.setMnemonicAt(i, ms[i]);
      //tabbedPane.setMnemonicAt(i, keys[i]);
    }
    tabbedPane.setSelectedIndex(0);
    add(tabbedPane, BorderLayout.CENTER);
    add(new JButton("button"), BorderLayout.SOUTH);
  }

  JPanel createPane(final String s) {
    JButton abcButton = new JButton("Abc");
    abcButton.setMnemonic('A');
    JButton xyzButton = new JButton("Xyz");
    xyzButton.setMnemonic('X');

    JPanel p = new JPanel();
    p.add(new JLabel(s));
    p.add(abcButton);
    p.add(xyzButton);
    return p;
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("TabbedPane Test");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.getContentPane().add(new MnemonicTabbedPaneExample());
    frame.setSize(250, 130);
    frame.setVisible(true);
  }
}

class MnemonicTabbedPane extends JTabbedPane {
  Hashtable mnemonics = null;

  int condition;

  public MnemonicTabbedPane() {
    setUI(new MnemonicTabbedPaneUI());
    mnemonics = new Hashtable();

    // I don't know which one is more suitable for mnemonic action.
    //setMnemonicCondition(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    setMnemonicCondition(WHEN_IN_FOCUSED_WINDOW);
  }

  public void setMnemonicAt(int index, char c) {
    int key = (int) c;
    if ('a' <= key && key <= 'z') {
      key -= ('a' - 'A');
    }
    setMnemonicAt(index, key);
  }

  public void setMnemonicAt(int index, int keyCode) {
    ActionListener action = new MnemonicAction(index);
    KeyStroke stroke = KeyStroke
        .getKeyStroke(keyCode, ActionEvent.ALT_MASK);
    registerKeyboardAction(action, stroke, condition);
    mnemonics.put(new Integer(index), new Integer(keyCode));
  }

  public int getMnemonicAt(int index) {
    int keyCode = 0;
    Integer m = (Integer) mnemonics.get(new Integer(index));
    if (m != null) {
      keyCode = m.intValue();
    }
    return keyCode;
  }

  public void setMnemonicCondition(int condition) {
    this.condition = condition;
  }

  public int getMnemonicCondition() {
    return condition;
  }

  class MnemonicAction implements ActionListener {
    int index;

    public MnemonicAction(int index) {
      this.index = index;
    }

    public void actionPerformed(ActionEvent e) {
      MnemonicTabbedPane tabbedPane = (MnemonicTabbedPane) e.getSource();
      tabbedPane.setSelectedIndex(index);
      tabbedPane.requestFocus();
    }
  }

  class MnemonicTabbedPaneUI extends MetalTabbedPaneUI {
    protected void paintText(Graphics g, int tabPlacement, Font font,
        FontMetrics metrics, int tabIndex, String title,
        Rectangle textRect, boolean isSelected) {
      g.setFont(font);
      MnemonicTabbedPane mtabPane = (MnemonicTabbedPane) tabPane;
      if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
        g.setColor(tabPane.getForegroundAt(tabIndex));
        BasicGraphicsUtils.drawString(g, title, mtabPane
            .getMnemonicAt(tabIndex), textRect.x, textRect.y
            + metrics.getAscent());
      } else {
        g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
        BasicGraphicsUtils.drawString(g, title, mtabPane
            .getMnemonicAt(tabIndex), textRect.x, textRect.y
            + metrics.getAscent());
        g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
        BasicGraphicsUtils.drawString(g, title, mtabPane
            .getMnemonicAt(tabIndex), textRect.x - 1, textRect.y
            + metrics.getAscent() - 1);
      }
    }
  }
}
           
       








Related examples in the same category

1.Swing Windows (Eclipse) like TabbedPanelSwing Windows (Eclipse) like TabbedPanel
2.Tab Color ExampleTab Color Example
3.Single Row Tabbed Pane Example 1Single Row Tabbed Pane Example 1
4.Single Row Tabbed Pane Example 2Single Row Tabbed Pane Example 2
5.Single Row Tabbed Pane Example 4Single Row Tabbed Pane Example 4
6.Color TabbedPane ExampleColor TabbedPane Example
7.Color TabbedPane Example 2Color TabbedPane Example 2
8.Color TabbedPane Example 3Color TabbedPane Example 3