Trying to create a Family tree program.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 8419 Views - Last Post: 06 September 2011 - 01:26 PM Rate Topic: -----

#1 peanot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-January 11

Trying to create a Family tree program.

Posted 28 August 2011 - 01:35 PM

I have been away for time and I forgot most of the basic things in Java.
Before I "left" I wasn't THAT good either, but still:P

Ok so here is the deal
This is not homework, but it's an assignment I'm making for school.(I created the idea myself)

Here is what I'm trying to make:
I'm trying to make a Family Tree program, the user will be able to save the tree in a database.
Here some kind of graphical explanation:
Posted Image

New Family Button:
So once the user runs the program he will have an empty screen, once he presses the NEW FAMILY button, the pop up window will appear, and there the user will be able to fill in some things.
Once the user presses OK, a tree will be created (Graphically).
Each persons window will have the button to modify and to create a child, and the children will have the same buttons.

Save tree button:
The tree will be saved in a database(Somehow....)

Load tree button:
The tree will be retrieved from the database (somehow...)

Message window
The message window will show some messages(Not really important, but more user friendly.

Since I'm a "noob" at Java/Programming I tried to keep it as simple as possible.
But of course, if anyone has some ideas/remarks, they are all welcome to shed some light.
So I guess that is about it.

I only gave the description of my work in the first post, I will start with the problem(s) in the next post(s).
That way there is a better overview in my opinion.

I guess the problem should be really easy to solve.
I'm having trouble with the content pane.

package familytree;

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainWindow {

    private Container contentPane;
    private JTextField messageField;
    private JButton newFamily, saveTree, loadTree;

    public MainWindow() {
    }

    public void createPane() {
        contentPane = new Container();
        contentPane.setSize(200, 700);
        contentPane.setLocation(600, 0);

        messageField = new JTextField();
        messageField.setEditable(false);
        messageField.setBounds(25, 550, 150, 150);
        messageField.setVisible(false);
        contentPane.add(messageField);

        newFamily = new JButton();
        newFamily.setName("New Tree");
        newFamily.setBounds(100, 100, 50, 25);
        newFamily.setVisible(false);
        contentPane.add(newFamily);

        saveTree = new JButton();
        saveTree.setName("Save");
        saveTree.setBounds(100, 225, 50, 25);
        saveTree.setVisible(true);
        contentPane.add(saveTree);

        loadTree = new JButton();
        loadTree.setName("Load");
        loadTree.setBounds(100, 350, 50, 25);
        loadTree.setVisible(true);
        contentPane.add(loadTree);
    }

    public void createFrame() {
        JFrame frame = new JFrame();
        frame.setTitle("Family Tree");
        frame.setSize(800, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.getContentPane();
        contentPane.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MainWindow run = new MainWindow();
                run.createFrame();
            }
        });
    }
}



Error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at familytree.Mainwindow.createFrame(Mainwindow.java:56)
        at familytree.MainWindow$1.run(Mainwindow.java:64)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 2 seconds)



Note that any remarks about the code are also very welcome.
Always trying to learn the "best" ways of coding.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create a Family tree program.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,771
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 01:38 PM

JDBC is pretty simple. We have a tutorial on DIC and the Oracle tutorials are pretty straight-forward.

To store the Tree-structure, I'd use a linking table:
parent-to-child
---------------
parent (PK, FK- user.user_id) | child (PK, FK- user.user_id)



Since JTree only deals with single-parented Trees, I would suggest focusing on a single person at a time, with links. A person may be married- that would be a link. A person may have children- those are links, as are the current person's parents. That's the easiest way to tackle this, unless you want to get into making a custom JComponent or doing custom painting, which gets more complex for this.

Also, why not just use your constructor rather than having makeFrame() and makePane() methods?
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 03:17 PM

Do you have to use a database? Could you just serialize a tree structure and store it in a file? It seems like hell to have to use a relational database to do this. What about an Object-Oriented database?
Was This Post Helpful? 0
  • +
  • -

#4 peanot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-January 11

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 03:42 PM

View Postblackcompe, on 28 August 2011 - 03:17 PM, said:

Do you have to use a database? Could you just serialize a tree structure and store it in a file? It seems like hell to have to use a relational database to do this. What about an Object-Oriented database?


Yup I will have to use some kind of database.
I need to store and retrieve the info somehow, I need to use a database, because I will need to explain all the steps that happen to my tutor. (So he can see if I understand it or not).
Doesn't matter what kind of db though.
Local, Internet etc.



View Postmacosxnerd101, on 28 August 2011 - 01:38 PM, said:

JDBC is pretty simple. We have a tutorial on DIC and the Oracle tutorials are pretty straight-forward.

To store the Tree-structure, I'd use a linking table:
parent-to-child
---------------
parent (PK, FK- user.user_id) | child (PK, FK- user.user_id)



