How to get tab height?

  • Follow


Hello,

I am having an issue where I need to determine the height of the tab
produced by the JTabbedPane.  I must be looking at the wrong JavaDocs
as I can't locate information on how to dynamically retrieve the
height of the tab.

Has anyone done this?  If so, how did you accomplish the retrieval of
the tab size at runtime?

Thanks in advance for the help,
Todd
0
Reply todd.heidenthal (70) 8/21/2008 3:38:33 PM

Todd wrote:
> Hello,
> 
> I am having an issue where I need to determine the height of the tab
> produced by the JTabbedPane.  I must be looking at the wrong JavaDocs
> as I can't locate information on how to dynamically retrieve the
> height of the tab.
> 
> Has anyone done this?  If so, how did you accomplish the retrieval of
> the tab size at runtime?
> 
> Thanks in advance for the help,
> Todd

If you are using 1.6 you can get the component that displays the tab and 
take its height.  There is some padding and insets which you could get 
from the UIDefaults.

I'm curious though why you need to know?

-- 

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
0
Reply nospam8071 (917) 8/21/2008 4:12:13 PM


On 21-8-2008 17:38, Todd wrote:
> Hello,
> 
> I am having an issue where I need to determine the height of the tab
> produced by the JTabbedPane.  I must be looking at the wrong JavaDocs
> as I can't locate information on how to dynamically retrieve the
> height of the tab.
> 
> Has anyone done this?  If so, how did you accomplish the retrieval of
> the tab size at runtime?
> 
> Thanks in advance for the help,
> Todd

The UI component of the JTabbedPane seems to know the bounds of each of 
its tabs.


<untested>

JTabbedPane tabPane = ...
TabbedPaneUI ui = tabPane.getUI();
Rectangle tabBounds = ui.getTabBounds(tabPane, indexOfRequestedTab);
int heightOfRequestedTab = tabBounds.height;

</untested>

-- 
Regards,

Roland
0
Reply roland.de.ruiter (139) 8/21/2008 4:52:56 PM

>
> I'm curious though why you need to know?
>

I am likely doing something wrong, so if you have a better idea
following my reasoning, I would love to hear it.

I have a JTabbedPane which holds other JTabbedPanes.  Each
tab of the embedded pane contains one or more panels.  I have
been calculating the height of the embedded pane based upon
the panels it contains, then setting size, preferredSize, and
minimumSize of the embedded pane.

Once that is done, the outer pane's size is then based upon
the largest embedded pane.  However, when I request the height
of the embedded pane, it returns the panel height, not including
the tab height.  Hence, my need for the tab height ~ to add it
to the panel height to get the total embedded pane height.  That
total height is then used to set the size, preferredSize, and
minimumSize of the outer pane.

I will play with the UI info to see how that goes.

Thanks,
Todd
0
Reply todd.heidenthal (70) 8/21/2008 5:01:22 PM

> int heightOfRequestedTab = tabBounds.height;

Roland,

I don't know if I am using the code you suggested improperly, so
I have a question.

When I request tabBounds.height, that provides the height of the
panel within the tabbedpane.  Is it supposed to provide me with
the height of the tab itself or of the panel?

Thanks,
Todd
0
Reply todd.heidenthal (70) 8/21/2008 5:38:59 PM

On 21-8-2008 19:38, Todd wrote:
>> int heightOfRequestedTab = tabBounds.height;
> 
> Roland,
> 
> I don't know if I am using the code you suggested improperly, so
> I have a question.
> 
> When I request tabBounds.height, that provides the height of the
> panel within the tabbedpane.  Is it supposed to provide me with
> the height of the tab itself or of the panel?
> 
> Thanks,
> Todd

It should indicate the height of the tab itself, not of the panel on the 
tab.

<ssccexample>

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.text.*;

public class TabbedPaneTest extends JFrame {

    private static final long serialVersionUID = 2594638227854923552L;

    private static final int NUM_OF_TABS = 7;

