Results 1 to 9 of 9
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default [SWING]Adding JButton(class1) to JPanel(class2)

    Hello, I have a problem. I have one class with the JButtons and one class with the JPanel. I want to add the JButtons from one class to the JPanel in another class. How do I do this?

    I can get it to work if the buttons are in the JPanel class this.add(button). But how do I do when they are in two classes? I've played around with objects and peer-objects but no success because I'm so new to this.


    I'm gonna have an ActionListener to the the buttons if that matters.
    Any suggestions?

    Thanks!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Add a method in the class that has you JButtons say call it getButton() that will return a JButton, create an instance of the Button class in the Panel class and call the button class' getButton() method.

  3. #3
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    a part of Button class:
    PHP Code:
    public JButton getNrButton()
    	{
    
    		_button1 = new JButton("1");
    		
    	 return _button1;
    	
    	}
    a part of Panel class:
    PHP Code:
    public BackgroundPanel()
    	{
    		super(new BorderLayout());
    		new BorderLayout();
    		ControlPanel cApp = new ControlPanel();
    		nrButton nrApp = new nrButton();
    		this.add(cApp, BorderLayout.CENTER);
    		result();
    		_button = _buttons.getNrButton(); 
    		this.add(_text);
    		this.add(_panel, BorderLayout.NORTH);
    		this.add(_button); 
    		
    	}
    i get error on "_button = _buttons.getNrButton(); "

    Error:
    PHP Code:
    Exception in thread "main" java.lang.NullPointerException
    	at BackgroundPanel.<init>(BackgroundPanel.java:20)
    What seems to be the problem?

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    NullPointerException... any uninitialised objects being accessed?

  5. #5
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Nope, dont think so?

    It gives error on _button = _buttons.getNrButton(); <- Here i get the JButton _button1 from the class with my buttons, and make it an object in the panel class right? But it seems like my panel class doesnt know that it is an JButton?

    What exactly does NullPointerException mean?

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    It means an object is 'pointing' to absolutely nothing.
    Java Code:
    MyClass mc=null;
    mc.toString(); // null pointer exception
    
    
    MyClass[] mcs = new MyClass[100];
    MyClass mc1=mcs[0]; // null pointer mcs[0] was never initialised;

  7. #7
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    solved it!

    Mixed up a instance of a class with a peer-object... stupid...


    Is there any good way of getting the all the 10 buttons i have to the other class or do i have to make 10 different get methods?

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by equal View Post
    solved it!

    Mixed up a instance of a class with a peer-object... stupid...


    Is there any good way of getting the all the 10 buttons i have to the other class or do i have to make 10 different get methods?
    have your getButton() method return an array of ten JButtons

  9. #9
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Aah good! Gonna try to setup several different panels now.

    Thanks for all the help! :)

Similar Threads

  1. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM
  2. adding JList and JButton to a TabbedPane
    By hariza in forum AWT / Swing
    Replies: 13
    Last Post: 10-06-2010, 01:46 AM
  3. Adding JButton to a JTable
    By ting.at.net@hotmail.com in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2009, 03:37 AM
  4. adding Image to JButton
    By mayhewj7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 03:39 AM
  5. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM

Tags for this Thread

Posting Permissions

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