Reputation: 1329
Hello I need to make a tab panel like google chrome. You press another tab , and chrome put the tab. I have this.
but when I press the tab with symbol "+" , this is the effect :
I thought exchange the position with symbol "+" for the position on the new tab, but I can't do it. I have problems. Really I want to do like navigator chrome , that effect. Thanks. Here I have the code:
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VentanaPrincipalFrame ventana = new VentanaPrincipalFrame();
ventana.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Principal Frame:
public class VentanaPrincipalFrame extends JFrame {
private JTabbedPane tabbedPane;
private JPanel panelAdd;
public VentanaPrincipalFrame() {
initFrame();
panelAdd = new JPanel();
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addChangeListener(new ChangeListener() {
int i = 1;
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane tab = (JTabbedPane) e.getSource();
if( tab.getSelectedIndex() == tabbedPane.getTabCount()-1 && tabbedPane.getTabCount() > 1){
i++;
addPestana("Tab"+i);
//mensaje("Hola soy el ultimo");
}
}
});
getContentPane().add(tabbedPane, BorderLayout.CENTER);
tabbedPane.addTab("New tab", new PanelDinamicoPanel());
tabbedPane.addTab( "+", panelAdd );
}
public void addPestana(String titulo){
tabbedPane.addTab( titulo , new PanelDinamicoPanel() );
}
private void initFrame() {
setTitle("Principal");
setSize(400,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
}
}
My custom tab:
public class PanelDinamicoPanel extends JPanel{
private JTextField textField;
public PanelDinamicoPanel() {
setLayout(new BorderLayout(0, 0));
JPanel botonesPanel = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) botonesPanel.getLayout();
flowLayout_1.setAlignment(FlowLayout.RIGHT);
add(botonesPanel, BorderLayout.SOUTH);
JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
botonesPanel.add(panel);
JButton btnNewButton = new JButton("New button");
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);
JPanel panel_1 = new JPanel();
add(panel_1, BorderLayout.CENTER);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel lblNombre = new JLabel("Name");
GridBagConstraints gbc_lblNombre = new GridBagConstraints();
gbc_lblNombre.insets = new Insets(0, 0, 0, 5);
gbc_lblNombre.anchor = GridBagConstraints.EAST;
gbc_lblNombre.gridx = 0;
gbc_lblNombre.gridy = 0;
panel_1.add(lblNombre, gbc_lblNombre);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel_1.add(textField, gbc_textField);
textField.setColumns(10);
}
}
Thanks to everybody.
Upvotes: 1
Views: 1427
Reputation: 347334
Basically, instead of "adding" a tab, you need to "insert" one, something like...
Component tabComp = ...
tab.insertTab("Title", null, tabComp, null, index);
For example, where index
is the location where you want the new tab to be added. If you just want to maintain the "+" at the end, the something like tab.getTabCount() - 1
should do the trick
If you want to add the tab next to the last active tab, then you need to get a little more adventurous...
The problem is, when the stateChanged
event is triggered, the selection has already changed, so doing tab.getSelectedIndex()
will always return the "+" tab.
What you need to know is the tab that was selected BEFORE it.
What you could do is inspect the selected tab when the state changed, if it's not the "+" tab, you will want to record the selected index value.
When the "+" tab becomes active, you would simply use the "last selected index" value your have being tracking as a base for inserting the new tab...
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestTabSelection {
public static void main(String[] args) {
new TestTabSelection();
}
public TestTabSelection() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JTabbedPane tab = new JTabbedPane();
tab.addTab("A", new JPanel());
tab.addTab("+", new JPanel());
tab.getModel().addChangeListener(new ChangeListener() {
private int lastSelected;
private boolean ignore = false;
@Override
public void stateChanged(ChangeEvent e) {
if (!ignore) {
ignore = true;
try {
int selected = tab.getSelectedIndex();
String title = tab.getTitleAt(selected);
if ("+".equals(title)) {
JPanel pane = new JPanel();
tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1);
tab.setSelectedComponent(pane);
} else {
lastSelected = selected;
}
} finally {
ignore = false;
}
}
}
});
final JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(tab.getTabCount());
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tab);
frame.add(btn, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Upvotes: 4
Reputation: 1092
I have a solution for you :
tab.setTabComponentAt(tab.getTabCount() - 1, new PanelDinamicoPanel());
tab.addTab("+", null);
Hope that helps, Salam
Upvotes: 0