Results 1 to 5 of 5
Thread: Trying to extend class
- 10-29-2009, 05:59 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
Trying to extend class
I've created a class "MyJTable extends JTable" and I'm trying to create a method which sets the default settings I want for most of my JTables (or my MyJTables). I can't seem to find out how to to this though, can anyone tell me what I'm doing wrong?
I want to be able to call the method like this myTable.setDefaultSettings().Java Code:public class MyJTable extends JTable { public void setDefaultTableSettings() { getTableHeader().setBackground(new Color(39,161,212)); setAutoResizeMode(JTable.AUTO_RESIZE_OFF); setDragEnabled(false); getTableHeader().setReorderingAllowed(false); setAutoCreateRowSorter(true); } }
I tried making the method staic but then it says I cannot call the methods within from a static context.
EDIT: I realize I am a complete idiot now, I wasn't aware of how to find the variables from the super class. I managed to figure out three of them. Is this correct now?
Java Code:public void setDefaultTableSettings() { tableHeader.setBackground(new Color(39,161,212)); autoResizeMode = AUTO_RESIZE_OFF; setDragEnabled(false); tableHeader.setReorderingAllowed(false); setAutoCreateRowSorter(true); }Last edited by ribbs2521; 10-29-2009 at 06:15 PM.
- 10-29-2009, 06:15 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You would need an instance of MyJTable to be able to call a non static method in the MyJTable class. You should revise these basics before trying to extend JTable.
- 10-29-2009, 06:17 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
I have an instance, it was in my Main class and it kept giving me an error. Now that I have changed some of the code (See edit above) it seems to work with no errors but I'm still not sure it's correct.
- 10-29-2009, 06:26 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
It's always good practice to go through setters when setting values inherited from super classes. Some of the reasons are
1.) The property you want to set may be private and the setter is really your only option.
2.) The setter also updates other properties to make the state of the class consistent over the update.
Whether your code is correct or not depends on whether it is achieving what you wanted or not.
- 10-29-2009, 06:28 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
How can I extend java.net.URI - why is it final?
By johann_p in forum Advanced JavaReplies: 1Last Post: 08-21-2009, 05:31 AM -
Override/Extend Eclipse Class Loading
By arun.ranganathan in forum EclipseReplies: 0Last Post: 07-15-2009, 10:57 AM -
extend a button
By Omarero in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:57 PM -
using J-classes or extend them ?
By itaipee in forum AWT / SwingReplies: 1Last Post: 01-01-2009, 07:58 PM -
How to extend Messages Component
By Jothi in forum JavaServer Faces (JSF)Replies: 0Last Post: 05-20-2008, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks