Change the Tab Size of JTabbedPane
The JTabbedPane class has not provided an API to set the tab size, but sometimes
we do need this, we can find some ways to work arround.
First of all I create a JTabbedPane component with four tabs.
<font color="#0000FF"> final JTabbedPane tabbedPane = new JTabbedPane();<br> tabbedPane.addTab("Tab1", new JLabel("Text Component 1"));<br> tabbedPane.addTab("Tab2", new JLabel("Text Component 2"));<br> tabbedPane.addTab("Tab3", new JLabel("Text Component 3"));<br> tabbedPane.addTab("Tab4", new JLabel("Text Component 4"));</font>
Add this component to a JFrame and show it:
I think the tabs are too small, I can use a one pixel width icon (1X32 pixels)
to force the tab become bigger:
<font color="#0000FF"> tabbedPane.setIconAt(0, new ImageIcon("1x32.png"));</font>
Now the tabs changed their heights:
But we can not change the tab width by using icon. Fortunately the tab text
support HTML tags, we can use HTML to achieve our mission, here is how:
<font color="#0000FF"> tabbedPane.addTab("<html><body leftmargin=15 topmargin=8 marginwidth=15 marginheight=5>Tab1</body></html>", new JLabel("Text Component 1"));<br> tabbedPane.addTab("<html><body leftmargin=15 topmargin=8 marginwidth=15 marginheight=5>Tab2</body></html>", new JLabel("Text Component 2"));<br> tabbedPane.addTab("<html><body leftmargin=15 topmargin=8 marginwidth=15 marginheight=5>Tab3</body></html>", new JLabel("Text Component 3"));<br> tabbedPane.addTab("<html><body leftmargin=15 topmargin=8 marginwidth=15 marginheight=5>Tab4</body></html>", new JLabel("Text Component 4"));</font>
Now we have four bigger tabs, it looks good:
- Login or register to post comments
- Printer-friendly version
- xuanyun's blog
- 6436 reads





