Skip to main content

How can I place JtabbedPane tabs at an arbitrary x location at top

4 replies [Last post]
blombar
Offline
Joined: 2010-09-09

I have a requirement to use tabs in an application, but the design requires that the tabs start about a quarter of the way in from the left top edge because the left edge is taken up by a logo. Is there any way to do this? I know how to place them at the top or bottom, etc. but not at an offset.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aephyr
Offline
Joined: 2009-11-20

You can't because doing so isn't/wasn't a "Cricket release showstopper": http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4199386

Also in the link is a really bad workaround and I am not just talking about the useless extending of JPanel; it also looks really bad with some L&F's.

kleopatra
Offline
Joined: 2003-06-11

what a cool reasoning to close the bug

Below is another trick: use a custom tab component in the first tab and make it unselectable by a custom model. This will look bad when there are too many tabs so that either scrolling or wrapping is required. Might give you some leeway, though.

Cheers
Jeanette

[code]
// dont dont dont dont extend JFrame
public class FakeTabComponent extends JFrame {

private final int tabNumber = 5;
private final JTabbedPane pane = new JTabbedPane();

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
// InteractiveTestCase.setLAF("Windows");
new FakeTabComponent("TabComponentsDemo").runTest();
}
});
}

public FakeTabComponent(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pane);
}

public void runTest() {
pane.setModel(new SelectionModel());
for (int i = 0; i < tabNumber; i++) {
String title = "Tab " + i;
pane.add(title, new JLabel(title));
if (i == 0) {
JXBusyLabel label = new JXBusyLabel();
label.setText(title);
label.setBusy(true);
pane.setTabComponentAt(i, label);
}
}
pane.setSelectedIndex(1);
setSize(new Dimension(400, 200));
setLocationRelativeTo(null);
setVisible(true);
}

class SelectionModel extends DefaultSingleSelectionModel {

/**
* @inherited

*/
@Override
public void setSelectedIndex(int index) {
super.setSelectedIndex(adjustIndex(index));
}

private int adjustIndex(int index) {
if (index < 0) return index;
if (pane.getTabCount() == 1) {
return -1;
}
if (index == 0)
index++;
if (index >= pane.getTabCount()) {
index = 1;
}
return index;
}

}
}

[/code]

blombar
Offline
Joined: 2010-09-09

Thanks. This is an interesting work-around. I've tried it and it seem like I could get it to do what I need assuming I can get the first tab to be the right size. I had to download the SwingX library. Does it require SwingX? I'm an old hand at Java, but I haven't done anything with Swing since 2000. Please explain your comment about not extending JFrame.

kleopatra
Offline
Joined: 2003-06-11

>
> size. I had to download the SwingX library. Does it
> require SwingX? I'm an old hand at Java, but I

no the trick as such has nothing to do with SwingX - not surprisingly, most of my example code contains some reference to it

> Please
> explain your comment about not extending JFrame.

Google is your friend

CU
Jeanette