如何设置GridBagLayout的宽度?

时间:2015-09-24 09:33:50

标签: java swing gridbaglayout

我有一个JPanel,我正在添加gridbagLayouts。 但有时候,gridbaglayout不足以显示树的标题 像这样:

enter image description here

如何设置此宽度?

   DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/some_db", "new_user", "some_password");

1 个答案:

答案 0 :(得分:1)

当鼠标悬停在标题上时,也许您可​​以为面板设置工具提示以显示整个边框标题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitledBorderTest
{
    private static void createAndShowUI()
    {
        UIManager.getDefaults().put("TitledBorder.titleColor", Color.RED);
        Border lowerEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        String titleText = "Long title that will be truncated in the panel";
        TitledBorder title = BorderFactory.createTitledBorder(lowerEtched, titleText);

        JPanel panel = new JPanel()
        {
            @Override
            public String getToolTipText(MouseEvent e)
            {
                Border border = getBorder();

                if (border instanceof TitledBorder)
                {
                    TitledBorder tb = (TitledBorder)border;
                    FontMetrics fm = getFontMetrics( getFont() );
                    int titleWidth = fm.stringWidth(tb.getTitle()) + 20;
                    Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());
                    return bounds.contains(e.getPoint()) ? super.getToolTipText() : null;
                }

                return super.getToolTipText(e);
            }
        };
        panel.setBorder( title );
        panel.setToolTipText(title.getTitle());

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}