Question

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
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top