• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning a panel from an object's method | adding components

 
Jon Hales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*New to java.

I'm trying to return a panel from an object's method and add it to my frame.
OR
Trying to pass a created panel into a method of an object and add components to it.

Should be simple, but I'm a bit slow I guess.

-- I HAVE been able to pass a panel into the instantiation of an object, and add components to that panel.
-- I HAVEN'T been able to create an object, then pass a panel into a method of that object and add components to the panel.
-- I HAVEN'T been able to define a panel creation with the instantiation of an object and set a panel = object.panel

(I get a lot of NullPointerExceptions when I try to add components to the panel.)
 
Jon Hales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, looks like the 'void' in my object instantiation method was the culprit.

Though I was returning a JPane from my .getPane method the nullPointer was somehow related to the void on the actual object was killing my ability there.

-- If anyone has simple insight into why this is the case. (I'm pretty new to OO and don't understand fully why this was an issue. If I have the void in the instantiation method, does that mean that none of my methods can return? Why was this the reason my .getPane method coudn't return the proper JPane?)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I'm sure if you show us some (working or not) code, we could talk about it. But in my experience there's no end to the creativity new programmers can display in writing programs that don't work -- thus it's hard for me to guess precisely where you've gone wrong.
 
Jon Hales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a shortened version of my Main Class and my PersonObject Class
I don't understand why the 'void' in the PersonObject Class made it impossible to set the boyPanel (in Main) = to boy.boyPanel in my object instance. Remove the void, and it works.

BTW, thanks for the greeting!

-Jon


// MAIN APP

package DTG_Manager;

import javax.swing.*;
import java.awt.*;


public class DTG_Main {

JFrame mainFrame; // This is the Application Frame

JPanel boyPanel; //The boyPanel


// Instantiated Objects which hold all current values to fill panels
PersonObject boy;

public DTG_Main()
{
// Setting up the JFrame for the App
mainFrame = new JFrame("Title");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new GridLayout(1,3));
// mainFrame.setSize(new Dimension(800,600)); // This doesn't work

//Always objects to fill panels
boy = new PersonObject();


// Create the Panels
boyPanel = new JPanel();
boyPanel.setLayout(new BoxLayout(boyPanel,BoxLayout.PAGE_AXIS));


// Add Components to the Panels
boyPanel = boy.boyPanel;
/* Also used boyPanel = boy.getPanel(boyPanel) method
* which allowed me to pass a panel into the method of
* my object and return a panel. Have since removed this method
*/



//Add The Panel to mainFrame
mainFrame.getContentPane().add(boyPanel,"1");



//Display the Frame
mainFrame.pack();
mainFrame.setVisible(true);
}

public static void createAndShowGUI()
{

JFrame.setDefaultLookAndFeelDecorated(true);

DTG_Main mainAPP = new DTG_Main();
}

public static void main(String[] args)
{
createAndShowGUI();

}

}




// PersonObject (separate class)

package DTG_Manager;

import javax.swing.*;
import java.awt.event.*;

public class PersonObject{

// Variables to hold TextField Informationn (Maybe not ever even needed.)
private String boysName; // Name

//Panel JTextFields
private JTextField nameField;

// JTextField Lables
private JLabel nameFieldL;

// Create Panel for Object
public JPanel boyPanel;

public void PersonObject()
{
// Instantiate all the fields
nameField = new JTextField("",15);
nameFieldL = new JLabel("Name | (Last, First, Middle)",SwingConstants.TRAILING);


//Instantiate Panel
boyPanel = new JPanel();
boyPanel.setLayout(new BoxLayout(boyPanel,BoxLayout.PAGE_AXIS));

//Add components to panel
boyPanel.add(nameField); boyPanel.add(nameFieldL);



}


}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK, I can explain this. A lot of people have made this mistake.

You clearly know what a constructor is -- it's a special kind of method that's called when an object is created; it generally initializes member variables, etc. Constructors are special in that they don't have a return type, not even void. If you write a method that has the same name as a class but give it a return type, then it's not a constructor -- it's just a plain old method that just happens to have the same name as the class its in. So with the "void", your "PersonClass()" method is not really a constructor, so it's never called. That's why when you create a PersonClass, all of its fields are null.

Removing the void turns this method into a constructor, and this it gets called when you create an object.
 
Jon Hales
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW!
Thanks for that insight, and for taking the time to drop a line.
**invaluable**

Thank you! -Jon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!