简体   繁体   中英

How to add button in JTabbedPane background?

I want to add button in my JTabbedPane background like Google Chrome so that every time I can add new tabs by clicking it.

在此处输入图片说明

How can I do it? Thanks in advance!

EDIT : I have taken an undecorated JFrame.

Look into the JTabbedPane.setTabComponentAt( int index, Component component ) method. This method allows you to set the component with which to render the title.

Description from documentation:

Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.

Note: The component must not be one that the developer has already added to the tabbed pane.


What you can do:

  • Create the JTabbedPane
  • Add a new tab to it, its intended function like the chrome "add tab page"
  • Set the title component of that tab to a button (style it appropriately)
  • When that button is clicked, add a new tab right before the button tab and show the newly added tab

This code will create only one tab and button to it.

class Test extends JFrame
{
 JTabbedPane jtab;
 JButton but;
 JPanel panel;

  Test()
 {
     super("JTabbedPane");
     jtab=new JTabbedPane();
     but=new Button("Click");
     panel=new JPanel();
     panel.add(but);

     jtab.add("Tab",panel);

     add(jtab);

     setVisible(true);
     setSize(400,400);
  }

 public static void main(String[] args)
 {
    new Test();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM