JCardPane
Aus Byte-Welt Wiki
Version vom 15. Oktober 2009, 21:29 Uhr von AndreUhres (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<code=java>→* JCardPane.java * wraps a JTabbedPane, optionally hiding the tabs. * The main method shows an example: import java.awt.*; import java.awt.ev…“)
<code=java>/*
* JCardPane.java * wraps a JTabbedPane, optionally hiding the tabs. * The main method shows an example */
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.TabbedPaneUI; import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class JCardPane extends JComponent {
private JTabbedPane pane; private boolean requestFocus = true; private CardAction nextAction; private CardAction previousAction; private TabbedPaneUI uiTabbed; private TabbedPaneUI uiNoTabs = new BasicTabbedPaneUI() {
@Override protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight) { return 0; }
@Override protected Insets getContentBorderInsets(int tabPlacement) { return new Insets(0, 0, 0, 0); }
@Override protected MouseListener createMouseListener() { return null; }
@Override protected void installKeyboardActions() { }
@Override protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { return 0; } };
public JCardPane() { this(false); }
public JCardPane(boolean tabbed) { pane = new JTabbedPane(); uiTabbed = pane.getUI(); if (!tabbed) { hideTabs(); } pane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) { if (requestFocus) { Runnable transferFocus = new Runnable() {
public void run() { pane.getSelectedComponent().transferFocus(); } }; SwingUtilities.invokeLater(transferFocus); } if (nextAction != null) { nextAction.setEnabled(hasNext()); } if (previousAction != null) { previousAction.setEnabled(hasPrevious()); } } }); setLayout(new BorderLayout()); add(pane); }
public int first() { return show(0); }
public int last() { return show(-1); }
public int next() { return show(pane.getSelectedIndex() + 1); }
public int previous() { return show(pane.getSelectedIndex() - 1); }
public int show(final int index) { if (pane.getTabCount() == 0) { return -1; } if (index >= pane.getTabCount()) { pane.setSelectedIndex(0); return 0;
} if (index < 0) { pane.setSelectedIndex(pane.getTabCount() - 1); return pane.getTabCount() - 1;
} pane.setSelectedIndex(index); return index; }
public int show(final String name) { int index = pane.indexOfTab(name); if (index >= 0) { pane.setSelectedIndex(index); } return index; }
public int show(final JComponent card) { int index = pane.indexOfComponent(card); if (index < 0) { index = pane.getTabCount(); pane.addTab(card.getName(), card); } pane.setSelectedIndex(index); return index; }
public boolean hasNext() { return pane.getSelectedIndex() < pane.getComponentCount() - 1; }
public boolean hasPrevious() { return pane.getSelectedIndex() > 0; }
public Component getCurrent() { return pane.getSelectedComponent(); }
public void setRequestFocus(boolean b) { requestFocus = b; }
public boolean isRequestFocus() { return requestFocus; }
public void addChangeListener(ChangeListener l) { pane.addChangeListener(l); }
public void removeChangeListener(ChangeListener l) { pane.removeChangeListener(l); }
public void hideTabs() { pane.setUI(uiNoTabs); }
public void showTabs() { pane.setUI(uiTabbed); }
public Action getNextAction() { if (nextAction == null) { nextAction = new CardAction("Next", true); nextAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_N); nextAction.setEnabled(hasNext()); }
return nextAction; }
public Action getPreviousAction() { if (previousAction == null) { previousAction = new CardAction("Previous", false); previousAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_P); previousAction.setEnabled(hasPrevious()); }
return previousAction; }
class CardAction extends AbstractAction {
private boolean isNext;
public CardAction(String text, boolean isNext) { super(text); this.isNext = isNext; putValue(Action.SHORT_DESCRIPTION, getValue(Action.NAME)); }
public void actionPerformed(ActionEvent e) { if (isNext) { next(); } else { previous(); } } }
//for testing only: public static void main(final String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { ex.printStackTrace(); } Runnable gui = new Runnable() {
@Override public void run() { final JFrame f = new JFrame("JCardPane Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400, 150); f.setLocationRelativeTo(null); final JCardPane cardPane = new JCardPane(); f.add(cardPane, BorderLayout.CENTER); class Card extends JPanel {
public Card(final String name) { JTextField tf = new JTextField(20); tf.setText(name); add(tf); setName(name); } } for (int i = 0; i < 5; i++) { cardPane.show(new Card(String.valueOf(i + 1))); } cardPane.first(); JPanel control = new JPanel(); control.add(new JButton(cardPane.getPreviousAction())); control.add(new JButton(cardPane.getNextAction())); final JToggleButton btTabbed = new JToggleButton("Tabbed"); btTabbed.setFocusable(false); control.add(btTabbed); btTabbed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { if (btTabbed.isSelected()) { cardPane.showTabs(); } else { cardPane.hideTabs(); } } }); f.add(control, BorderLayout.SOUTH); f.setVisible(true); } }; //GUI must start on EventDispatchThread: SwingUtilities.invokeLater(gui); }
} </code=java>