K2xL
K2xL

Reputation: 10330

setSize of JOptionPane

Trying to set size of a JOptionPane but it's sticking with the same size. I've tried setPreferredSize and setSize but for some reason the JOptionPane is sticking with the same width and height. Basically I have a bunch of text and it's being "cut off" because of the size of the window.

I'm actually using a port of the swing library in another language, so it could be a bug with their library - but according to the docs it should mirror the Java Swing calls.

Am I missing something?

Upvotes: 0

Views: 13917

Answers (4)

Jens Elbaek
Jens Elbaek

Reputation: 11

You can just as well override setSize by extending our Componemt:

public class Headerfield extends JLabel
{
    public Headerfield(String text)
    {
        super(text);
    }

    public void setSize(int width, int height)
    {
        super.setSize(100, 20);
    }
}

Upvotes: 0

Hemant Metalia
Hemant Metalia

Reputation: 30698

It is good idea to create a JPanel with prefereed size or JTextArea with JScrollPane scrollbars and add it to optionpan.

It's easy to do, and not only sets the size of the dialog but allows great flexibility of content.

JTextArea mytext = new JTextArea();
mytext.setText("mytextline1\nmytextline2\nmytextline3\nmytextline4\nmytextline5\nmytextline6");
mytext.setRows(5);
mytext.setColumns(10);
mytext.setEditable(true);
JScrollPane mypane = new JScrollPane(mytext);

Object[] objarr = {
    new JLabel("Enter some text:"),
    mypane,
};

JOptionPane Optpane = new JOptionPane(objarr, JOptionPane.PLAIN_MESSAGE); 

see details

Upvotes: 5

K2xL
K2xL

Reputation: 10330

Actually, found the fix. The ported library built the JOptionPane I guess slightly different than the Java version.

optionpane.getFrame().setSize()

(using ASwing - Actionscript port of Java Swing)

Upvotes: 1

xyz
xyz

Reputation: 201

Check the layout manager of the container

Upvotes: 3

Related Questions