001: /*
002: * ComponentTitledPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Insets;
026: import java.awt.Rectangle;
027:
028: import javax.swing.JComponent;
029: import javax.swing.JPanel;
030:
031: /* ----------------------------------------------------------
032: * CVS NOTE: Changes to the CVS repository prior to the
033: * release of version 3.0.0beta1 has meant a
034: * resetting of CVS revision numbers.
035: * ----------------------------------------------------------
036: */
037:
038: /**
039: * This class provides a panel with a component
040: * as the title within a titled border.<br>
041: * Modified example from
042: * http://www2.gol.com/users/tame/swing/examples/BorderExamples1.html
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.5 $
046: * @date $Date: 2006/06/14 15:07:18 $
047: */
048: public class ComponentTitledPanel extends JPanel {
049:
050: protected ComponentTitledBorder border;
051: protected JComponent component;
052: protected JPanel panel;
053:
054: public ComponentTitledPanel() {
055: this (null);
056: }
057:
058: public ComponentTitledPanel(JComponent component) {
059: super (new BorderLayout());
060: this .component = component;
061: border = new ComponentTitledBorder(component);
062: setBorder(border);
063: panel = new JPanel();
064:
065: if (component != null) {
066: add(component);
067: }
068: add(panel);
069: }
070:
071: public JComponent getTitleComponent() {
072: return component;
073: }
074:
075: public void setTitleComponent(JComponent newComponent) {
076: if (component != null) {
077: remove(component);
078: }
079: add(newComponent);
080: border.setTitleComponent(newComponent);
081: component = newComponent;
082: }
083:
084: public JPanel getContentPane() {
085: return panel;
086: }
087:
088: public void doLayout() {
089: Insets insets = getInsets();
090: Rectangle rect = getBounds();
091:
092: rect.x = 0;
093: rect.y = 0;
094:
095: Rectangle compR = border.getComponentRect(rect, insets);
096: component.setBounds(compR);
097: rect.x += insets.left;
098: rect.y += insets.top;
099: rect.width -= insets.left + insets.right;
100: rect.height -= insets.top + insets.bottom;
101: panel.setBounds(rect);
102: }
103:
104: }
|