I have a a JTabbedPane myTab within my JFrame. Its first tab has a title of "old title". I want to change the title dynamically, so I use this code to set:
myTab.setTitleAt(myTab.getSelectedIndex(), "my full new title");
And somehow my new title is longer than my old one. The problem is, the tab size does not change, and it does not display the new title fully, only "my full n...".
And if I click on the tab, suddenly the tab can show full new title.
I already tried this code too, to set the title name:
myTab.setTabComponentAt(myTab.getSelectedIndex(), new JLabel("my full new title"));
This code can help me change the tab size accordingly to the new title. But the cross (x) to close tab is not there anymore.
Does anyone know how to change the tab size when changing tab title, but still keep the close tab option?
Thank you and much appreciate!
I never saw that,
but this is possible only in one cases, code that you run is out of EDT,
Swing is single threaded and all changes to the Swing GUI must be done on Event Dispatch Thread, more about this topic in Concurency in Swing, (I'd suggest to search for this common topic on this Forum),
from code,
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;
/*based on @trashgod code original code
@see http://stackoverflow.com/questions/5617027 */
public class Cab extends JPanel {
private static final Random random = new Random();
private static final String format = "00000000";
private static final DecimalFormat df = new DecimalFormat(format);
private static JTabbedPane tabbedPane = new JTabbedPane();
private static final long serialVersionUID = 1L;
private Hue hue = Hue.Yellow;
private Timer timer;
private JLabel odometer = new JLabel(df.format(0));
private int km;
public Cab() {
this.setPreferredSize(new Dimension(320, 240));
this.setBackground(hue.getColor());
this.add(odometer);
final JComboBox colorBox = new JComboBox();
for (Hue h : Hue.values()) {
colorBox.addItem(h);
}
colorBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Hue h = (Hue) colorBox.getSelectedItem();
Cab.this.setBackground(h.getColor());
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
}
});
this.add(colorBox);
timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
km += random.nextInt(100);
odometer.setText(df.format(km));
}
});
timer.start();
}
private enum Hue {
Yellow(Color.yellow), Cyan(Color.cyan), Magenta(Color.magenta);
private final Color color;
private Hue(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
}
private static void display() {
JFrame f = new JFrame("Dispatch");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane.add("Cab #1", new Cab());
tabbedPane.add("Cab #2", new Cab());
tabbedPane.add("Cab #3", new Cab());
f.add(tabbedPane);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}