    /**
     * Main method which starts the TabbedPaneTest application.
     *
     * @param args command line arguments
     */
    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
             TabbedPaneTest application = new TabbedPaneTest();
             application
                   .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             application.setSize(480, 320);
             application.setVisible(true);
          }
       });
    }

    private JButton jBtnClearLog;

    private JButton jBtnShowBounds;

    private JPanel jButtonPanel;

    private JCheckBox jChkIconsOnTabs;

    private JPanel jContentPane;

    private JTabbedPane jTabbedPane;

    private Document logDocument;

    /**
     * This is the default constructor
     */
    public TabbedPaneTest() {
       initialize();
       postInitialize();
    }

    /**
     * Returns an icon for the tab with the given
     * <code>tabIndex</code>.
     *
     * @param tabIndex
     * @return an icon for the tab with the given
     *         <code>tabIndex</code>
     */
    private Icon getIconForTab(int tabIndex) {
       switch (tabIndex % 5) {
       case 0:
          return UIManager.getIcon("FileView.computerIcon");
       case 1:
          return UIManager.getIcon("FileView.directoryIcon");
       case 2:
          return UIManager.getIcon("FileView.fileIcon");
       case 3:
          return UIManager.getIcon("FileView.hardDriveIcon");
       case 4:
       default:
          return UIManager.getIcon("FileView.floppyDriveIcon");
       }
    }

    /**
     * This method initializes jBtnClearLog
     *
     * @return javax.swing.JButton
     */
    private JButton getJBtnClearLog() {
       if (jBtnClearLog == null) {
          jBtnClearLog = new JButton();
          jBtnClearLog.setText("Clear log");
          jBtnClearLog.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                Document logDoc = getLogDocument();
                try {
                   logDoc.remove(0, logDoc.getLength());
                } catch (BadLocationException ex) {
                   ex.printStackTrace();
                }
             }
          });
       }
       return jBtnClearLog;
    }

    /**
     * This method initializes jPrintBoundsButton
     *
     * @return javax.swing.JButton
     */
    private JButton getJBtnShowBounds() {
       if (jBtnShowBounds == null) {
          jBtnShowBounds = new JButton();
          jBtnShowBounds.setText("Print tab bounds");
          jBtnShowBounds.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                JTabbedPane tp = getJTabbedPane();
                int tabCount = tp.getTabCount();
                TabbedPaneUI ui = tp.getUI();

                println(new Date());
                for (int i = 0; i < tabCount; i++) {
                   Rectangle tabBounds = ui.getTabBounds(tp, i);
                   println("Bounds of tab " + (i + 1) + ": "
                         + tabBounds);
                }
                println("");
             }

             private void println(Object s) {
                System.out.println(s);
                Document logDoc = getLogDocument();
                try {
                   logDoc.insertString(logDoc.getLength(), s
                         + "\n", null);
                } catch (BadLocationException ex) {
                   ex.printStackTrace();
                }
             }
          });
       }
       return jBtnShowBounds;
    }

    /**
     * This method initializes jButtonPanel
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJButtonPanel() {
       if (jButtonPanel == null) {
          jButtonPanel = new JPanel();
          jButtonPanel.setLayout(new FlowLayout());
          jButtonPanel.add(getJBtnShowBounds(), null);
          jButtonPanel.add(getJBtnClearLog(), null);
          jButtonPanel.add(getJChkIconsOnTabs(), null);
       }
       return jButtonPanel;
    }

    /**
     * This method initializes jIconsOnTabs
     *
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getJChkIconsOnTabs() {
       if (jChkIconsOnTabs == null) {
          jChkIconsOnTabs = new JCheckBox();
          jChkIconsOnTabs.setText("Icons on tabs");
          jChkIconsOnTabs.setSelected(true);
          jChkIconsOnTabs.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                updateTabIcons(getJTabbedPane(), jChkIconsOnTabs
                      .isSelected());
             }
          });
       }
       return jChkIconsOnTabs;
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
       if (jContentPane == null) {
          jContentPane = new JPanel();
          jContentPane.setLayout(new BorderLayout());
          jContentPane.setSize(new Dimension(286, 188));
          jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
          jContentPane.add(getJButtonPanel(), BorderLayout.NORTH);
       }
       return jContentPane;
    }

    /**
     * This method initializes jTabbedPane
     *
     * @return javax.swing.JTabbedPane
     */
    private JTabbedPane getJTabbedPane() {
       if (jTabbedPane == null) {
          jTabbedPane = new JTabbedPane();

          // this is the document used for the text area on each
          // tab
          Document sharedDocument = getLogDocument();

          // create and add NUM_OF_TABS tabs
          for (int i = 0; i < NUM_OF_TABS; i++) {
             final int tabNumber = i + 1;

             JLabel tabLabel = new JLabel();
             tabLabel.setText("This is Tab #" + tabNumber);
             tabLabel.setHorizontalAlignment(JLabel.CENTER);

             JTextArea textArea = new JTextArea();
             textArea.setDocument(sharedDocument);

             JScrollPane textAreaScrollPane = new JScrollPane();
             textAreaScrollPane.setViewportView(textArea);

             JPanel tabPanel = new JPanel();
             tabPanel.setLayout(new BorderLayout());
             tabPanel.add(tabLabel, BorderLayout.NORTH);
             tabPanel.add(textAreaScrollPane, BorderLayout.CENTER);

             jTabbedPane.addTab("Tab " + tabNumber, null,
                   tabPanel, null);
          }
       }
       return jTabbedPane;
    }

    private Document getLogDocument() {
       if (logDocument == null) {
          logDocument = new PlainDocument();
       }
       return logDocument;
    }

    /**
     * This method initializes the user interface.
     */
    private void initialize() {
       this.setSize(400, 300);
       this.setContentPane(getJContentPane());
       this.setTitle("Tabbed Pane Test");
    }

    /**
     * This method takes care of correctly showing state of UI
     * after it has been initialized.
     */
    private void postInitialize() {
       // make sure the tab icons match the state of the checkbox
       updateTabIcons(getJTabbedPane(), getJChkIconsOnTabs()
             .isSelected());
    }

    /**
     * Updates the icons of each tab of the given
     * <code>tabbedPane</code>. If <code>showIcons</code> is
     * <code>false</code>, the icon of each tab of
     * <code>tabbedPane</code> is removed. If
     * <code>showIcons</code> is <code>true</code>, an icon
     * is added on each tab.
     *
     * @param tabbedPane
     * @param showIcons
     */
    private void updateTabIcons(JTabbedPane tabbedPane,
          boolean showIcons) {
       int tabCount = tabbedPane.getTabCount();
       for (int i = 0; i < tabCount; i++) {
          if (showIcons) {
             // set the icon
             tabbedPane.setIconAt(i, getIconForTab(i));
          } else {
             // remove icon
             tabbedPane.setIconAt(i, null);
          }
       }
    }
}



