0

This is perhaps more of a whine than a question, and I'm aware there's a workaround to the problem described in this post. But I have a situation where I have to add components to a GridLayout, possibly enlarging the grid as I go; but the components don't get created and inserted in a nice neat order; and I don't know when I'm done being handed components to insert into the grid. In other words, in a 3x3 grid I might get handed a component to put at (0,2) and then another at (1,0) and then one at (0,0). And then I might get one at (5,2) and need to enlarge the grid. And then I might get told to replace the line at (0,0) with something else.

I understand that there's no way to say "put a component at x, y". I get that I'm going to have to build a 2D array to hold my components and then empty and refill the grid from the array, each time it changes, which is going to be quite often in several large grids, so I'd rather not. I get that life is like that and the language is the language and who am I to question why.

But I'm really curious. This seems like the most basic of operations for a grid to support. Not having it feels like I'm working with a spreadsheet that only lets you enter values in order from left to right - it's simple madness.

I haven't taken apart the source code for GridLayout, but any naive understanding of how it "must work" makes me think add(component, row, col) should be trivial to implement. It must not be, because GridLayout isn't exactly new and I can't believe I'm the first to think random access to a grid is a good idea. So it must be really hard. But why?

I realize understanding why the internals of GridLayout can't support this, doesn't solve any real problems, except the problem of me unable to stop thinking about how fundamentally weird the restriction is. In the end I'm going to end up with a parallel array, or messing with the more complex GridBagLayout, unless someone knows a better way. But I just want to know why I have to.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Scott M
  • 684
  • 7
  • 14
  • `GridLayout` isn't really a "grid" in the concept you are thinking of it, it's more about taking the child components that are available and laying them out in a grid pattern. *""must work" makes me think add(component, row, col)"* - What about all the layouts that don't support a grid like layout? I would, personally do one of three things. Either add "blank" components into every "cell" position and either use them as the bases for the components you want to add or replace them as needed; use `GridBagLayout`, but, you'll have similar issues; Write my own layout manager – MadProgrammer Jan 28 '20 at 02:20
  • *"But I just want to know why I have to"* - Layout management is actually a complex problem - you should give it a go and see. Not saying your idea is "wrong" or "bad" but it might not be a suitable solution for everyone - it's just another way of approaching a given problem – MadProgrammer Jan 28 '20 at 02:22
  • *I understand that there's no way to say "put a component at x, y".* - depending on your exact requirement, you could use a `GridBagLayout`. You can define the grid dimensions and then add components randomly to any cell in the grid. See: https://stackoverflow.com/a/59022153/131872 for an example of this approach. – camickr Jan 28 '20 at 03:16
  • I ended up with GridBagLayout, with each cell being a JPanel I could stuff a component and glue into. – Scott M Jan 31 '20 at 20:11

1 Answers1

2

Why not approach the problem from a different angle:

  • Create your grid using GridLayout
  • Fill the grid with empty JPanels
  • JPanels that each uses a BorderLayout
  • Keep these JPanels where they are
  • But swap the JPanels that they display within them in each of their BorderLayout.CENTER positions

Alternatively, you could have them use a CardLayout, but the effect would be the same

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Oh. Yeah. I'm off to implement this and then I'll accept this as the answer. It nicely answers the question "why didn't they implement..." with the point "because there's a trivial workaround." – Scott M Jan 28 '20 at 02:22