-1

I am attempting to code a JFrame containing a JPanel. Within the JPanel is an array of JTextField's. So, my GUI looks like:-

enter image description here

I am not using a layout manager, and have set this to null for the JFrame and the JPanel. I am sizing these components by hand.

You can see that the right hand portion of the JPanel is chopped off, even though I have used the same sizing as the containing JFrame.

The code appears as below:-

enter image description here

I have calculated the required width of the JPanel by multiplying the number of columns in the JTextField array by the width of the JTextField. Aside from that would need to be added the width of each gap between the JTextFields (there would be (columnNumber - 1) of them), as well as the two border gaps.

I have done this, yet the right hand side border gap is chopped off, as you can see from the diagram.

If I add some random amount to the panelWidth, then you can see the right hand gap there, but my question is what am I missing here? This ought to work surely, if the JFrame side and the JPanel size are identical, which they are as I have also printed them both out, and the print outs give the same number.

Jeremy

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jeremy Watts
  • 147
  • 1
  • 2
  • 8
  • 2
    1) Don't post images of code, post the text. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) *"I am not using a layout manager, .. what am I missing here?"* A layout manager. Learn how to use them, combinations of them if needed. The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the `CENTER` of a border layout, the flow layout in the `PAGE_END`, pack the top level container (for non-cropped, 'right size') & the job is done. – Andrew Thompson Oct 07 '18 at 16:12

2 Answers2

2

I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing.

The GridLayout allows you to specify a horizontal/vertical gap between each component and allows you to control the size of the grid.

Then you can wrap the panel using a GridLayout in a panel that respects the size of the grid.

For example you could do:

JPanel grid = new JPanel( new GridLayout(...) );

JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(grid, new GridBagConstraints());

frame.add(wrapper, BorderLayout.CENTER);

If you pack the frame, the grid panel will be displayed at is preferred size.

If you resize the frame the grid panel will remain centered in the wrapper panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hi there, Many thanks for the reply. I have just implemented your suggestion, and yes it seems to have worked. However, the JTextFields appear as tiny boxes, but are not resizing if I resize the JFrame - which is what I wanted. My question is now, what do I need to pass as arguments to the GridBagConstraints class to force it to use the default sizing for the JTextField? Jeremy – Jeremy Watts Oct 08 '18 at 18:30
  • 1
    @JeremyWatts - `the JTextFields appear as tiny boxes` - You should use `new JTextField(10)`, for example. The 10 will allow the text field to calculate its own preferred size to display 10 "W" and based on other properties like the Font and Border. This size will adjust if any property of the text field is changed. – camickr Oct 08 '18 at 20:06
1

The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the CENTER of a border layout, the flow layout in the PAGE_END, pack the top level container (for non-cropped, 'right size') & the job is done.

It might end up looking something like this:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I have already tried using GridLayout - it produced undesirable results. For instance, I am allowing the user to set the Row/Column numbers themselves, and using a GridLayout seemed to change the size of the JTextFields for each combination of row/column numbers selected. – Jeremy Watts Oct 07 '18 at 17:47
  • Basically, I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing. When I was trying this before, I could never get it to do that. – Jeremy Watts Oct 07 '18 at 17:52
  • *"using a GridLayout seemed to change the size of the JTextFields"* Did you *"**pack** the top level container"*? If you seriously want to solve this, post an MCVE like I suggested 9 hours ago. I'm not wasting more time on this until you put in the minimum necessary effort. – Andrew Thompson Oct 08 '18 at 01:35