Since JTree only deals with single-parented Trees, I would suggest focusing on a single person at a time, with links. A person may be married- that would be a link. A person may have children- those are links, as are the current person's parents. That's the easiest way to tackle this, unless you want to get into making a custom JComponent or doing custom painting, which gets more complex for this.

Also, why not just use your constructor rather than having makeFrame() and makePane() methods?

About the JTree: I will really look into it, it does sound easier indeed. And since it's my own project I an decide how it all looks, as long it makes sense.

About not using the constructor.
Heh, can't really tell you why I did it that way, I guess I just wanted multiple methods.
But it seems like having multiple methods for the Pane and the Frame is just giving more headaches.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 04:36 PM

Quote

Yup I will have to use some kind of database.
I need to store and retrieve the info somehow, I need to use a database, because I will need to explain all the steps that happen to my tutor. (So he can see if I understand it or not).
Doesn't matter what kind of db though.
Local, Internet etc.


You don't need a database to persist data. A file stores data just fine. If you don't like the idea of a file, you should take a look at this before making your final decision.

Quote

About the JTree: I will really look into it, it does sound easier indeed. And since it's my own project I an decide how it all looks, as long it makes sense.


How does a JTree represent a family tree? It doesn't represent relationships between two people. A JTree is a traditional hierarchy and a family tree is a offshoot.

Hierarchy (JTree)

Posted Image

Family Tree

Posted Image

A JTree is for graphical components, you need a backing data structure to store your tree.

This post has been edited by blackcompe: 28 August 2011 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,771
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 04:54 PM

More advanced JComponents like JTree and JTable have a Model and a Renderer. You would logically store the family Tree information in the Model.
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 05:02 PM

I'm aware, but I don't think it's a good model for representing a family tree because it lacks the ability to represent relationships between two parents. It's all child-parent relationships.

This post has been edited by blackcompe: 28 August 2011 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,771
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 05:11 PM

I agree. Based on your family tree image (the last one), a JTree would work for that. In general, I agree it's not the best solution. :)
Was This Post Helpful? 0
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 05:30 PM

Quote

I agree. Based on your family tree image (the last one), a JTree would work for that. In general, I agree it's not the best solution.


Your right, but the family tree wouldn't look how peanot wants it to look (in the image he posted).

This post has been edited by blackcompe: 28 August 2011 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 peanot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-January 11

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 07:55 PM

View Postblackcompe, on 28 August 2011 - 04:36 PM, said:

Quote

Yup I will have to use some kind of database.
I need to store and retrieve the info somehow, I need to use a database, because I will need to explain all the steps that happen to my tutor. (So he can see if I understand it or not).
Doesn't matter what kind of db though.
Local, Internet etc.


You don't need a database to persist data. A file stores data just fine. If you don't like the idea of a file, you should take a look at this before making your final decision.

Quote

About the JTree: I will really look into it, it does sound easier indeed. And since it's my own project I an decide how it all looks, as long it makes sense.


How does a JTree represent a family tree? It doesn't represent relationships between two people. A JTree is a traditional hierarchy and a family tree is a offshoot.

Hierarchy (JTree)

Posted Image

Family Tree

Posted Image

A JTree is for graphical components, you need a backing data structure to store your tree.


I will look at the OODB later, it's 5 am so won't be able to do that now.
My teachers are quite persistent in using a db. But since the school hasn't begun yet I can't ask them if I'm allowed to use the OODB system.
But I also guess, that the storage system is a problem for later, I should focus more on the program itself for now.

About your second picture/image.
Well it's actually about the same as a JTree but just flipped, so maybe you posted a bad example there.
Would it be an idea to have like two JTree's next to each other?
Just pitching up some suggestions, but then I would have to find a way for the reference.
Was This Post Helpful? 0
  • +
  • -

#11 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 08:00 PM

Quote

About your second picture/image.
Well it's actually about the same as a JTree but just flipped, so maybe you posted a bad example there.
Would it be an idea to have like two JTree's next to each other?
Just pitching up some suggestions, but then I would have to find a way for the reference.


Haha. That's exactly what I noticed.
Was This Post Helpful? 0
  • +
  • -

#12 peanot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-January 11

Re: Trying to create a Family tree program.

Posted 30 August 2011 - 12:23 PM

Ok I have adjusted my code a little bit. But still not using the constructor, else my Main won't work.

