|
| 1 | +/* |
| 2 | + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * - Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * - Neither the name of Oracle or the names of its |
| 16 | + * contributors may be used to endorse or promote products derived |
| 17 | + * from this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package components; |
| 33 | + |
| 34 | +/* |
| 35 | + * TabbedPaneDemo.java requires one additional file: |
| 36 | + * images/middle.gif. |
| 37 | + */ |
| 38 | + |
| 39 | +import javax.swing.JTabbedPane; |
| 40 | +import javax.swing.ImageIcon; |
| 41 | +import javax.swing.JLabel; |
| 42 | +import javax.swing.JPanel; |
| 43 | +import javax.swing.JFrame; |
| 44 | +import javax.swing.JComponent; |
| 45 | +import javax.swing.SwingUtilities; |
| 46 | +import javax.swing.UIManager; |
| 47 | +import java.awt.BorderLayout; |
| 48 | +import java.awt.Dimension; |
| 49 | +import java.awt.GridLayout; |
| 50 | +import java.awt.event.KeyEvent; |
| 51 | + |
| 52 | +public class TabbedPaneDemo extends JPanel { |
| 53 | + public TabbedPaneDemo() { |
| 54 | + super(new GridLayout(1, 1)); |
| 55 | + |
| 56 | + JTabbedPane tabbedPane = new JTabbedPane(); |
| 57 | + ImageIcon icon = createImageIcon("images/middle.gif"); |
| 58 | + |
| 59 | + JComponent panel1 = makeTextPanel("Panel #1"); |
| 60 | + tabbedPane.addTab("Tab 1", icon, panel1, |
| 61 | + "Does nothing"); |
| 62 | + tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); |
| 63 | + |
| 64 | + JComponent panel2 = makeTextPanel("Panel #2"); |
| 65 | + tabbedPane.addTab("This is Tab 2", icon, panel2, |
| 66 | + "Does twice as much nothing"); |
| 67 | + tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); |
| 68 | + |
| 69 | + JComponent panel3 = makeTextPanel("Panel #3"); |
| 70 | + tabbedPane.addTab("Tab 3", icon, panel3, |
| 71 | + "Still does nothing"); |
| 72 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 73 | + |
| 74 | + JComponent panel3b = makeTextPanel("Panel #3"); |
| 75 | + tabbedPane.addTab("Tab 3b", icon, panel3b, |
| 76 | + "Still does nothing"); |
| 77 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 78 | + |
| 79 | + JComponent panel3c = makeTextPanel("Panel #3"); |
| 80 | + tabbedPane.addTab("Tab 3", icon, panel3c, |
| 81 | + "Still does nothing"); |
| 82 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 83 | + |
| 84 | + JComponent panel3d = makeTextPanel("Panel #3"); |
| 85 | + tabbedPane.addTab("Tab 3", icon, panel3d, |
| 86 | + "Still does nothing"); |
| 87 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 88 | + |
| 89 | + JComponent panel3e = makeTextPanel("Panel #3"); |
| 90 | + tabbedPane.addTab("Tab 3", icon, panel3e, |
| 91 | + "Still does nothing"); |
| 92 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 93 | + |
| 94 | + JComponent panel3f = makeTextPanel("Panel #3"); |
| 95 | + tabbedPane.addTab("Tab 3", icon, panel3f, |
| 96 | + "Still does nothing"); |
| 97 | + tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + JComponent panel4 = makeTextPanel( |
| 102 | + "Panel #4 (has a preferred size of 410 x 50)."); |
| 103 | + panel4.setPreferredSize(new Dimension(410, 50)); |
| 104 | + tabbedPane.addTab("Tab 4", icon, panel4, |
| 105 | + "Does nothing at all"); |
| 106 | + tabbedPane.setMnemonicAt(3, KeyEvent.VK_4); |
| 107 | + |
| 108 | + //Add the tabbed pane to this panel. |
| 109 | + add(tabbedPane); |
| 110 | + |
| 111 | + //The following line enables to use scrolling tabs. |
| 112 | + // tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); |
| 113 | + tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); |
| 114 | + } |
| 115 | + |
| 116 | + protected JComponent makeTextPanel(String text) { |
| 117 | + JPanel panel = new JPanel(false); |
| 118 | + JLabel filler = new JLabel(text); |
| 119 | + filler.setHorizontalAlignment(JLabel.CENTER); |
| 120 | + panel.setLayout(new GridLayout(1, 1)); |
| 121 | + panel.add(filler); |
| 122 | + return panel; |
| 123 | + } |
| 124 | + |
| 125 | + /** Returns an ImageIcon, or null if the path was invalid. */ |
| 126 | + protected static ImageIcon createImageIcon(String path) { |
| 127 | + java.net.URL imgURL = TabbedPaneDemo.class.getResource(path); |
| 128 | + if (imgURL != null) { |
| 129 | + return new ImageIcon(imgURL); |
| 130 | + } else { |
| 131 | + System.err.println("Couldn't find file: " + path); |
| 132 | + return null; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Create the GUI and show it. For thread safety, |
| 138 | + * this method should be invoked from |
| 139 | + * the event dispatch thread. |
| 140 | + */ |
| 141 | + private static void createAndShowGUI() { |
| 142 | + //Create and set up the window. |
| 143 | + JFrame frame = new JFrame("TabbedPaneDemo"); |
| 144 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 145 | + |
| 146 | + //Add content to the window. |
| 147 | + frame.add(new TabbedPaneDemo(), BorderLayout.CENTER); |
| 148 | + |
| 149 | + //Display the window. |
| 150 | + frame.pack(); |
| 151 | + frame.setVisible(true); |
| 152 | + } |
| 153 | + |
| 154 | + public static void main(String[] args) { |
| 155 | + //Schedule a job for the event dispatch thread: |
| 156 | + //creating and showing this application's GUI. |
| 157 | + SwingUtilities.invokeLater(new Runnable() { |
| 158 | + public void run() { |
| 159 | + //Turn off metal's use of bold fonts |
| 160 | + UIManager.put("swing.boldMetal", Boolean.FALSE); |
| 161 | + createAndShowGUI(); |
| 162 | + } |
| 163 | + }); |
| 164 | + } |
| 165 | +} |
0 commit comments