0

I am writing a stylish CLI in Java using Swing and the Graphics2d APIs but am having a problem reverse stacking the output of any given input. In other words, most layout managers seem to position from Left to Right or Top to Bottom and none of them seem to be able to insert into the middle or between two JComponents or move one component up and above after a new component been added/repainted/revalidated. Any help or direction is much appreciated, as I have tried searching and am unable to find an answer to this question.

This is essentially what I'm looking for:

enter image description here

User inputs command and presses enter, input is output along with whatever else that needs to follow, user can then type in next command.

Braj
  • 46,415
  • 5
  • 60
  • 76
joshuar500
  • 164
  • 1
  • 15
  • better to share a link of the snapshot of the desired design. – Braj Aug 05 '14 at 20:07
  • Added link to snapshot! – joshuar500 Aug 05 '14 at 20:20
  • @joshuar500 Are you asking how to append text to the bottom of a Swing component that can hold text? – NESPowerGlove Aug 05 '14 at 20:30
  • @NESPowerGlove No. ADD a Swing component(s) above the input component. – joshuar500 Aug 05 '14 at 20:36
  • And then move that component up when a new component is added where the input component is left unmoved. – joshuar500 Aug 05 '14 at 20:37
  • @joshuar500 So, like a JPanel with BorderLayout that contains a textfield and button on the south position (both are added to the same JPanel with a FlowLayout), and a JPanel with a GridLayout(0, 1) in the main panel's center position that you add components to routinely everytime someone interacts with the bottom input components? I think you just need to nest your containers and mix and match layouts as needed. – NESPowerGlove Aug 05 '14 at 21:01
  • [`JTextArea.append`: Appends the given text to the end of the document](http://docs.oracle.com/javase/8/docs/api/javax/swing/JTextArea.html#append-java.lang.String-) <-- there. – Boann Aug 05 '14 at 21:17
  • @NESPowerGlove Thanks. I think that is the solution that makes the most sense. – joshuar500 Aug 05 '14 at 21:26
  • You have two basic solutions, write your own layout manager or usin something like GridBagLayout. You could also add each component to the end of the component list.... – MadProgrammer Aug 05 '14 at 21:46
  • Also consider converting to a GUI application as outlined [here](http://stackoverflow.com/a/24726834/230513). – trashgod Aug 05 '14 at 22:17

1 Answers1

1

most layout managers seem to position from Left to Right or Top to Bottom and none of them seem to be able to insert into the middle or between two JComponents

Depends on which layout manager you use. You can use a FlowLayout, BoxLayout or GridLayout and insert the components anywhere you want using:

panel.add(component, index);

Other layout managers like BorderLayout and GridBagLayout require constraints to add components so no you can't control the ordering of the component.

ADD a Swing component(s) above the input component

I would guess a BoxLayout would be the layout to look at.

camickr
  • 321,443
  • 19
  • 166
  • 288