user1527336
user1527336

Reputation:

how to show items in JOptionPane.showOptionDialog horizontally?

I'm trying to put a TextBox & a Button in a JOptionPane.showOptionDialog horizontally. I've used this code.

JTextField txt = new JTextField();
JButton btn = new JButton("Button");
int value = JOptionPane.showOptionDialog(this,
                    new Object[]{txt, btn},
                    "Hello World",
                    JOptionPane.OK_CANCEL_OPTION, 
                    JOptionPane.INFORMATION_MESSAGE,
                    null, null, null);

But TextBox & Button showing vertically. How can I show them in horizontally ? Please help... Thanks.

Upvotes: 2

Views: 1138

Answers (1)

Vinay
Vinay

Reputation: 6881

From here you can actually do it like this, setting an JPanel which has a textfield and a button.

int value = JOptionPane.showOptionDialog(this,
                    getPanel(),
                    "Hello World",
                    JOptionPane.OK_CANCEL_OPTION, 
                    JOptionPane.INFORMATION_MESSAGE,
                    null, null, null);

private JPanel getPanel() {
    JPanel panel = new JPanel();
    JTextField txt = new JTextField(20);
    JButton btn = new JButton("Button");

    panel.add(txt);
    panel.add(btn);

    return panel;
}

EDIT Each JPanel object is initialized to use a FlowLayout, unless you specify differently when creating the JPanel. as per the doc here.

Upvotes: 4

Related Questions