質問

I'm trying to apply the flow layout to a JTabbedPane so that I can set the hgap(0) and vgap(0). The default layout for the JTabbedPane leaves gaps all around.

The following code throws when I try to set the layout:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;


public class Main {

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override public void run() {
                    JFrame F = new JFrame("Testing");
                    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JTabbedPane Pane = new JTabbedPane();
                    Pane.setLayout(new FlowLayout(0, 0, FlowLayout.CENTER));
                    F.setLayout(new BorderLayout());
                    F.add(Pane, BorderLayout.CENTER);

                    Pane.addTab("Hello", new JPanel());
                                    F.pack();
                    F.setVisible(true);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

What I see without the flow layout: enter image description here

I want to remove the turquoise looking border thing around the Black JPanel. I don't want to remove the tab. I just want to remove the space between the tab and the component. I want to remove the space around the component that looks blue-ish in the above picture.

Why does it throw an exception that says ArrayIndexOutOfBounds:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at javax.swing.plaf.basic.BasicTabbedPaneUI.paintTabArea(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
役に立ちましたか?

解決

I don't think that you can mess with the layout of a JTabbedPane. It just doesn't make sense to even consider this. It likely has its own specialty layout.


Edit 1
And in fact it certainly does: TabbedPaneLayout

Solution: don't mess with the layout of a JTabbedPane, and certainly don't swap it for another layout that won't function with a JTabbedPane.


Edit 2
you state:

How can I remove the gaps around it then? I leaves these gaps all around the component :S I tried other layouts as well.. yeah they throw too.

It would likely be best to change the layouts of the components being held by the JTabbedPane to allow them to re-size and fill in gaps, but so that we can fully understand what problems you may be having, please show images of just what you're seeing and what you're trying to show.


Edit 3
You state:

want to remove the turquoise looking border thing around the Black JPanel. I don't want to remove the tab. I just want to remove the space between the tab and the component. I want to remove the space around the component that looks blue-ish in the above picture.

That is probably a Look & Feel issue and not a layout issue. As for the direct answer to your original question, the answer is: don't mess with the JTabbedPane layout, period. As for your problem you're trying to solve, consider using a different L&F, or if that doesn't work, writing your own ui delegate for the JTabbedPane. I've never done this sort of thing myself though.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top