Write a Java Program That Will Accept Input and Display a Person's Family Tree

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 23
  • Joined: 17-January eleven

Trying to create a Family tree plan.

Posted 28 August 2011 - 01:35 PM

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

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

Here is what I'one thousand trying to make:
I'm trying to make a Family Tree plan, the user volition be able to salvage the tree in a database.
Here some kind of graphical caption:
Posted Image

New Family Button:
So once the user runs the programme he will have an empty screen, in one case he presses the NEW FAMILY button, the pop upward window will announced, and at that place the user will exist able to fill in some things.
One time 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 volition prove some messages(Not really important, only more user friendly.

Since I'one thousand a "noob" at Java/Programming I tried to go on it as simple every bit possible.
Just of course, if anyone has some ideas/remarks, they are all welcome to shed some light.
So I estimate that is nigh it.

I only gave the clarification of my piece of work in the first mail service, I will get-go with the trouble(southward) in the next post(s).
That mode there is a better overview in my opinion.

I judge the problem should exist really easy to solve.
I'm having problem with the content pane.

packet 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(simulated);         contentPane.add(newFamily);          saveTree = new JButton();         saveTree.setName("Save");         saveTree.setBounds(100, 225, fifty, 25);         saveTree.setVisible(true);         contentPane.add(saveTree);          loadTree = new JButton();         loadTree.setName("Load");         loadTree.setBounds(100, 350, 50, 25);         loadTree.setVisible(true);         contentPane.add together(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 master(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 coffee.awt.EventQueue.dispatchEvent(EventQueue.java:597)         at coffee.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)         at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.coffee:184)         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.coffee:169)         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.coffee:161)         at java.awt.EventDispatchThread.run(EventDispatchThread.coffee:122) BUILD SUCCESSFUL (full time: ii seconds)            

Note that any remarks about the code are as well very welcome.
Ever trying to learn the "all-time" ways of coding.


Is This A Expert Question/Topic? 0

  • +

#ii macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 01:38 PM

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

To store the Tree-structure, I'd employ a linking tabular array:

parent-to-child --------------- parent (PK, FK- user.user_id) | child (PK, FK- user.user_id)            

Since JTree but deals with single-parented Trees, I would suggest focusing on a unmarried person at a fourth dimension, with links. A person may exist married- that would exist a link. A person may have children- those are links, as are the electric 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 but utilise your constructor rather than having makeFrame() and makePane() methods?

#3 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: two,547
  • Joined: 05-May 05

Re: Trying to create a Family tree programme.

Posted 28 August 2011 - 03:17 PM

Do y'all have to utilize a database? Could you lot but serialize a tree construction and store it in a file? It seems like hell to have to apply a relational database to practise this. What about an Object-Oriented database?

#4 peanot User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 23
  • Joined: 17-January eleven

Re: Trying to create a Family unit tree program.

Posted 28 Baronial 2011 - 03:42 PM

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

Practice you have to use a database? Could you just serialize a tree structure and store information technology in a file? It seems like hell to take to use a relational database to do this. What nigh an Object-Oriented database?

