Kova
|
 |
«
Posted
2006-04-16 13:42:48 » |
|
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.
|
|
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #1 - Posted
2006-04-16 18:26:13 » |
|
It's a normal swing component, rendered like all other swing components. Read the tutorials for the basics.
|
|
|
|
Kova
|
 |
«
Reply #2 - Posted
2006-04-17 00:30:39 » |
|
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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #3 - Posted
2006-04-17 13:51:34 » |
|
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.
|
|
|
|
Kova
|
 |
«
Reply #4 - Posted
2006-04-17 23:54:04 » |
|
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  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.
|
|
|
|
Kova
|
 |
«
Reply #5 - Posted
2006-04-24 17:53:31 » |
|
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 
|
|
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #6 - Posted
2006-04-25 03:42:51 » |
|
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).
|
|
|
|
CommanderKeith
|
 |
«
Reply #7 - Posted
2006-04-25 05:31:30 » |
|
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.  Keith
|
|
|
|
Kova
|
 |
«
Reply #8 - Posted
2006-04-25 18:28:16 » |
|
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.
|
|
|
|
CommanderKeith
|
 |
«
Reply #9 - Posted
2006-04-26 02:43:06 » |
|
This time I did custom painting on my JPanel instead of JFrame and I add the panel to JFrame's container. 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
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Kova
|
 |
«
Reply #10 - Posted
2006-04-26 12:43:38 » |
|
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 
|
|
|
|
Mr_Light
|
 |
«
Reply #11 - Posted
2006-04-26 22:13:40 » |
|
didn't swing and the cardlayout have issues or am I mixing up stuff or was that fixed over time?
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
Kova
|
 |
«
Reply #12 - Posted
2006-04-27 09:53:26 » |
|
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 
|
|
|
|
CommanderKeith
|
 |
«
Reply #13 - Posted
2006-04-27 13:29:57 » |
|
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
|
|
|
|
swpalmer
|
 |
«
Reply #14 - Posted
2006-04-27 22:13:26 » |
|
Use CardLayout to flip between panels. Each panel can have all sorts of stuff on it.
|
|
|
|
|