JTabbedPane:更改选项卡标题时更改选项卡大小

时间:2012-02-20 07:46:07

标签: java swing tabs title jtabbedpane

我的JFrame中有一个JTabbedPane myTab。它的第一个标签标题为“旧标题”。我想动态更改标题,所以我使用此代码设置:

myTab.setTitleAt(myTab.getSelectedIndex(), "my full new title");

不知何故,我的新头衔比旧版头更长。问题是,标签大小不会改变,并且它不会完全显示新标题,只有“我的完整...”。

如果我点击标签,突然标签会显示全新的标题。

我也尝试过这段代码,设置标题名称:

myTab.setTabComponentAt(myTab.getSelectedIndex(), new JLabel("my full new title"));

此代码可以帮助我根据新标题更改标签大小。但是交叉(x)关闭标签不再存在。

有没有人知道在更改标签标题时如何更改标签大小,但仍保留关闭标签选项?

谢谢你,非常感谢!

1 个答案:

答案 0 :(得分:2)

我从未见过,

但这只有在一种情况下才有可能,你运行的代码不在EDT中,

Swing是单线程的,所有对Swing GUI的更改都必须在Event Dispatch Thread上完成,更多关于Concurency in Swing中的这个主题,(我建议在这个论坛上搜索这个常见主题),< / p>

enter image description here enter image description here

来自代码,

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();
            }
        });
    }
}