package familytree;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainWindow {
    private JFrame frame;
    private JPanel buttonPanel, messagePanel;
    //private Container contentPane;
    private JTextField messageField;
    private JButton newFamily, saveTree, loadTree;

    public MainWindow() {}

    public void createFrame(){
        frame = new JFrame("Family Tree");

        // Creating the panels for the buttons and the message field.
        buttonPanel = new JPanel();
        buttonPanel.setBounds(700, 0, 200, 700);

        messagePanel = new JPanel();

        // Creating the button that will create a new instance of the Tree.
        newFamily = new JButton();
        newFamily.setName("New Tree");
        newFamily.setBounds(100, 100, 50, 25);
        newFamily.setVisible(true);
        buttonPanel.add(newFamily);

        // Creating the button that will eventually save the tree to the DB.
        saveTree = new JButton();
        saveTree.setName("Save");
        saveTree.setBounds(100, 250, 50, 25);
        saveTree.setVisible(true);
        buttonPanel.add(saveTree);

        // Creating the button that will eventually load the tree from the DB.
        loadTree = new JButton();
        loadTree.setName("Load");
        loadTree.setBounds(100, 400, 50, 25);
        loadTree.setVisible(true);
        buttonPanel.add(loadTree);

        // Creating the message field.
        messageField = new JTextField();
        messageField.setEditable(false);
        messageField.setBounds(25, 550, 150, 100);
        messageField.setVisible(true);
        buttonPanel.add(messageField);

        // Building the frame and adding the button panel to it.
        frame.setSize(900, 900);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(buttonPanel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MainWindow run = new MainWindow();
                run.createFrame();
            }
        });
    }
}



Not getting any errors, but it doesn't look the way it's supposed to.
Image
Posted Image

P.S. About the tree system... I will return to that later, I want to do it all step by step, if I start worrying about the tree system now...

BTW what is the main difference between an OODB and a "normal" DB? (Still need to read the article though.)
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 30 August 2011 - 01:30 PM

In a OODB you can store the objects in the database directly without having to map them to a set of fields. My suggestion of using a OODB was really only if you were going to use some sort of tree structure to represent the family tree. Now that I see how flipping the tree is essentially the same thing, I would use a binary tree as my model. You could store the whole tree in the database. The link I posted on OODB shows a few examples in Java.

There's a million and one ways to do this.

This post has been edited by blackcompe: 30 August 2011 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#14 peanot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-January 11

Re: Trying to create a Family tree program.

Posted 30 August 2011 - 03:12 PM

View Postblackcompe, on 30 August 2011 - 01:30 PM, said:

In a OODB you can store the objects in the database directly without having to map them to a set of fields. My suggestion of using a OODB was really only if you were going to use some sort of tree structure to represent the family tree. Now that I see how flipping the tree is essentially the same thing, I would use a binary tree as my model. You could store the whole tree in the database. The link I posted on OODB shows a few examples in Java.

There's a million and one ways to do this.


Thanks I will look into it, even though the Binary Tree isn't really what I had in mind. But I guess it should be a lot easier then building a custom tree with Swing.



Does anyone has a solution to the panel not being shown correctly?(See image previous post).
Was This Post Helpful? 0
  • +
  • -

#15 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 30 August 2011 - 04:16 PM

I went ahead and created my own little project. Here's some code from it that will show you how to layout the widgets.

Posted Image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MainWindow {

	public static void main( String[] args ) {
		Window win = new Window();
		win.setSize( 750, 750 );
		win.setVisible( true );
		win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
}

class Window extends JFrame {

	static int			winX			= 750, winY = 750;

	ControlPanel			controlPanel	= new ControlPanel();
	static EditorPanel	editorPanel		= new EditorPanel();

	Window() {
		super( "FamilyTree" );
		setLayout( new FlowLayout() );
		add( editorPanel );
		add( controlPanel );
	}
}

class ControlPanel extends JPanel {

	ActionPanel	actionPanel		= new ActionPanel();
	MessagePanel	messagePanel	= new MessagePanel();

	ControlPanel() {
		super();
		setPreferredSize( new Dimension( Appwindow.winX / 4, Appwindow.winY / 2 ) );
		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
		add( actionPanel );
		add( messagePanel );
		setBorder( BorderFactory.createLineBorder( Color.black ) );
	}
}

class ActionPanel extends JPanel {

	JButton		newFamilyBtn	= new JButton( "New Family" );
	JButton		linkFamilyBtn	= new JButton( "Link Families" );
	JButton		dbLoadBtn		= new JButton( "Load Family" );
	JButton		dbSaveBtn		= new JButton( "Save Family" );
	JButton		clearBtn		= new JButton( "Clear" );
	TreeStore	treeStore		= new TreeStore();

	ActionPanel() {
		super();
		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
		add( newFamilyBtn );
		add( linkFamilyBtn );
		add( dbLoadBtn );
		add( dbSaveBtn );
		add( clearBtn );
	}

}

class MessagePanel extends JPanel {

	JTextArea	messageArea	= new JTextArea( 15, 15 );

	MessagePanel() {
		super();
		add( messageArea );
	}
}

class EditorPanel extends JPanel {

	EditorPanel() {
		super();
		setPreferredSize( new Dimension( ( int ) ( Appwindow.winX / 1.5 ), Appwindow.winY - 50 ) );
		setBorder( BorderFactory.createLineBorder( Color.black ) );
		setBackground( Color.WHITE );
	}

	public void paintComponent( Graphics g ) {
		super.paintComponent( g );

	}
}


This post has been edited by blackcompe: 30 August 2011 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2