2

I'm having endless problems when I need to show the user some very complex interface with save or cancel buttons and need this interface to correctly deal with different monitor resolutions. Suppose for instance this interface needs to fit 17 JTextFields and a resizable JTextArea in a 1280 x 768 monitor (my 13'' laptop has vertical size of 760 pixel).

here is an SSCCE:

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

public class OptionPanePanel extends JFrame
{
    private static Container layoutComponents(String title, float alignment)
    {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++)
        {
            JTextField jtextField= new JTextField("jtextfield "+i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), 
                    new java.awt.Dimension(32767, 20)));
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++)
        {
            JTextField jtextField= new JTextField("jtextfield "+i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), 
                    new java.awt.Dimension(32767, 20)));
        }
        return container;
    }

    public static void main(String args[])
    {
        Container panel1 = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        JOptionPane.showConfirmDialog(
            null, panel1, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);  
    }
}

Now I would like the above example to behave so that:

  1. The size of the window resizes not cropping any of the content
  2. The size of the window somehow deals differently based on the resolution of the monitor.
  3. I dont' have to statically specify maximumSize, MinimumSize and preferredSize (with the NetBeans GUI editor for instance) so that each time I have to make numerous tests just to find out the correct size
  4. the JtextArea resizes itself vertically depending on the vertical resolution up to a maximum.
dendini
  • 3,602
  • 9
  • 35
  • 69
  • *"however I can call pack() and setSize and after some tries find the correct size."* The aim is to do 'some tries' on every single computer that this is intended to run on? Because one number will not necessarily work for another screen/PLAF/OS... – Andrew Thompson Mar 09 '13 at 13:14
  • 2
    If the answer here is not `JScrollPane` then I don't understand the question (which is also a strong possibility). – Andrew Thompson Mar 09 '13 at 13:15
  • The aim is for Java to calculate a correct size for the window based on screen resolution and maybe some minimum size I specify. JScrollPane is too extreme for a solution because I don't want to hide elements. – dendini Mar 09 '13 at 13:24
  • +1 for [sscce](http://sscce.org/). – trashgod Mar 11 '13 at 13:30

3 Answers3

3

You can add an option pane to a dialog, as shown here and here.

As an aside, call setSize() after pack().

Addendum: Here's a variation of your sscce that places the option pane in a scroll pane having an initial size based on screen geometry, as shown here.

image

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

public class OptionPanePanel extends JFrame {

    private static Container layoutComponents(String title, float alignment) {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++) {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++) {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        return container;
    }

    private static Box.Filler createFiller() {
        return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
            new Dimension(Short.MAX_VALUE, 20));
    }

    public static void main(String args[]) {
        Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JScrollPane jsp = new JScrollPane(panel){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 2 * screenSize.height / 3);
            }
        };
        JOptionPane optPane = new JOptionPane();
        optPane.setMessage(jsp);
        optPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
        optPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
        JFrame f = new OptionPanePanel();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.add(optPane);
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • That's another option I have, but it doesn't seem to deal better with respect to sizing than my two proposed examples.. – dendini Mar 09 '13 at 13:52
  • Then I don't understand the question, either. The usual problem is option panes that overflow the screen, for which a [scroll pane](http://stackoverflow.com/a/14011536/230513) may be warranted. – trashgod Mar 09 '13 at 13:57
  • I edited my question with an SSCCE because I realized it was too generic. – dendini Mar 11 '13 at 09:45
  • I've elaborated above; see also this [Q&A](http://stackoverflow.com/q/3680221/230513). – trashgod Mar 11 '13 at 10:12
  • Good answer although I don't like the idea of scrolling a dialog. I actually changed your monitor size detection for multimonitor setups.. see my last edit – dendini Mar 12 '13 at 11:40
2

how can I show something like the above so that:

I don't know what that means. Post your SSCCE that shows us exactly the problem you are having.

This works fine for me:

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

public class OptionPanePanel extends JFrame
{
    public OptionPanePanel()
    {
        JPanel panel = new JPanel( new BorderLayout() );
        JPanel north = new JPanel();
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );

        JTextArea textArea = new JTextArea(5, 20);

        panel.add(north, BorderLayout.NORTH);
        panel.add(new JScrollPane(textArea));

        int result = JOptionPane.showConfirmDialog(
            this, panel, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    }


    public static void main(String[] args)
    {
        JFrame frame = new OptionPanePanel();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}
camickr
  • 308,339
  • 18
  • 152
  • 272
  • I added an SSCCE with the specific problem, your SSCCE aligns horizontally all its JTextFields while the problem is the vertical real estate of monitors... – dendini Mar 11 '13 at 09:47
0

Thank you for you answers, so far I came with this solution: get the monitor height and if smaller than 1024 then show a small dialog within a JscrollPane (thanks to trashgod for pointing it), otherwise show a normal dialog with standard height (I have to calculate by trial unfortunately)

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

public class OptionPanePanel extends JFrame
{

    private static Container layoutComponents(String title, float alignment)
    {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++)
        {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++)
        {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        return container;
    }

    private static Box.Filler createFiller()
    {
        return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
                new Dimension(Short.MAX_VALUE, 20));
    }

    public static void main(String args[])
    {
        Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        /*Let's check the monitor height in multi monitor setup */
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int monitorHeight = gd.getDisplayMode().getHeight();
        int result;
        if (monitorHeight <= 1024)
        {
            final Dimension preferredDimension = new Dimension(500, monitorHeight - 110);
            panel.setPreferredSize(preferredDimension);
            JScrollPane jsp = new JScrollPane(panel)
            {
                @Override
                public Dimension getPreferredSize()
                {
                    return new Dimension(500, 700);
                }
            };
            result = JOptionPane.showOptionDialog(null, jsp,
                    "",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    new String[]
                    {
                        ("save"), ("cancel")
                    }, // this is the array
                    "default");
        }
        else
        {
            final Dimension preferredDimension = new Dimension(500, 700);
            panel.setPreferredSize(preferredDimension);
            result = JOptionPane.showOptionDialog(null, panel,
                    "",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    new String[]
                    {
                        ("save"), ("cancel")
                    }, // this is the array
                    "default");
        }

        if (result == JOptionPane.OK_OPTION)
        {
            //do something
        }
    }
}
dendini
  • 3,602
  • 9
  • 35
  • 69