JSpinner Arrows in odd positioning when used with JLabel

339 Views Asked by At

Hello So i have created a JFrame with a JSpinner inside (as you can see in the picture). Right now, the BorderLabel is showing in the Jspinner (as it should) but the arrows on the JSpinner are there as a part of the entire thing instead of just the JSpinner field. I would like help to find out how to put the JSpinner arrows on the bar. Thank you.

enter image description here

For you who asked for code,

Also I miss stated JLabel Earlier. I meant TitledBorder

import java.awt.ComponentOrientation;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;


public class ex extends JFrame{
    public static void main(String[] args){
        new ex();


    }
    ex(){
        super("test");
        setSize(200,100);
        SpinnerModel sm = new SpinnerNumberModel(3,1,25,1);
        JSpinner shiftIn = new JSpinner(sm);
        JPanel p = new JPanel();
        shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        shiftIn.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(0,
                0, 0, 0), "Shift Key"), shiftIn
                .getBorder()));
        p.add(shiftIn);
        add(p);
        setVisible(true);
    }
}
2

There are 2 best solutions below

4
On BEST ANSWER
shiftIn.setBorder(new CompoundBorder(
    new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Shift Key"), 
    shiftIn.getBorder()));

enter image description here

This is not the sort of thing that a titled border was made for!

Use a JLabel and put it in the PAGE_START of a BorderLayout, put the JSpinner in the PAGE_END. Add that container (the panel) where the spinner is currently added. (Then add a mnemonic for the label and make it the 'label for' the spinner.)


This is how to use that idea inside another layout (GridLayout in this example)..

enter image description here

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

public class ex extends JFrame {

    public static void main(String[] args) {
        new ex();
    }

    ex() {
        super("test");
        // imagine this is actually using GridBagLayout
        JPanel ui = new JPanel(new GridLayout(0, 3,4,4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));
        SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
        JSpinner shiftIn = new JSpinner(sm);
        JLabel l = new JLabel("Shift Key"); 
        JPanel p = new JPanel(new BorderLayout());
        p.add(l, BorderLayout.PAGE_START);
        p.add(shiftIn, BorderLayout.PAGE_END);
        add(ui);

        for (int ii=0; ii<9; ii++) {
            if (ii==4) {
                ui.add(p);
            } else {
                ui.add(new JButton("Button"));
            }
        }

        pack();
        setVisible(true);
    }
}
2
On

You appear to be running into risk by messing with the JSpinner's border. Myself, I would wrap my JSpinner in a JPanel and then give that wrapper JPanel the desired border. For example:

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

@SuppressWarnings("serial")
public class Ex extends JFrame {
   public static void main(String[] args) {
      new Ex();

   }

   Ex() {
      super("test");
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
      JSpinner shiftIn = new JSpinner(sm);
      JPanel spinnerWrapper = new JPanel(new BorderLayout());
      spinnerWrapper.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEmptyBorder(0, 0, 0, 0), "Shift Key"));
      spinnerWrapper.add(shiftIn);
      shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      add(spinnerWrapper);
      pack(); 
      setVisible(true);
   }
}