Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 1766 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





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

Junior Member




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 Member





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




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 Member





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





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




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: http://www.java-gaming.org/forums/index.php?topic=13211.15 and look out for Luke's post.  Smiley

Keith

Offline Kova

Senior Member





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

Keith

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

Senior Member





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

Senior Member


Medals: 1


shiny.


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





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

Keith

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!