Title truncation in JPanel TitledBorder - Java swing

放肆的年华 提交于 2019-12-11 07:51:51

问题


I've got a JPanel with a TitledBorder, but the contents of the panel are narrower than the title in the border and the title gets truncated. I am using BoxLayout for the JPanel which as depicted here pays attention to manual setting of width. I tried to set the minimum, maximum and preferred width of the panel according to the TitledBorder getMinimumSize() function along with the width of its components but all don't work. The only thing that worked was using a box filler but that introduced an undesired indentation.

Any way to the show the full title irrespective to the content it contains?

this.jpCases.setLayout(new javax.swing.BoxLayout(this.jpCases, javax.swing.BoxLayout.LINE_AXIS));
        List<Category> categories = db.getCategories();
        for (Category cat : categories) {
            JPanel jp = new JPanel();
            TitledBorder tb = BorderFactory.createTitledBorder(cat.getDescription());

            jp.setBorder(tb);
            jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
            jp.setAlignmentY(Component.TOP_ALIGNMENT);
            jp.setAlignmentX(Component.LEFT_ALIGNMENT);

            List<Case> cases = db.getCasesByCategoryId(cat.getId());
            for (Case c : cases) {
                JRadioButton jrbCase = new JRadioButton();
                jrbCase.setText(c.getDescription());
                jrbCase.setToolTipText(c.getText());
                jrbCase.setMaximumSize(tb.getMinimumSize(jp));
                bgCases.add(jrbCase);
                jp.add(jrbCase);
            }
        //jp.add(new Box.Filler(tb.getMinimumSize(jp), tb.getMinimumSize(jp), tb.getMinimumSize(jp)));
            this.jpCases.add(jp);
    }

回答1:


What about calculating the needed width:

       JRadioButton jrb = new JRadioButton();
       int width = (int) SwingUtilities2.getFontMetrics( jrb, jrb.getFont() ).getStringBounds( cat.getDescription(), null ).getWidth();
       for (Case c : cases) {
            JRadioButton jrbCase = new JRadioButton();
            jrbCase.setText(c.getDescription());
            jrbCase.setToolTipText(c.getText());
            jrbCase.setPreferredSize( new Dimension( width, jrbCase.getPreferredSize().height ) );
            bgCases.add(jrbCase);
            jp.add(jrbCase);
        }



回答2:


I "solved" it by adding a label (BoxLayout.Y_AXIS) to the top (or bottom) of the panel. The label contains only spaces leading to the same (or greater) width of the title. (Making the label with the very same text as the title invisible does not solve it.)




回答3:


This Works well for me:

 JPanel aPanel = new JPanel(){
      @Override
      public Dimension getPreferredSize(){
           Dimension d = super.getPreferredSize();
           Border b = this.getBorder();
           if(b == null) return d;
           if(!(b instanceof TitledBorder)) return d;
           TitledBorder tb = (TitledBorder)b;
           if(tb.getTitle() == null) return d;
           int insets = 2 * (tb.getBorderInsets(this).left + tb.getBorderInsets(this).right);
           double minWidth = this.getFontMetrics(tb.getTitleFont()).stringWidth(tb.getTitle()) + insets;
           if(d.getWidth() > minWidth) return d;
           return new Dimension((int)minWidth, d.height);
      }
 };


来源:https://stackoverflow.com/questions/6407897/title-truncation-in-jpanel-titledborder-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!