Yup I volition accept to use some kind of database.
I need to shop and retrieve the info somehow, I need to use a database, because I volition demand to explain all the steps that happen to my tutor. (Then he can see if I empathise 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. Nosotros have a tutorial on DIC and the Oracle tutorials are pretty direct-forward.

To store the Tree-structure, I'd utilise a linking tabular array:

parent-to-child --------------- parent (PK, FK- user.user_id) | kid (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, every bit are the electric current person's parents. That's the easiest manner to tackle this, unless you want to get into making a custom JComponent or doing custom painting, which gets more circuitous for this.

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

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

About non using the constructor.
Heh, can't really tell you lot why I did it that way, I guess I simply wanted multiple methods.
Merely it seems like having multiple methods for the Pane and the Frame is only giving more than headaches.

#5 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Trying to create a Family unit tree program.

Posted 28 August 2011 - 04:36 PM

Quote

Yup I will have to use some kind of database.
I need to shop and retrieve the info somehow, I demand to use a database, because I will need to explain all the steps that happen to my tutor. (So he tin see if I understand information technology 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 but fine. If you don't like the idea of a file, y'all should take a look at this before making your final decision.

Quote

Nearly the JTree: I will really look into information technology, it does audio 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 ii people. A JTree is a traditional hierarchy and a family tree is a offshoot.

Hierarchy (JTree)

Posted Image

Family unit Tree

Posted Image

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

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

#6 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 Baronial 2011 - 04:54 PM

More avant-garde JComponents like JTree and JTable have a Model and a Renderer. Yous would logically store the family Tree data in the Model.

#7 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: ii,547
  • Joined: 05-May 05

Re: Trying to create a Family unit 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 unit tree because information technology lacks the ability to represent relationships betwixt ii parents. It'due south all kid-parent relationships.

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

#eight macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Trying to create a Family tree program.

Posted 28 August 2011 - 05:eleven PM

I agree. Based on your family tree image (the concluding 1), a JTree would work for that. In general, I agree information technology's non the best solution. :)

#ix blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Trying to create a Family tree program.

Posted 28 Baronial 2011 - 05:30 PM

Quote

I agree. Based on your family tree paradigm (the terminal one), a JTree would piece of work for that. In general, I concord it's not the all-time solution.

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

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

#x peanot 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 shop and retrieve the info somehow, I need to apply a database, considering I will need to explicate all the steps that happen to my tutor. (So he tin can see if I empathize it or non).
Doesn't thing what kind of db though.
Local, Net etc.

You don't demand a database to persist data. A file stores information only fine. If you don't like the idea of a file, yous should have a look at this before making your final decision.

Quote

About the JTree: I will actually expect into information technology, information technology does audio easier indeed. And since it's my own project I an determine how information technology all looks, as long information technology makes sense.

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

Hierarchy (JTree)

Posted Image

Family unit Tree

Posted Image

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

I volition await at the OODB after, it's five 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 tin't ask them if I'm allowed to use the OODB organization.
But I likewise approximate, that the storage system is a problem for afterward, I should focus more than on the program itself for now.

Virtually your second pic/paradigm.
Well it's actually most the same as a JTree just just flipped, then peradventure y'all posted a bad case there.
Would information technology be an idea to take like ii JTree's next to each other?
Simply pitching upwardly some suggestions, simply and so I would have to discover a way for the reference.

#xi blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: two,547
  • Joined: 05-May 05

Re: Trying to create a Family unit tree program.

Posted 28 August 2011 - 08:00 PM

Quote

Most your second picture/image.
Well it'southward actually about the same equally a JTree but just flipped, so maybe you posted a bad example in that location.
Would it be an thought to have like two JTree's side by side to each other?
Simply pitching up some suggestions, but then I would have to find a fashion for the reference.

Haha. That'southward exactly what I noticed.

#12 peanot User is offline

  • New D.I.C Head

Reputation: 0

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

Re: Trying to create a Family unit tree program.

Posted 30 August 2011 - 12:23 PM

Ok I have adjusted my lawmaking a picayune bit. But still non 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 {     individual JFrame frame;     individual JPanel buttonPanel, messagePanel;     //private Container contentPane;     private JTextField messageField;     private JButton newFamily, saveTree, loadTree;      public MainWindow() {}      public void createFrame(){         frame = new JFrame("Family unit 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 push button that will create a new case of the Tree.         newFamily = new JButton();         newFamily.setName("New Tree");         newFamily.setBounds(100, 100, 50, 25);         newFamily.setVisible(truthful);         buttonPanel.add(newFamily);          // Creating the push that will eventually salve the tree to the DB.         saveTree = new JButton();         saveTree.setName("Save");         saveTree.setBounds(100, 250, 50, 25);         saveTree.setVisible(true);         buttonPanel.add together(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(truthful);         buttonPanel.add(messageField);          // Building the frame and adding the push panel to it.         frame.setSize(900, 900);         frame.setResizable(false);         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         frame.setLocationRelativeTo(zippo);         frame.add together(buttonPanel);         frame.setVisible(truthful);     }      public static void principal(String[] args) {         javax.swing.SwingUtilities.invokeLater(new Runnable() {              public void run() {                 MainWindow run = new MainWindow();                 run.createFrame();             }         });     } }            

Non getting any errors, but information technology doesn't look the mode information technology's supposed to.
Epitome
Posted Image

P.S. About the tree system... I will return to that subsequently, I want to do it all step by step, if I start worrying nigh the tree system at present...

BTW what is the main deviation between an OODB and a "normal" DB? (Withal need to read the commodity though.)

#13 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: two,547
  • Joined: 05-May 05

Re: Trying to create a Family tree plan.

Posted thirty Baronial 2011 - 01:xxx 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 just if you were going to utilise 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. Y'all could store the whole tree in the database. The link I posted on OODB shows a few examples in Coffee.

In that location'due south a million and 1 ways to do this.

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

#14 peanot 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 programme.

Posted 30 August 2011 - 03:12 PM

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

In a OODB yous tin store the objects in the database straight without having to map them to a ready of fields. My suggestion of using a OODB was actually only if you were going to utilise 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 employ a binary tree equally my model. You could store the whole tree in the database. The link I posted on OODB shows a few examples in Java.

There'south a million and i means to do this.

Cheers I volition look into it, even though the Binary Tree isn't really what I had in heed. Only I guess it should be a lot easier and so building a custom tree with Swing.

Does anyone has a solution to the panel non being shown correctly?(Encounter image previous post).

#15 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Trying to create a Family tree plan.

Posted 30 August 2011 - 04:16 PM

I went ahead and created my own little project. Here'south some code from information technology that will evidence y'all how to layout the widgets.

Posted Image

import coffee.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 together( 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 / two ) ); 		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) ); 		add( actionPanel ); 		add together( messagePanel ); 		setBorder( BorderFactory.createLineBorder( Colour.blackness ) ); 	} }  class ActionPanel extends JPanel {  	JButton		newFamilyBtn	= new JButton( "New Family unit" ); 	JButton		linkFamilyBtn	= new JButton( "Link Families" ); 	JButton		dbLoadBtn		= new JButton( "Load Family unit" ); 	JButton		dbSaveBtn		= new JButton( "Save Family" ); 	JButton		clearBtn		= new JButton( "Articulate" ); 	TreeStore	treeStore		= new TreeStore();  	ActionPanel() { 		super(); 		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) ); 		add( newFamilyBtn ); 		add( linkFamilyBtn ); 		add( dbLoadBtn ); 		add together( dbSaveBtn ); 		add( clearBtn ); 	}  }  form MessagePanel extends JPanel {  	JTextArea	messageArea	= new JTextArea( 15, 15 );  	MessagePanel() { 		super(); 		add together( messageArea ); 	} }  form EditorPanel extends JPanel {  	EditorPanel() { 		super(); 		setPreferredSize( new Dimension( ( int ) ( Appwindow.winX / 1.five ), Appwindow.winY - fifty ) ); 		setBorder( BorderFactory.createLineBorder( Colour.blackness ) ); 		setBackground( Colour.WHITE ); 	}  	public void paintComponent( Graphics g ) { 		super.paintComponent( grand );  	} }            

This mail service has been edited past blackcompe: 30 August 2011 - 06:24 PM

smithcamvintat.blogspot.com

Source: https://www.dreamincode.net/forums/topic/245239-trying-to-create-a-family-tree-program/

0 Response to "Write a Java Program That Will Accept Input and Display a Person's Family Tree"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel