0

So my issue is, which I've had before with Jspinners. Is that I surround my jspinner with a titled border. However in the gui it cuts of the title text because the js is smaller. So how can I force the gui to rezise based on the length of the title, or how to I manger to make to the jspinner longer i guess to provide enough room to fit the entire title?

JTextField name= new JTextField(9);
name.setBorder(new TitledBorder("Name"));
//setup spinner date format
SimpleDateFormat datePattern = new SimpleDateFormat("MM/dd/yyyy");
JSpinner dob = new JSpinner(new SpinnerDateModel());
dob.setEditor(new JSpinner.DateEditor(dob, datePattern.toPattern()));
dob.setBorder(new TitledBorder("Date of Birth"));
JPanel childPanel = new JPanel();
childPanel.setLayout(new FlowLayout());
childPanel.add(name);
childPanel.add(dob);
childPanel.setBorder(new TitledBorder("Child Info"));

Then:

JPanel mainPanel = new JPanel(); 
mainPanel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));        
mainPanel.add(childPanel);
childPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
//there are more items in this panel, just omitted them, that's why using BoxLayout
whitewolfpgh
  • 441
  • 2
  • 6
  • 13
  • I've encountered this issue before when the titledborder of a jspinner is even longer than this example. I've also tried .setMaximumSize(object.getPreferredSize()); on the spinner, the childPanel, and the mainPanel. Neither seem to fix the issue. – whitewolfpgh Jun 12 '12 at 15:09
  • `a)` maybe use Nimbus Look and Feel, instead of bothering with XxxBorders, `b)` I'd suggest that only in the case that you'll never to set for Background or Foreground, notice should be so hard and tricky to set both values for Nimbus L&F – mKorbel Jun 12 '12 at 15:20
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One of the gurus of Swing curses the `TitledBorder` - it has quirks & problems. – Andrew Thompson Jun 12 '12 at 16:25

1 Answers1

4

Just add the JSpinner to a JPanel, and add the border to the panel:

    JPanel dobPanel = new JPanel();
    dobPanel.add(dob);
    dobPanel.setBorder(BorderFactory.createTitledBorder("Date of Birth"));

EDIT: Also note that using a BorderFactory is better than creating Border instances:

Wherever possible, this factory will hand out references to shared Border instances.

http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • thx for the tip for using borderfactory :). And thanks your solution worked, why didn't I think of it!?!?! – whitewolfpgh Jun 12 '12 at 16:26
  • 2
    +1 For reference, "we recommend that you put the component in a `JPanel` and set the border on the `JPanel`"—[`setBorder()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setBorder%28javax.swing.border.Border%29). – trashgod Jun 12 '12 at 17:50