Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2005, 11:10 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Which class to extend?

I am working on an application similar to a tic-tac-toe game in Java. It will draw a number of squares in a window which will contain a number. The user should be able to click any one of these squares to select it and change its value by pressing a number key.

I want to create each of these squares as a separate object (that draws itself) so that I can easily modify the number they contain--for example, square1.setNumber(1). I don't have much experience working with GUI components in Java. What I am wondering is what class would be the best choice to extend as a square object--JPanel, JButton, JComponent, etc. Any ideas? Thanks...


Also, objects I add to the content pane often don't display until I resize the window. Has anyone else had this problem? It has been happening ever since I started working with the GUI.

Last edited by titaniumdecoy; Nov 29th, 2005 at 11:37 PM.
titaniumdecoy is offline   Reply With Quote
Old Nov 30th, 2005, 2:58 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,051
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by titaniumdecoy
I am working on an application similar to a tic-tac-toe game in Java. It will draw a number of squares in a window which will contain a number. The user should be able to click any one of these squares to select it and change its value by pressing a number key.

I want to create each of these squares as a separate object (that draws itself) so that I can easily modify the number they contain--for example, square1.setNumber(1). I don't have much experience working with GUI components in Java. What I am wondering is what class would be the best choice to extend as a square object--JPanel, JButton, JComponent, etc. Any ideas? Thanks...


Also, objects I add to the content pane often don't display until I resize the window. Has anyone else had this problem? It has been happening ever since I started working with the GUI.
JComponent is a base for most (all?) of the Swing components, like JButton, JPanel, etc. It's probably not useful to extend it yourself for this job.

Personally, I'd use one of the following:
    • JButton- if you want it to look like a button, obviously, this is your pick.
    • JLabel- simple and effective. You can detect the clicks with a MouseListener.
    • JPanel- similar to using JLabel, except you don't have to draw the number as text, meaning you could have custom images for the numbers.
As for components not displaying, generally what you want to do is create all your subcomponents (ie, in your JFrame's constructor) before you show the window. This means instantiating them, adding them to the container that holds them, and making them visible (in that order). Then, in whatever class's main method gets things going, you instantiate your main window, and then make it visible with setVisible(true).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 30th, 2005, 7:33 AM   #3
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
If you can't see them until you resize the window, then there are any number of possible issues. The objects might be too big to be drawn within the window, so they don't show up until you make the window big enough, you may have bits of code interfering with each other, you have not yet discovered pack()..

It is also hard o say what classes you should extend, because we don't know what it is you are doing. When I do GUI, I often extend JFrame or JPanel, depending on what it is I am doing. But it is also a good way to introduce more problems.
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC