I use this function to create tabs. When I want to add button "?" that will open the Wizard, this button appears at the right side of this JTable. How to edit this?
comp1 - "1. Dp/Drho", comp2 - "2. Df2/Drho", comp3 - "3. SDSS", comp4 - "4. Help". name1-4 - names of this tabs.
static protected JPanel allocateUniPane(Component comp1,Component comp2,Component comp3, Component comp4,
String name1, String name2, String name3, String name4){
JPanel resultPane = new JPanel();
GridBagLayout gridbagforUnionPane = new GridBagLayout();
GridBagConstraints cUnionPane = new GridBagConstraints();
resultPane.setLayout(gridbagforUnionPane);
cUnionPane.fill = GridBagConstraints.BOTH;
cUnionPane.anchor = GridBagConstraints.CENTER;
JButton showWizard = new JButton("?");
cUnionPane.weightx = 0.5;
cUnionPane.weighty = 0.5;
JTabbedPane jtp = new JTabbedPane();
jtp.addTab(name1, comp1);
jtp.addTab(name2, comp2);
jtp.addTab(name3, comp3);
jtp.addTab(name4, comp4);
cUnionPane.gridx = 0;
cUnionPane.gridy = 0;
resultPane.add(jtp,cUnionPane);
cUnionPane.fill = GridBagConstraints.NORTH;
cUnionPane.weightx = 0.5;
cUnionPane.gridx = 1;
cUnionPane.gridy = 0;
resultPane.add(showWizard,cUnionPane);
return resultPane;
}
You want the button to appear to be part of the tabbed pane. You can't use a GridBagLayout (or most other layouts) because they layout positions components in two dimensions on the panel.
Maybe the OverlayLayout will do what you want since it positions components in the third dimension. For example:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class TabbedPaneWithComponent
{
private static void createAndShowUI()
{
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("1", new JTextField("one"));
tabbedPane.add("2", new JTextField("two"));
tabbedPane.setAlignmentX(1.0f);
tabbedPane.setAlignmentY(0.0f);
JCheckBox checkBox = new JCheckBox("Check Me");
checkBox.setOpaque( false );
checkBox.setAlignmentX(1.0f);
checkBox.setAlignmentY(0.0f);
panel.add( checkBox );
panel.add(tabbedPane);
JFrame frame = new JFrame("TabbedPane With Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.setSize(400, 100);
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}