0

I have the following java problem:

I definitively have to use a null layout set by

mainWindow.getContentPane().setLayout(null);

with mainWindow being a JFrame. Now, I want to place objects (JLabel for example) absolutely but depending on the window size. For that, I have to determine the size of the JFrame or that of the contentPane. Is there any way to get that information in a null-layout?

I have tried:

mainWindow.getContentPane().getBounds();
mainWindow.getContentPane().getPreferredSize();
mainWindow.getContentPane().getSize();
mainWindow.getContentPane().getInsets();

And the same for mainWindow itself. But for all of them, I just get zeros.

Either google holds no answer or I've looked for the wrong keywords.

I would be glad to get any advice or link to a solution. Thanks.

Markus
  • 1
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). What you are seeing, is one of the **many** reasons that experienced programmers advise *against* using `null` layouts. – Andrew Thompson Dec 11 '13 at 11:39
  • 2
    *"I definitively have to use a null layout.."* That is never true, ***ever!*** See [this thread](https://forums.oracle.com/thread/1353374) for more. – Andrew Thompson Dec 11 '13 at 11:40
  • Insets ............................ – mKorbel Dec 11 '13 at 11:48
  • 1
    You might try [*Creating a Custom Layout Manager*](http://docs.oracle.com/javase/tutorial/uiswing/layout/custom.html). – trashgod Dec 11 '13 at 13:59
  • `But for all of them, I just get zeros.` - that is because components don't have a size until the frame has been packed or made visible. `Now, I want to place objects (JLabel for example) absolutely but depending on the window size.` - that is the definition of a layout manager, you position the component based on the size of the window. – camickr Dec 11 '13 at 16:59
  • Ok. I see your points. Maybe I just don't have enough experience with the layout manager system. I'll try to build my layout with a custom layout manager. Thanks very much for your replies! – Markus Dec 11 '13 at 20:43
  • Tip: Add @camickr (or whoever - the `@` is important) to *notify* them of a new comment. – Andrew Thompson Dec 13 '13 at 04:29

0 Answers0