Results 1 to 14 of 14
  1. #1
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Object Confusion

    This is a simple problem to fix I'm sure. I have a class that makes a button and formats it (doesn't do that in my example) and i want to add it to my main Frame class. Here is my code, but it doesn't work. Did i do something wrong, or do i just have a wrong idea of how the object works. Sorry, I'm brand new to java.

    Java Code:
    package testframe;
    import javax.swing.*;
    
    public class TestFrame {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(400,400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            makeButton button = new makeButton();
            button.addButton("My Button!");
            
            frame.getContentPane().add(button);   //<-- this line incorrect
        }
    }
    
    
    
    
    package testframe;
    import javax.swing.*;
    
    public class makeButton {
        
        public void addButton(String name){
        JButton button = new JButton(name);
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Java Code:
    makeButton button = new makeButton();
    Just because you call your variable "button" it doesn't make it a JButton object, it is a makeButton object.
    Java Code:
    frame.getContentPane().add(button);
    So when you try to do this ^ you are attempting to add the makeButton object to the frame and not a JButton. Only objects that extend Component can be added and your makeButton class does not extend Component.

    What you can do instead is have your addButton method (which is a bad name, createButton or generateButton would be better) return the JButton to the main method and then add that to the frame.

    By the way if you are only new to Java then you should not be touching GUI's until you have a better grasp of the basics.

  3. #3
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    I meant to say im new to GUI in Java. extends works great. Thanks

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    NOOOOOOOOOOOOOOoooooooooooooooooooooooooooo!

    Having your makeButton class extend Component is NOT the correct solution.

  5. #5
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Explain please

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Your makeButton class is not a Component. Just like a Factory is not a Box just because it makes cardboard boxes. When you ran your code did the button appear?

  7. #7
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    it did, but i couldn't format it. I figured it out. does this look better?


    Java Code:
    package testframe;
    import javax.swing.*;
    import java.awt.*;
    
    public class TestFrame {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(400,400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            makeButton button = new makeButton();
            JButton button1 = button.createButton("My Button");
            
            frame.getContentPane().add(BorderLayout.WEST, button1);
        }
    }
    
    
    
    package testframe;
    import javax.swing.*;
    
    public class makeButton {
        
        public JButton createButton(String name){
            JButton button = new JButton(name);
            return button;
        }
    }

  8. #8
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    i know my variable names aren't the best, but i was just testing. don't worry, in my main project, the names are much better. not just button and button1

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Now I'm confused. You said "extends works great". Yet the code above does not extend anything. Did you change things or did you just use incorrect terminology?

  10. #10
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    no after you said extends wasn't the correct way i changed it.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The most up to date code is fine, but it's a long route to take for such a simple task.

  12. #12
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    It gets more complex, i just made a simple to test. Thanks though!

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Code Conventions for the Java(TM) Programming Language: Contents

    Class names should start with an uppercase letter.

    db

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Also "makeButton" is more of a method name than a class name. It's not a Thing, it's something that's done.
    ButtonFactory would be better, on the assumption that you're doing a load of standard stuff for defining your buttons in there (rather than simply doing new JButton()).

Similar Threads

  1. [HELP] bit string confusion
    By aufis in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 04:42 PM
  2. Replies: 0
    Last Post: 02-09-2011, 03:22 PM
  3. object name confusion
    By mobosecomin in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 02:45 AM
  4. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 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
  •