Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  manually drawing Swing components  (Read 2263 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Devvie

« Posted 2006-04-16 13:42:48 »

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.
Offline 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.
Offline Kova

Senior Devvie

« 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline Kova

Senior Devvie

« 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 Smiley
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.
Offline Kova

Senior Devvie

« 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  Smiley Wink
Offline 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).
Offline 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: and look out for Luke's post.  Smiley


Offline Kova

Senior Devvie

« 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.
Offline 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.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Devvie

« Reply #10 - Posted 2006-04-26 12:43:38 »

guys at directed me to CardLayout, works beautiflly, you don't need to add / remove components from coontainer, just set visibility and that's it Smiley
Offline Mr_Light

Senior Devvie

Medals: 1


« 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.
Offline Kova

Senior Devvie

« 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 Smiley
Offline 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?


Offline swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

« 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.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (11 views)
2015-07-29 04:35:27

Riven (33 views)
2015-07-27 16:38:00

Riven (16 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (10 views)
2015-07-27 12:23:39

BurntPizza (28 views)
2015-07-25 00:14:37

BurntPizza (39 views)
2015-07-24 22:06:39

BurntPizza (22 views)
2015-07-24 06:06:53

NoxInc (26 views)
2015-07-22 22:16:53

NoxInc (17 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!