Add a JLabel in the JTabbedPane header

纵饮孤独 提交于 2019-12-24 00:42:11

问题


I have this JDialog with a JTabbedPane:

I just want to add a JLabel in the right-top area of the JTabbedPane so i can fire some events (e.g close the JDialog). And i don't want to make it a JFrame.

PS: I know that is may be duplicated, but none of the responses give me a solution.


回答1:


Well, actually that positioning inside JTabbedPane is not supported in Swing. But you can always make some street magic with layouts and layers:

public static void main ( String[] args )
{
    try
    {
        UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName () );
    }
    catch ( Throwable e )
    {
        e.printStackTrace ();
    }

    JFrame frame = new JFrame ();
    frame.setUndecorated ( true );

    frame.setLayout ( new LayoutManager ()
    {
        private List<Component> special = new ArrayList<Component> ();

        public void addLayoutComponent ( String name, Component comp )
        {
            if ( name != null )
            {
                special.add ( comp );
            }
        }

        public void removeLayoutComponent ( Component comp )
        {
            special.remove ( comp );
        }

        public Dimension preferredLayoutSize ( Container parent )
        {
            Dimension ps = new Dimension ();
            for ( Component component : parent.getComponents () )
            {
                if ( !special.contains ( component ) )
                {
                    Dimension cps = component.getPreferredSize ();
                    ps.width = Math.max ( ps.width, cps.width );
                    ps.height = Math.max ( ps.height, cps.height );
                }
            }
            return ps;
        }

        public Dimension minimumLayoutSize ( Container parent )
        {
            return preferredLayoutSize ( parent );
        }

        public void layoutContainer ( Container parent )
        {
            Insets insets = parent.getInsets ();
            for ( Component component : parent.getComponents () )
            {
                if ( !special.contains ( component ) )
                {
                    component.setBounds ( insets.left, insets.top,
                            parent.getWidth () - insets.left - insets.right,
                            parent.getHeight () - insets.top - insets.bottom );
                }
                else
                {
                    Dimension ps = component.getPreferredSize ();
                    component.setBounds ( parent.getWidth () - insets.right - 2 - ps.width,
                            insets.top + 2, ps.width, ps.height );
                }
            }
        }
    } );

    final JTabbedPane tabbedPane = new JTabbedPane ();
    tabbedPane.addTab ( "Tab1", new JLabel () );
    tabbedPane.addTab ( "Tab2", new JLabel () );
    tabbedPane.addTab ( "Tab3", new JLabel () );
    frame.add ( tabbedPane );

    final JLabel label = new JLabel ( "Close X" );
    label.addMouseListener ( new MouseAdapter ()
    {
        public void mousePressed ( MouseEvent e )
        {
            System.exit ( 0 );
        }
    } );
    frame.add ( label, "special", 0 );

    frame.setSize ( 200, 150 );
    frame.setLocationRelativeTo ( null );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.setVisible ( true );
}

This will work anywhere without any problems. Just be aware that label will be always in top right corner and will not position itself (unless you modify the code to support more "features") on other OS LaF's like Mac OS laf where tabs might be in the middle. Also it will be painted over tabs if they will cross with label.

So you will get something like this:

Anyway, you can easily modify applied to JLabel bounds inside the layout to make some side-spacing and such...




回答2:


This can be done by writing your own TabbedPaneUI by extending BasicTabbedPaneUI or MetalTabbedPaneUI. But this would be difficult to write.

I belive that you want to implement something like close button (below will work with JLabel as well)

  • I would try to implement this button on GlassPane and Layers

or

  • (if your JDialog is not resizable) you can setLayout(null) and provide hardcoded values for position and size of the components and add JButton over JTabbedPane

or

  • you can override paintComponents on your content pane and draw your own button (or provided by "fake" JButton). Next - you have to setMouseListener on JTabbedPane and check if mouse click position is in button's bounds. Of course - you will need to implement button visual state (pressed, hover, etc.) or just pass event to faked JButton instance.


来源:https://stackoverflow.com/questions/10620630/add-a-jlabel-in-the-jtabbedpane-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!