JSpinner Arrows in odd position when used with JLabel
Hello, So I created a JFrame with a JSpinner inside (as you can see in the picture). Right now, BorderLabel shows up in the Jspinner (as it should), but the arrows on the JSpinner are part of everything, not just the JSpinner fields. I would like to help find out how to put JSpinner arrows on the panel. Thank.
For you who requested the code,
Also I missed the 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);
}
}
source to share
shiftIn.setBorder(new CompoundBorder(
new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Shift Key"),
shiftIn.getBorder()));
This is not the thing that had the title frame on it!
Use JLabel
and place it in PAGE_START
in BorderLayout
, place JSpinner
in PAGE_END
. Add this container (panel) where the spinner is currently added. (Then add a mnemonic for the label and make it the "label for" the counter.)
Here's how to use this idea inside another layout ( GridLayout
in this example).
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);
}
}
source to share
It looks like you are running into risk messing around with the JSpinner border. I would wrap my JSpinner in a JPanel myself and then provide that JPanel wrapper with 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);
}
}
source to share