You might have seen that tabs in JTabbedPane don't scroll. If more tabs are added into the JTabbedPane, then tabs are added to the bottom if the tabs aren't fit in a single row.
Multiple rows are messy, right? So, wouldn't a scroll layout be better just like in the CCleaner? Fine, when you ask me Do i need to hack? I say, NO. There is a method, yes, here it is.
This method takes either of these two values, SCROLL_TAB_LAYOUT and WRAP_TAB_LAYOUT which have 1 and 0 values respectively. The first one is what does the thing. Let us see it in action.
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)
Multiple rows are messy, right? So, wouldn't a scroll layout be better just like in the CCleaner? Fine, when you ask me Do i need to hack? I say, NO. There is a method, yes, here it is.
public void setTabLayoutPolicy(int policy);
This method takes either of these two values, SCROLL_TAB_LAYOUT and WRAP_TAB_LAYOUT which have 1 and 0 values respectively. The first one is what does the thing. Let us see it in action.
import javax.swing.*;
import java.awt.*;
class JTabbedPaneScroll extends JFrame
{
JTabbedPane t;
public JTabbedPaneScroll()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("JTabbedPane Scroll");
setSize(400,400);
t=new JTabbedPane();
for(int i=1;i<=10;i++){
JPanel p=new JPanel();
p.setBackground(new Color(i,i*8,i*4));
t.addTab("Tab "+i,p);
}
add(t);
// Now make it scrollable
// or you can call
t.setTabLayoutPolicy(1);
// t.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new JTabbedPaneScroll();
}
}
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)
No comments:
Post a Comment