Josh
Josh

Reputation: 6272

Netbeans JDialog size

I'm creating a JDialog with netbeans as my IDE. I change the size to how I want it in netbeans, and I can see it updating the prefferredSize, but when I run my application it looks like this:

enter image description here

Is there something else I need to set to set the size? Or at least make it sized properly so I can see the controls... (theres 6 on it)

Upvotes: 3

Views: 5225

Answers (4)

Piseth Sok
Piseth Sok

Reputation: 1819

Download latest Netbean IDE from https://netbeans.org/downloads/

And set the following property: Jframe property

Good luck!

Upvotes: 1

user330315
user330315

Reputation:

In the form designer switch to the "Code" tab of the properties window. There is a property named "Form Size Policy".

This defaults to pack() which means that the dialog is resized to be as big as the contained components require it. For more details please read the Javadocs for the pack() method. Without knowing what components you put on the dialog, it's hard to tell why pack() doesn't work for you.

If you change that to "Generate Resize Code" the dialog will stick to the size you define:

enter image description here

Upvotes: 4

COD3BOY
COD3BOY

Reputation: 12092

I guess you might be using the Netbeans GUI builder, possible solutions/suggestions,

  1. You might be missing dialog.pack();
  2. Right click jDialog > Properties > (Set) minimumSize
  3. (Suggestion) Ditch the GUI Builder, learn Java instead of learning an IDE !

btw, this works for me ,

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DialogsTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();

                f.setSize(400, 300);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);

                JPanel message = new JPanel();
                message.add(new JLabel("This is a dialog :)"));
                JDialog dialog = new JDialog(f, "Dialog");

                dialog.setContentPane(message);
                dialog.pack();
                dialog.setLocationRelativeTo(f);
                dialog.setVisible(true);
            }
        });
    }
}

Upvotes: 3

StartingGroovy
StartingGroovy

Reputation: 2860

Use the setSize() method instead. For some reason this works, but setPreferredSize() doesn't (at least not as expected).

By the way, it's usually advised not to set a specific size on components.

Upvotes: 0

Related Questions