Results 1 to 5 of 5
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

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

    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 want to be able to call the method like this myTable.setDefaultSettings().
    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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  3. #3
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    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.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    OK, it is doing what I want, I was just worried that if I'm missing something, it will fail down the road and I won't think to check there.

Similar Threads

  1. How can I extend java.net.URI - why is it final?
    By johann_p in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2009, 05:31 AM
  2. Override/Extend Eclipse Class Loading
    By arun.ranganathan in forum Eclipse
    Replies: 0
    Last Post: 07-15-2009, 10:57 AM
  3. extend a button
    By Omarero in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:57 PM
  4. using J-classes or extend them ?
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 01-01-2009, 07:58 PM
  5. How to extend Messages Component
    By Jothi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 05-20-2008, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •