Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.04 KB | None | 0 0
  1. NEWFAMILYTREE.JAVA:
  2.  
  3. package familyTreeTest;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.EventQueue;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10.  
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTabbedPane;
  18.  
  19. import net.miginfocom.swing.MigLayout;
  20. import javax.swing.SwingConstants;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23.  
  24. public class NewFamilyTree {
  25.  
  26.     private JFrame frame;
  27.     private JPanel panel_1;
  28.     private JScrollPane scrollPane;
  29.     private JTabbedPane tabbedPane;
  30.     int cooX = 5;
  31.     int cooY = 90;
  32.  
  33.     /**
  34.      * Launch the application.
  35.      */
  36.     public static void main(String[] args) {
  37.         EventQueue.invokeLater(new Runnable() {
  38.             public void run() {
  39.                 try {
  40.                     NewFamilyTree window = new NewFamilyTree();
  41.                     window.frame.setVisible(true);
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.  
  47.         });
  48.     }
  49.  
  50.     /**
  51.      * Create the application.
  52.      */
  53.     public NewFamilyTree() {
  54.         initialize();
  55.     }
  56.  
  57.     /**
  58.      * Initialize the contents of the frame.
  59.      */
  60.     private void initialize() {
  61.         frame = new JFrame();
  62.         frame.setTitle("New family tree");
  63.         frame.getContentPane().setBackground(new Color(135, 206, 250));
  64.         frame.setBounds(100, 100, 906, 569);
  65.         frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  66.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.  
  68.         JPanel panel = new JPanel();
  69.         panel.setBackground(new Color(30, 144, 255));
  70.         frame.getContentPane().add(panel, BorderLayout.EAST);
  71.         panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
  72.  
  73.         JButton newPersonButton = new JButton("New Person");
  74.         newPersonButton.addActionListener(new ActionListener() {
  75.             public void actionPerformed(ActionEvent arg0) {
  76.  
  77.                 final Data data = NewPerson.createPerson(frame);
  78.                 if (data != null) {
  79.  
  80.                     String Val = data.CBvalue;
  81.  
  82.                     if (Val == "mother") {
  83.  
  84.                         JLabel lblHomer = new JLabel(data.names);
  85.  
  86.                         lblHomer.setIcon(new ImageIcon(data.fileID));
  87.  
  88.                         cooX = cooX + 20;
  89.                         cooY = cooY - 20;
  90.  
  91.                         panel_1.add(lblHomer, "cell " + cooX + " " + cooY);
  92.                         panel_1.revalidate();
  93.  
  94.                     } else {
  95.                         JLabel lblHomer = new JLabel(data.names);
  96.                         lblHomer.addMouseListener(new MouseAdapter() {
  97.                             @Override
  98.                             public void mouseClicked(MouseEvent arg0) {
  99.  
  100.                                 final Data data = NewPerson.createPerson(frame);
  101.                                 if (data != null) {
  102.  
  103.                                     String Val = data.CBvalue;
  104.  
  105.                                     if (Val == "mother") {
  106.  
  107.                                         JLabel lblHomer = new JLabel(data.names);
  108.  
  109.                                         lblHomer.setIcon(new ImageIcon(data.fileID));
  110.  
  111.                                         cooX = cooX + 20;
  112.                                         cooY = cooY - 20;
  113.  
  114.                                         panel_1.add(lblHomer, "cell " + cooX + " " + cooY);
  115.                                         panel_1.revalidate();
  116.                                
  117.                             }}}
  118.                         });
  119.  
  120.                         lblHomer.setIcon(new ImageIcon(data.fileID));
  121.  
  122.                         panel_1.add(lblHomer, "cell " + cooX + " " + cooY);
  123.                         panel_1.revalidate();
  124.                     }
  125.                 }
  126.             }
  127.         });
  128.         panel.add(newPersonButton, "cell 0 5");
  129.  
  130.         JButton btnNewButton_1 = new JButton("New button");
  131.         panel.add(btnNewButton_1, "cell 0 6");
  132.  
  133.         tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  134.         frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
  135.  
  136.         scrollPane = new JScrollPane(null);
  137.         tabbedPane.addTab("Tree", null, scrollPane, null);
  138.  
  139.         panel_1 = new JPanel(null);
  140.         scrollPane.setViewportView(panel_1);
  141.         panel_1.setLayout(new MigLayout(
  142.                 "",
  143.                 "[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]",
  144.                 "[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]"));
  145.  
  146.         JScrollPane scrollPane_1 = new JScrollPane();
  147.         tabbedPane.addTab("Info", null, scrollPane_1, null);
  148.  
  149.         frame.repaint();
  150.  
  151.     }
  152.  
  153. }
  154.  
  155. NEWPERSON.JAVA:
  156.  
  157. package familyTreeTest;
  158.  
  159. import java.awt.Color;
  160. import java.awt.Component;
  161. import java.awt.Dialog;
  162. import java.awt.Frame;
  163. import java.awt.Window;
  164. import java.awt.event.ActionEvent;
  165. import java.awt.event.ActionListener;
  166. import java.io.File;
  167.  
  168. import javax.swing.JButton;
  169. import javax.swing.JComboBox;
  170. import javax.swing.JDialog;
  171. import javax.swing.JFileChooser;
  172. import javax.swing.JLabel;
  173. import javax.swing.JPanel;
  174. import javax.swing.JTextArea;
  175. import javax.swing.JTextField;
  176. import javax.swing.SwingUtilities;
  177.  
  178. import net.miginfocom.swing.MigLayout;
  179.  
  180. public class NewPerson extends JPanel {
  181.  
  182.     private JTextField textFieldNames;
  183.     private JButton selectPictureButton;
  184.     private JLabel labelDateOfBirth;
  185.     private JTextField textFieldDateOfBirth;
  186.     private JLabel labelShortBio;
  187.     private JTextArea textAreaBio;
  188.     private JLabel labelSelectPicture;
  189.     private JButton buttonAdd;
  190.     private String fileID;
  191.  
  192.     private Data data;
  193.     private JComboBox comboBox;
  194.  
  195.     /**
  196.      * Create the application.
  197.      */
  198.     private NewPerson() {
  199.         initialize();
  200.     }
  201.  
  202.     public static Data createPerson(Component comp) {
  203.         NewPerson newPerson = new NewPerson();
  204.         Window win = SwingUtilities.getWindowAncestor(comp);
  205.         JDialog dialog = null;
  206.         if (win instanceof Frame) {
  207.             dialog = new JDialog((Frame) win, "New person", true);
  208.         } else if (win instanceof Dialog) {
  209.             dialog = new JDialog((Dialog) win, "New person", true);
  210.         } else {
  211.             dialog = new JDialog((Frame) null, "New person", true);
  212.         }
  213.         newPerson.addActionListener(new ActionListener() {
  214.  
  215.             public void actionPerformed(ActionEvent e) {
  216.                 Object source = e.getSource();
  217.                 if (source instanceof Component) {
  218.                     Window win = SwingUtilities
  219.                             .getWindowAncestor((Component) source);
  220.                     win.dispose();
  221.                 }
  222.             }
  223.         });
  224.         dialog.getContentPane().add(newPerson);
  225.         dialog.pack();
  226.         dialog.setLocationRelativeTo(comp);
  227.         dialog.setVisible(true);
  228.  
  229.         return newPerson.getData();
  230.     }
  231.  
  232.     public void addActionListener(ActionListener listener) {
  233.         listenerList.add(ActionListener.class, listener);
  234.     }
  235.  
  236.     protected void fireActionPerformed() {
  237.         ActionListener[] listeners = listenerList
  238.                 .getListeners(ActionListener.class);
  239.         if (listeners != null && listeners.length > 0) {
  240.             ActionEvent evt = new ActionEvent(this,
  241.                     ActionEvent.ACTION_PERFORMED, "created");
  242.             for (ActionListener listener : listeners) {
  243.                 listener.actionPerformed(evt);
  244.             }
  245.         }
  246.     }
  247.  
  248.     public Data getData() {
  249.         return data;
  250.     }
  251.  
  252.     /**
  253.      * Initialize the contents of the frame.
  254.      */
  255.     private void initialize() {
  256.         setBackground(new Color(135, 206, 250));
  257.         setLayout(new MigLayout("", "[][][grow]", "[][][][grow][][][]"));
  258.  
  259.         JLabel labelNames = new JLabel("Name and Surname:");
  260.         add(labelNames, "cell 1 1,alignx trailing");
  261.  
  262.         textFieldNames = new JTextField();
  263.         add(textFieldNames, "cell 2 1,growx");
  264.         textFieldNames.setColumns(10);
  265.  
  266.         labelDateOfBirth = new JLabel("Date of birth:");
  267.         add(labelDateOfBirth, "cell 1 2,alignx center,aligny center");
  268.  
  269.         textFieldDateOfBirth = new JTextField();
  270.         add(textFieldDateOfBirth, "cell 2 2,growx");
  271.         textFieldDateOfBirth.setColumns(10);
  272.  
  273.         labelShortBio = new JLabel("Bio:");
  274.         add(labelShortBio, "cell 1 3,alignx center,aligny center");
  275.  
  276.         textAreaBio = new JTextArea();
  277.         add(textAreaBio, "cell 2 3,grow");
  278.  
  279.         labelSelectPicture = new JLabel("Select picture:");
  280.         add(labelSelectPicture, "cell 1 4,alignx center,aligny center");
  281.  
  282.         selectPictureButton = new JButton("...");
  283.         selectPictureButton.setBackground(new Color(30, 144, 255));
  284.         selectPictureButton.addActionListener(new ActionListener() {
  285.             public void actionPerformed(ActionEvent arg0) {
  286.  
  287.                 JFileChooser chooser = new JFileChooser(new File(System
  288.                         .getProperty("user.home") + "\\Desktop"));
  289.  
  290.                 chooser.setDialogTitle("Select Location");
  291.                 chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
  292.                 chooser.setAcceptAllFileFilterUsed(false);
  293.  
  294.                 if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
  295.                     fileID = chooser.getSelectedFile().getPath();
  296.  
  297.                     // txtField.setText(fileID);
  298.                 }
  299.  
  300.             }
  301.         });
  302.         add(selectPictureButton, "cell 2 4");
  303.  
  304.         String[] persons = { "this is my first person", "mother", "father",
  305.                 "brother", "sister", "spouse", "son", "daughter" };
  306.         comboBox = new JComboBox(persons);
  307.         add(comboBox, "cell 2 5,growx");
  308.  
  309.         buttonAdd = new JButton("Add");
  310.         buttonAdd.addActionListener(new ActionListener() {
  311.             public void actionPerformed(ActionEvent arg0) {
  312.  
  313.                 String names = textFieldNames.getText();
  314.                 String dateBirth = textFieldDateOfBirth.getText();
  315.                 String bio = textAreaBio.getText();
  316.                 String CBvalue = comboBox.getSelectedItem().toString();
  317.                 Boolean.valueOf(CBvalue);
  318.                
  319.                 System.out.print(CBvalue);
  320.  
  321.                 data = new Data(names, dateBirth, bio, fileID, CBvalue);
  322.                 fireActionPerformed();
  323.             }
  324.         });
  325.         buttonAdd.setBackground(new Color(30, 144, 255));
  326.         add(buttonAdd, "cell 2 6,grow");
  327.     }
  328.  
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement