-3

I'm working on a 2D RPG game and am currently working on a dialogue window that pops up, to prompt the user to enter the quantity of a given item they would like to buy/sell in a shop. For this I need the user to be able to provide a number as input.

I've read some docs on JTextFields, but I'm mostly using my own graphics in this game, drawing to the screen with a Graphics object and don't want to use the standard Java JTextField Swing graphics, as it would be out of place. I have my own, fully working, KeyManager handler class that handles keyboard input in my game. Do you know of any approaches that would allow me to make custom text fields, which are focusable (to type in)?

Another question I have is: Is it possible to remove the JTextField border and set the background to full transparent, so only the cursor for typing is visible and I can add my own implementations of a "textbox"?

Then finally: My game opens with a JFrame and the graphics are drawn to a Canvas. A JTextField should be added to a JFrame, to a JPanel, right? If this is the case, should my DialogueWindow class extend a new JFrame and contain a JPanel to add the JTextField to or do I have to add this to my existing JFrame?

I have little experience with using the Swing library, so hoping someone can answer these questions for me.

tl;dr:

I don't want my input field to look like the standard JTextField below, how to implement my own without using the standard Swing graphics?

"How I don't want it"

iPsych0
  • 19
  • 3
  • 1
    https://stackoverflow.com/questions/6008058/customizing-jtextfield and https://stackoverflow.com/questions/19919554/java-textfield-custom-picture-look – Steve Smith Sep 19 '17 at 13:42
  • 2
    Swing is quite customizable, but setting the JEditField border to background color, the font to something fancy, would do. – Joop Eggen Sep 19 '17 at 13:43
  • 3
    You can do a lot of stuff with existing libraries. What you shouldn't do is reinvent the wheel, mix Swing classes and Awt classes (`Canvas`) where they shouldn't be, etc. Also, lack of experience is not an excuse not to do research before you ask a question. – RealSkeptic Sep 19 '17 at 13:45
  • 2
    `setBorder( BorderFactory.createEmptyBorder() ); setOpaque( false );` – Usagi Miyamoto Sep 19 '17 at 14:08

1 Answers1

0

Try using the Graphics class. But that would be really complicated. You will implement a KeyListener to the entire container. Whenever you pressed a key, some text should be drawn to the screen. Whoops!!! the coordinates (x,y) of the text doesn't move automatically like JTextField does, you need to increment the x whenever you type. and reset it to 0 if the user hits ENTER and increment y. The value that will be added to x and y depends on Font Size.

Mr. Leeds
  • 154
  • 2
  • 11