0

Here's my code:

public class PaymentDialog {

    public static int show(Double value) {
        JLabel total = new JLabel("Total: ");
        JLabel totalValue = new JLabel(value.toString());
        JLabel input = new JLabel("Entrada: ");
        JTextField inputField = new JTextField();
        JLabel change = new JLabel("Troco: ");
        JLabel changeValue = new JLabel("1234");
        JComponent[] components = new JComponent[] {
            total,
            totalValue,
            input,
            inputField,
            change,
            changeValue
        };
        int result = JOptionPane.showConfirmDialog(null, components, "Pagamento", JOptionPane.OK_CANCEL_OPTION);
        return result;
    }

}

Here's an image of it:

enter image description here

As you can see, each component occupies an entire line, which is atrocious. The label should be behind it's corresponding component, instead of above it.

How do I customize this layout behavior? If it's impossible, how could I create a custom JDialog that allows me to do it? (Since I don't want to create a JFrame to customize the layout properly, as it's not the same thing).

Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
  • 1
    Interesting approach, add all your components to their own `JPanel` and pass that as the value parameter - [as a conceptual example](http://stackoverflow.com/questions/12008100/formatting-text-in-jdialog-box/12008603#12008603) and probably [a more relevent example](http://stackoverflow.com/questions/14783836/joptionpane-and-reading-integers-beginner-java/14784096#14784096) – MadProgrammer Mar 02 '17 at 04:44
  • 1
    Oh, that's clever. I'll try that using a grid layout on a single JPanel. – Ericson Willians Mar 02 '17 at 04:46
  • Yep, that's the idea, if you get something working, post it as self-answered question ;) – MadProgrammer Mar 02 '17 at 04:47

2 Answers2

2

You can add any component to the option pane. So you can create your own panel and use any layout and add any components you want.

One problem is that the text field won't have focus. Focus will be on a button of the option pane.

If you want focus on the text field then check out the RequestFocusListener found in Dialog Focus. This will allow you to control which component on the panel gets focus when the option pane is displayed.

It also contains sample code for creating a panel with a GridLayout to display on the option pane.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

enter image description here

I'll post the code for future humans:

public class PaymentDialog {

    public static int show(Double value) {
        JPanel componentPanel = new JPanel();
        componentPanel.setLayout(new GridLayout(3, 2));
        JLabel total = new JLabel("Total: ");
        JLabel totalValue = new JLabel(value.toString());
        JLabel input = new JLabel("Entrada: ");
        JTextField inputField = new JTextField();
        JLabel change = new JLabel("Troco: ");
        JLabel changeValue = new JLabel("1234");
        componentPanel.add(total);
        componentPanel.add(totalValue);
        componentPanel.add(input);
        componentPanel.add(inputField);
        componentPanel.add(change);
        componentPanel.add(changeValue);
        int result = JOptionPane.showConfirmDialog(null, new JComponent[]{componentPanel}, "Pagamento", JOptionPane.OK_CANCEL_OPTION);
        return result;
    }

}
Ericson Willians
  • 7,606
  • 11
  • 63
  • 114