2 // A panel capable of displaying one of many components at a time. The
3 // component to display is chosen by a row of tab buttons.
5 import java.util.Vector;
7 public class TabbedPanel extends Panel
9 TabSelector tab; // component for choosing panel
10 TabbedDisplayPanel disp; // where other panels are displayed
15 this(Util.body_hi, Util.dark_edge_hi, Util.body);
18 TabbedPanel(Color hi, Color lo, Color bk)
20 setLayout(new BorderLayout());
21 add("North",tab = new TabSelector(hi, lo, bk));
22 add("Center",disp = new TabbedDisplayPanel(hi, lo));
23 disp.setLayout(card = new CardLayout());
27 // Add a component to be chosen by a tab with the given name
28 void addItem(String n, Component c)
35 // Display a component in the panel
43 // Called back by a TabSelector object when the user clicks on a tab
50 class TabSelector extends Canvas
53 Vector name = new Vector();
55 Font font = new Font("timesRoman", Font.PLAIN, 12),
56 chfont = new Font(font.getName(), Font.BOLD, 13);
58 TabSelector(Color h, Color l, Color b)
60 hi = h; lo = l; bk = b;
63 void addItem(String n)
71 for(int i=0; i<name.size(); i++)
72 if (((String)name.elementAt(i)).equals(n)) {
78 public void paint(Graphics g)
80 if (g == null || name.size() == 0)
83 g.fillRect(0, 0, size().width, size().height);
84 int tw = size().width / name.size(),
86 for(int i=0; i<name.size(); i++) {
90 g.drawLine(x+tw-3, 1, x+tw-3, th-1);
91 g.drawLine(x+tw-4, 2, x+tw-4, th-1);
93 g.drawLine(x, 0, x, th-1);
94 g.drawLine(x+1, 0, x+1, th-1);
95 g.drawLine(x, 0, x+tw-4, 0);
96 g.drawLine(x, 1, x+tw-5, 1);
97 g.drawLine(x+tw-3, th-1, x+tw-1, th-1);
98 g.drawLine(x+tw-3, th-2, x+tw-1, th-2);
102 g.drawLine(x+tw-3, 6, x+tw-3, th-1);
103 g.drawLine(x+tw-4, 7, x+tw-4, th-1);
105 g.drawLine(x, 5, x, th-1);
106 g.drawLine(x+1, 5, x+1, th-1);
107 g.drawLine(x, 5, x+tw-4, 5);
108 g.drawLine(x, 6, x+tw-5, 6);
109 g.drawLine(x, th-1, x+tw-1, th-1);
110 g.drawLine(x, th-2, x+tw-1, th-2);
113 if (i == chosen) g.setFont(chfont);
114 else g.setFont(font);
115 String str = (String)name.elementAt(i);
116 int textw = g.getFontMetrics().stringWidth(str);
117 int texth = g.getFontMetrics().getHeight();
119 g.drawString(str, x+(tw-textw)/2, (th-texth)/2+texth);
123 public boolean mouseDown(Event evt, int x, int y)
125 if (name.size() == 0) return false;
126 chosen = x / (size().width / name.size());
127 paint(getGraphics());
128 ((TabbedPanel)getParent()).chose((String)name.elementAt(chosen));
132 public Dimension minimumSize()
134 return new Dimension(50,25);
137 public Dimension preferredSize()
139 return minimumSize();
143 class TabbedDisplayPanel extends Panel
148 TabbedDisplayPanel(Color h, Color l)
151 setLayout(card = new CardLayout());
155 // Add one component to the set of possibles to be shown
156 void addItem(String n, Component c)
162 // Display the named panel
163 void choose(String n)
165 ((CardLayout)getLayout()).show(this, n);
168 public Insets insets()
170 return new Insets(5,5,5,5);
173 public void paint(Graphics g)
176 g.drawLine(0, 0, 0, size().height-1);
177 g.drawLine(1, 0, 1, size().height-1);
179 g.drawLine(0, size().height-1, size().width-1, size().height-1);
180 g.drawLine(0, size().height-2, size().width-1, size().height-2);
181 g.drawLine(size().width-1, size().height-1, size().width-1, 0);
182 g.drawLine(size().width-2, size().height-1, size().width-2, 0);