-- 
Regards,

Roland
0
Reply roland.de.ruiter (139) 8/21/2008 8:17:04 PM

Todd wrote:
>> I'm curious though why you need to know?
>>
> 
> I am likely doing something wrong, so if you have a better idea
> following my reasoning, I would love to hear it.
> 
> I have a JTabbedPane which holds other JTabbedPanes.  Each
> tab of the embedded pane contains one or more panels.  I have
> been calculating the height of the embedded pane based upon
> the panels it contains, then setting size, preferredSize, and
> minimumSize of the embedded pane.
> 
> Once that is done, the outer pane's size is then based upon
> the largest embedded pane.  However, when I request the height
> of the embedded pane, it returns the panel height, not including
> the tab height.  Hence, my need for the tab height ~ to add it
> to the panel height to get the total embedded pane height.  That
> total height is then used to set the size, preferredSize, and
> minimumSize of the outer pane.
> 
> I will play with the UI info to see how that goes.
> 
> Thanks,
> Todd

You can use a layoutmanager to layout your panels.  The tabbed pane will 
size itself to the largest one.  In the example below the panels don't 
have any components so I size them specifically.  The tabbed pane sizes 
itself to fit the largest.  The layout manager used by JTabbedPane sizes 
the panel to fit the room available, that's why they are all the same 
size when you look at them.  If the panels are bigger than what you want 
your frame to be you can always put them in a JScrollPane.

So I wouldn't worry about the size of the tabs.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 JTabbedPane outer = new JTabbedPane();

                 JTabbedPane inner = new JTabbedPane();
                 System.out.println(inner.getLayout());

                 JPanel red = new JPanel();
                 red.setBackground(Color.RED);
                 red.setPreferredSize(new Dimension(400,300));

                 JPanel green = new JPanel();
                 green.setBackground(Color.GREEN);
                 green.setPreferredSize(new Dimension(200,150));

                 JPanel yellow = new JPanel();
                 yellow.setBackground(Color.YELLOW);
                 yellow.setPreferredSize(new Dimension(100,75));

                 inner.addTab("Red",red);
                 inner.addTab("Green",green);
                 inner.addTab("Yellow",yellow);

                 outer.addTab("Outer",inner);

                 f.add(outer,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}

-- 

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
0
Reply nospam8071 (917) 8/21/2008 11:11:51 PM

6 Replies
32 Views

(page loaded in 0.075 seconds)


Reply: