0

I have a requirement to put the minimize and close button at the right corner where my JTabbedPane ends. For clarity, the encircled area are the two button which I require (I have an undecorated frame)

The encircled area showing the two buttons

Can anyone please guide me on how to achieve this? My question is (more) on positioning of the buttons (on the JTabbedPane) rather than achieving the functionality of minimize/close

Thank you!

user1639485
  • 808
  • 3
  • 14
  • 26
  • are you using a IDE? what one? – elyashiv Sep 19 '12 at 20:32
  • Do you have any additional context about this requirement? Is this a stand-alone document or is it part of a larger html page? One possible solution is to use CSS "position: absolute" and use right/top and margin/padding to position it as you would like. If this idea won't work, can you provide additional details about the issue? – jennyfofenny Sep 19 '12 at 20:34
  • I am using Eclipse for all my coding. I don't have an IDE for my GUI designing. If I am not clear, this is what I require: I have a undecorated JFrame onto which I have added a JTabbedPane (the JTabbedPane in turns has 4 tabs on it). I want 2 buttons at the end of my JTabbedPane as shown above in the picture. The encircled portion of the image does not exist for me as of now (its just a representation of my requirement added through an image editor) – user1639485 Sep 19 '12 at 20:40

2 Answers2

1

All components added to a JTabbedPane as children will be wrapped in tabs. This means it is not possible to add the buttons directly to the JTabbedPane. If you want to have the close and minimize buttons as part of the tabbed pane directly (rather than of to the side a little), I would recommend adding two tabs to the tabbed pane with custom tab components (JTabbedPane.setTabComponentAt()) that are buttons. As the button is the only component of the tab, selecting the actual "tab" itself is not possible, and only the buttons events will occur. With a JTabbedPane it is not possible to fix the location of a tab, which will cause these two "button tabs" to float within the layout you have selected. Inserting further tabs to the left of them will have the effect of always ending the tab list with the close and minimize buttons.

Zoe
  • 1,833
  • 1
  • 16
  • 18
0

I got a solution to this problem: Just put a toolbar beside the tabbed pane

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.JTabbedPane;

import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.JToolBar;

import java.awt.ComponentOrientation;

import javax.swing.JButton;

import java.awt.Cursor;

public class PanelTab extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (UnsupportedLookAndFeelException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PanelTab frame = new PanelTab();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public PanelTab() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[] { 424, 0 };
    gbl_contentPane.rowHeights = new int[] { 51, 0 };
    gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
    gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
    contentPane.setLayout(gbl_contentPane);

    JPanel panel = new JPanel();
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.fill = GridBagConstraints.BOTH;
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    contentPane.add(panel, gbc_panel);

    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[] { 184, 0 };
    gbl_panel.rowHeights = new int[] { 54, 0 };
    gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
    gbl_panel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
    panel.setLayout(gbl_panel);

    JToolBar toolBar = new JToolBar();
    toolBar.setOpaque(false);
    toolBar.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    toolBar.setFloatable(false); // The toolbar must not be float

    GridBagConstraints gbc_toolBar = new GridBagConstraints();
    gbc_toolBar.anchor = GridBagConstraints.NORTHEAST;
    gbc_toolBar.gridx = 0;
    gbc_toolBar.gridy = 0;
    panel.add(toolBar, gbc_toolBar);

    JButton btnHola = new JButton("You must put your exit icon here");
    btnHola.setOpaque(false);
    toolBar.add(btnHola);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
    gbc_tabbedPane.fill = GridBagConstraints.BOTH;
    gbc_tabbedPane.gridx = 0;
    gbc_tabbedPane.gridy = 0;
    panel.add(tabbedPane, gbc_tabbedPane);

    JPanel panel_1 = new JPanel();
    tabbedPane.addTab("New tab", null, panel_1, null);
}
}

I guess this might help you...