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