0

I'm working on a game that has a chatbox in it, but despite looking into it, (I might not be searching for the right thing), I can't figure out the best way to create a custom 'scroll pane' for a 'textbox' draw on the graphics. This horrible paint representation kind of shows what I'm trying to accomplish:

enter image description here

If I can give anymore information on this, please ask. This is the best way I could figure out how to explain it.

Edit: OK, so to give more info on this, the JPanel the game uses to display the game. It's done via overriding PaintComponent in the JPanel. I'm not sure if it's possible to add a new component to draw this with. If it is, then that might work. I just need to be able to position it in a specific spot. If adding a new component to this would work, then that would work and I feel like an idiot.

1 Answer 1

1

Option 1) Use a JTextArea held in a JScrollPane. Option 2) Use a JList held in a JScrollPane. The JTextArea is a little simpler, but the JList works better and is more responsive if it needs to hold a lot of data. The tutorials can be found here:

Note that this question could have been very quickly answered by you after a quick scan of the Swing Tutorials, and so for future reference, I've included a link to them.


Edit
Please consider these options for possibly a better solution:

  • Remember that your GUI can use more than one JPanel, and in fact can and often will use many.
  • Use your drawing JPanel for just that, drawing and nothing more.
  • In other words, don't have it hold any other components.
  • Place it BorderLayout.CENTER in another JPanel that uses, of course, a BorderLayout.
  • Place your other non-graphic control structures in JPanels that are held by the same BorderLayout-using JPanel described above. This way your control structures, such as your JScrollPane, is displayed in the GUI, but is not over-lying and covering the drawing JPanel.
  • Add your BorderLayout-using main JPanel into your top-level window, or if need be, in another JPanel if you need a more complex GUI.
  • You will want these sub JPanels to use layouts of their own, so that the whole GUI has a pleasing appearance that is easy to change and enhance.
Sign up to request clarification or add additional context in comments.

5 Comments

The issue with this is that this whole thing is within a JPanel and the bulk of it is drawn with Graphics/Graphics2D, making this slightly more complicated.
@VoidWhisperer: so is it that you're wanting to create your own drawn textarea and scrollpane, essentially re-inventing the wheel? Please tell us the details of your problem.
To an extent.. yes. I would use the things listed above.. except... since I'm already overriding PaintComponent in the JPanel, there's not a whole ton I can do here.
@VoidWhisperer: what do you mean? Just because you're overriding paintComponent doesn't mean that you can't have the JPanel hold standard components. Again, please expand on your question. Again, please tell us the details. It can be very frustrating for you and us for us to have to extract information bit by bit from you. Let's avoid that if at all possible by you telling us the whole complete story in an edit to your original question. Thanks in advance for your efforts in this regard.
Updated it, pointing out that adding those things to it will actually work, then that possibly/probably solves the problem. I just didn't think that was possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.