Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Which class to extend? (http://www.programmingforums.org/showthread.php?t=7291)

titaniumdecoy Nov 29th, 2005 11:10 PM

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.

lectricpharaoh Nov 30th, 2005 2:58 AM

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

groovicus Nov 30th, 2005 7:33 AM

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. :)


All times are GMT -5. The time now is 3:29 PM.

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