Member Avatar for hfx642

I have a few JSpinner(s) and I'm trying to change the button sizes.
I know how to change the font of the SpinnerModel(s),
but not the button sizes.
Can someone please point me in the right direction?
TIA!

Recommended Answers

All 7 Replies

@hfx642 good question +1

but SpinnerModel has nothing to do with JSpinner' View, there you have to change Font, its size ...

to the JButton size(s) that's depend of JSpinner size, or are you meaning triagle JButtons Icon ???

anyway there are two way

1/ extract all JComponents from JSpinner (JFormattedTextField + two JButtons)

a/ by DefaultSpinnerUI but overide that for all Native OS
b/ MetalSpinnerUI for reduced usage of ..

2/ create own/custom JComponents

Member Avatar for hfx642

I understand that the SpinnerModel has nothing to do with sizing.
I was just trying to say that I can change the font of the JTextField contained within the JSpinner.
I realize that I would have to isolate the two JButtons,
(as I have already isolated the JTextField to change the font),
but how do I change the size of the JButtons?
TIA!

Font in JTextField ???

o.k. please edit my code with your question, beacuse/maybe there is/are small mistake(s), just with intention to avoid that :-)

1/ how you are extract JTextField, because there is JFormattedTextField

2/ and override preffered size for Two JButtons (hint change Icon)

DISLAIMER FOR CODE POLICIES == @ JamesCherrill this code is Free to use and please distribute that everywhere
import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.LineBorder;

public class InactiveBackgroundTest {

    public JComponent makeUI() {
        JSpinner s0 = new JSpinner();
        s0.setPreferredSize(new Dimension(100, 20));
        s0.setEnabled(false);
        UIManager.put("FormattedTextField.inactiveBackground", Color.RED);
        JSpinner s1 = new JSpinner();
        s1.setEnabled(false);
        s1.setPreferredSize(new Dimension(100, 20));
        JSpinner s2 = new JSpinner();
        s2.setEnabled(false);
        s2.setPreferredSize(new Dimension(100, 20));
        JTextField field = ((JSpinner.NumberEditor) s2.getEditor()).getTextField();
        field.setEditable(false);
        field.setBackground(UIManager.getColor("FormattedTextField.background"));
        JSpinner s3 = new JSpinner();
        s3.setPreferredSize(new Dimension(100, 20));
        s3.setEnabled(false);
        s3.setBorder(null);
        JTextField tf = ((JSpinner.DefaultEditor) s3.getEditor()).getTextField();
        tf.setDisabledTextColor(Color.black);
        tf.setBackground(Color.white);
        tf.setBorder(new LineBorder(Color.blue, 1));
        s3.setBorder(new LineBorder(Color.red, 1));
        int n = s3.getComponentCount();
        if (n > 0) {
            Component[] components = s3.getComponents();
            String compName = "";
            for (int i = 0, l = components.length; i < l; i++) {
                if (components[i] instanceof JButton) {
                    JButton button = (JButton) components[i];
                    if (button.hasFocus()) {
                        String btnMane = button.getName();
                    }
                    button.setBorder(new LineBorder(Color.red, 1));
                    System.out.println("JButton");
                } else if (components[i] instanceof JComboBox) {
                    System.out.println("JComboBox");
                } else if (components[i] instanceof JTextField) {
                    System.out.println("JTextField");
                } else if (components[i] instanceof JFormattedTextField) {
                    System.out.println("JFormattedTextField");
                } else if (components[i] instanceof JTable) {
                    System.out.println("JTable");
                } else if (components[i] instanceof JScrollPane) {
                    System.out.println("JScrollPane");
                } else if (components[i] instanceof JPanel) {
                    JPanel panel = (JPanel) components[i];
                    panel.setBackground(Color.red);
                    panel.setBorder(null);
                    System.out.println("JPanel");
                }
            }
        }
        JPanel p = new JPanel();
        p.setBackground(Color.black);
        p.add(s0);
        p.add(s1);
        p.add(s2);
        p.add(s3);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI() {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*try {
            // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if (info.getName().equals("Nimbus")) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }*/
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new InactiveBackgroundTest().makeUI());
        f.setPreferredSize(new Dimension(120, 140));
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
    }
}

EDIT: why reinvent the wheel, because there are lots of Custom Java Look & Feel, with non-bugged setting for majorities of JComponents and with correct output to the GUI

Member Avatar for hfx642

mKorbel
Altering the code presented, as follows...

JSpinner s3 = new JSpinner();

// This will alter the Size of the entire JSpinner... but so does a font change
s3.setPreferredSize(new Dimension(100, 100));

// As long as the dimensions fit within the JSpinner size,
// this will enlarge the X Size of the button (but not the Y Size),
s3.getComponent (0).setPreferredSize (new Dimension (75, 75));
// which IS what I was looking for.

...does the trick.
Thank-You!!!

HI,

I too have the same problem. I have followed the example.I am using windows 7 if I look and feel as "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" it works fine if not it does not works. Any idea to solve this isssue?

Regards,
Selva.

Member Avatar for hfx642

Even though I was able to make the example work,
I still haven't been able to make MY application work.
(I'll get back to it some time next month. I have other stuff to do for now.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.