manually drawing Swing components

Hello.
For my game menu I use JFrame with overriden paint(), so for different position in game menu I draw different pictures. Now it’s time I implement some fields where user can enter data and then start the game or whatever.
So for example I need user to enter IP adress of server before he clicks connect image, so how do I draw JTextField on the screen? All other drawn things are .png images. Thank you.

It’s a normal swing component, rendered like all other swing components. Read the tutorials for the basics.

what tutorials? … couldn’t find any

All I know about swing is that I can show it if I add it to some JContainer and then show it by seting it visible. Here’s a totally different thing. If I add it, not only it changes the colors since it has it’s own coloring, but also messes my listeners completely. If someone could give me some pointers what to search, what kind of tutorial or something.

Google ‘how to use text fields’.

Besides, the text field cannot change the color of anything that is not part of itself.

It cannot remove listeners from other components. However it CAN - and this is a fair moment of frustration - obtain focus from other components, rendering their keylisteners inoperative.

Use jTextField.setRequestFocusEnabled(false) to prevent them from continually stealing focus. However your key listeners will still be inoperative for obvious reasons while the user types into the fields.

I only get crappy tutorials for basic stuff I already know. Well I’ll dug some more detailed tutorial, there must be one out there…

With color changing I meant his background turns to white by default when you start typing… and white is not the color my pics are :slight_smile:
It’s all ok about keyboard focus, wouldn’t make any sense if it dosen’t acquire it… but it steals my mouse listener also… after I click on him, further clicks on JPanel (where I draw my pics) aren’t registered.

OK after many reading of unreadable stuff and trying to mix something I came up with this.
I have a JFrame that has my JPanel where I overriden paintComponent()… I add textfields to JPanel and make them visible when needed. Woks almost fine except it dosen’t show preset text I made with setText(), but if I write to them text shows fine, don’t know why is that. Since I need to see the background I make don’t make them opaque. Only their border shows which is perfect but this time not only preset text dosen’t show up but also I can’t write any text (won’t show up or be remembered). Help please, I’m so close to cracking this one :slight_smile: :wink:

That’s peculiar. Perhaps you should post some code.

Btw, I find the tutorials quite readable and have used many of them myself (and still do).

If the content pane of the JFrame is/contains your custom component that does the painting then you can call paintComponents(Graphics) from your render method to paint all components that have been added to your custom component. Just make sure they have setVisible(true) called on them and that they have their size set.

To get input from a JTextField, I just put the text field in a JInternalFrame and add it to my custom component and call paintComponents(…). There’s an example of this in the below thread that you should read.

Also take a look at this if you have thread problems: http://www.java-gaming.org/forums/index.php?topic=13211.15 and look out for Luke’s post. :slight_smile:

Keith

after triming code to few basics it worked. Don’t know what bothered him in first place. I rebuild everything. This time I did custom painting on my JPanel instead of JFrame and I add the panel to JFrame’s container. Since my animation thread is also a JPanel I wanted to add it also to JFrame and swamp visibility of those 2 panels when needed, but I’m having troubles with that. I’ll try to solve it, but if anyone has any suggestions or comments they are welcome.

JFrames are heavyweight so you don’t want to over-ride their paint methods. Doing it the content-pane way probably fixed your problem.

To stop components from painting just call setVisible(false), remove them from their ‘mother’ container (mother.remove(child)) and call revalidate() on the mother. Works for me.

Keith

guys at comp.lang.java.gui directed me to CardLayout, works beautiflly, you don’t need to add / remove components from coontainer, just set visibility and that’s it :slight_smile:

didn’t swing and the cardlayout have issues or am I mixing up stuff or was that fixed over time?

I’ve googled it, lots of bugs for cardlayout in 1.4, like 10 pages in Google… but almost everyone of them are fixed. Stoped looking after page 3, after quick pass through I think there are 2 bugs scheaduled for fixing, others are just duplicates or related. My conclusion is that it’s safe to use CardLayout :slight_smile:

Good idea, hadn’t thought of that… but what if you want to display more than one component?

Doesn’t card layout just flip between single components?

Keith

Use CardLayout to flip between panels. Each panel can have all sorts of stuff on it.