0

I would like to have JFormattedTextField components in one column. I don´t understand why this code causes to enlarge to maximum size.

    // Create the text fields and set them up.
    priemerKolesa = new JFormattedTextField(numberFormat);
    priemerOsi = new JFormattedTextField(numberFormat);
    hrubkaPlasta = new JFormattedTextField(numberFormat);
    hrubkaKolesa = new JFormattedTextField(numberFormat);
    hrubkaNavinu = new JFormattedTextField(numberFormat);
    hrubkaNavinu.setColumns(6);
    // Layout the text fields in a panel.
    JPanel fieldPane = new JPanel();
    fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
    fieldPane.add(priemerKolesa);
    fieldPane.add(priemerOsi);
    fieldPane.add(hrubkaPlasta);
    fieldPane.add(hrubkaKolesa);
    fieldPane.add(hrubkaNavinu);

I want to have it in a nice stack, not expanded like this.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
palyxk
  • 341
  • 2
  • 4
  • 17
  • You can simply just add it without a layout. But make sure the lenght of your JTextfield is almost equal the size of your Panel. – Thecarisma Nov 10 '16 at 15:13
  • @Thecarisma *"You can simply just add it without a layout."* Please stop giving layout advice until you realize *why* that is such poor advice! Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 10 '16 at 17:09
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Nov 10 '16 at 17:09
  • 3) `[pic][1]` It seems you were trying to add a screenshot. Please review the preview of your question before posting it! 4) In the spirit of making an MCVE, 1st try to get it to work using just two formatted text fields of different size. Once you have that working, it should be obvious how to do it for 3, 4 or ..50 of them. – Andrew Thompson Nov 10 '16 at 17:12

1 Answers1

1

It seems that BoxLayout stretches components to fit the available space. One way around that it to first put each component into a JPanel with FlowLayout (which will not stretch the components within) then add that panel to the container using BoxLayout. E.G. in the form of an MCVE as mentioned above:

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

public class BoxLayoutQuandary {

    private JComponent ui = null;

    BoxLayoutQuandary() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        NumberFormat numberFormat = new DecimalFormat("##.#");
        // Create the text fields and set them up.
        JFormattedTextField priemerKolesa = new JFormattedTextField(numberFormat);
        priemerKolesa.setColumns(8);
        JFormattedTextField hrubkaNavinu = new JFormattedTextField(numberFormat);
        hrubkaNavinu.setColumns(6);
        // Layout the text fields in a panel.
        JPanel fieldPane = new JPanel();
        fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
        ui.add(fieldPane);
        JPanel priemerKolesaPanel = new JPanel();
        priemerKolesaPanel.add(priemerKolesa);
        fieldPane.add(priemerKolesaPanel);
        JPanel hrubkaNavinuPanel = new JPanel();
        hrubkaNavinuPanel.add(hrubkaNavinu);
        fieldPane.add(hrubkaNavinuPanel);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BoxLayoutQuandary o = new BoxLayoutQuandary();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433