JTree не появляется при использовании другого jframe для ввода - PullRequest
0 голосов
/ 05 ноября 2018

Я новичок в JAVA и в настоящее время создаю генеалогическое древо, однако у меня возникли некоторые проблемы.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * '*' '' '' 'отображается в графическом интерфейсе, когда я вызывал CreateTree методы из familytree (содержат мой JTree) класс напрямую с инициализированным объектом person, однако, если бы я вызвал TreeCreation (новый jframe для ввода) класс для jframe только для приема входных данных от пользователя перед передачей объекта person в качестве параметра в CreateTree методы и последующим обновлением моего основного графического интерфейса, почему-то JTree не отображается.

Я попытался отладить, и я вижу, что мой JTree содержит эти объекты, но по какой-то причине он просто не отображается в главном интерфейсе. я потратил 3 дня, пытаясь выяснить, что заставляет его не показываться в графическом интерфейсе после использования другого класса для ввода. Любая помощь приветствуется. Пожалуйста, дайте мне знать, если это возможно, иначе я перестану пытаться исправить это и найду другой способ, поскольку мой срок подходит к концу.

public class FamilyTreeApp extends JFrame implements ActionListener{
private JLabel Header;
private JPanel Body;
private JScrollPane FamilyTree;
private JButton createTreeB, saveTreeB, loadTreeB, editTreeB;
                                //skipped gui components
Container pane = getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 4;
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    pane.add(Header, c);

    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 1;      
    pane.add(createTreeB, c);       

    c.gridx = 1;
    c.gridy = 1;       
    pane.add(loadTreeB, c);

    c.gridx = 2;
    c.gridy = 1;       
    pane.add(saveTreeB, c);

    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(15,0,0,0);
    FamilyTree.setPreferredSize(new Dimension(250,600));
    pane.add(FamilyTree, c);

    c.gridwidth = 2;
    c.gridx = 2;
    c.gridy = 2;
    Body.setPreferredSize(new Dimension(250,600));
    pane.add(Body, c);
public void actionPerformed(ActionEvent e) 
{
    if(e.getSource() == createTreeB)
    { 
       getContentPane().removeAll();
       TC.preparePanel();
       PrepareGUI();
       repaint();
       revalidate();
    }


public class TreeCreation extends JFrame implements ActionListener 
{
private JPanel mainPanel;
private JScrollPane mainScrollPane;
private JLabel LOwner, LFather, LMother, LSpouse, LfirstChild, LsecondChild, fName, bSurname, mSurname, gender, streetNumber, streetName, suburb, postCode, description;
private JTextField Name, faName, mName, sName, c1Name, c2Name;
private JTextField originSurname, faOriginSurname, mOriginSurname, sOriginSurname, c1OriginSurname, c2OriginSurname;
private JTextField marriageSurname, faMarriageSurname, mMarriageSurname, sMarriageSurname, c1MarriageSurname, c2MarriageSurname;
private JTextField Gender, faGender, mGender, sGender, c1Gender, c2Gender;
private JTextField stNumber, faStNumber, mStNumber, sStNumber, c1StNumber, c2StNumber;
private JTextField stName, faStName, mStName, sStName, c1StName, c2StName;
private JTextField Suburb, faSuburb, mSuburb, sSuburb, c1Suburb, c2Suburb;
private JTextField postalCode, faPostalCode, mPostalCode, sPostalCode, c1PostalCode, c2PostalCode;
private JTextArea Description, faDescription, mDescription, sDescription, c1Description, c2Description;
private JButton submitButton, clearButton;

public void preparePanel()
{
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
           //skipped inits fields, label and adding them to mainPanel
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == submitButton)
    {   
        Person Owner = new Person();
        Person Father = new Person();
        Person Mother = new Person();
        Person Spouse = new Person();
        Person Child1 = new Person();
        Person Child2 = new Person();

        Owner.setName(Name.getText());
        Owner.setbSurname(originSurname.getText());
        Owner.setmSurname(marriageSurname.getText());
        Owner.setGender(Gender.getText());
        Owner.setStreetNumber(stNumber.getText());
        Owner.setStreetName(stName.getText());
        Owner.setSuburb(Suburb.getText());
        Owner.setPostcode(postalCode.getText());
        Owner.setDescription(Description.getText());

        Father.setName(faName.getText());
        Father.setbSurname(faOriginSurname.getText());
        Father.setmSurname(faMarriageSurname.getText());
        Father.setGender(faGender.getText());
        Father.setStreetNumber(faStNumber.getText());
        Father.setStreetName(faStName.getText());
        Father.setSuburb(faSuburb.getText());
        Father.setPostcode(faPostalCode.getText());
        Father.setDescription(faDescription.getText());

        Mother.setName(mName.getText());
        Mother.setbSurname(mOriginSurname.getText());
        Mother.setmSurname(mMarriageSurname.getText());
        Mother.setGender(mGender.getText());
        Mother.setStreetNumber(mStNumber.getText());
        Mother.setStreetName(mStName.getText());
        Mother.setSuburb(mSuburb.getText());
        Mother.setPostcode(mPostalCode.getText());
        Mother.setDescription(mDescription.getText());

        Spouse.setName(sName.getText());
        Spouse.setbSurname(sOriginSurname.getText());
        Spouse.setmSurname(sMarriageSurname.getText());
        Spouse.setGender(sGender.getText());
        Spouse.setStreetNumber(sStNumber.getText());
        Spouse.setStreetName(sStName.getText());
        Spouse.setSuburb(sSuburb.getText());
        Spouse.setPostcode(sPostalCode.getText());
        Spouse.setDescription(sDescription.getText());

        Child1.setName(c1Name.getText());
        Child1.setbSurname(c1OriginSurname.getText());
        Child1.setmSurname(c1MarriageSurname.getText());
        Child1.setGender(c1Gender.getText());
        Child1.setStreetNumber(c1StNumber.getText());
        Child1.setStreetName(c1StName.getText());
        Child1.setSuburb(c1Suburb.getText());
        Child1.setPostcode(c1PostalCode.getText());
        Child1.setDescription(c1Description.getText());

        Child2.setName(c2Name.getText());
        Child2.setbSurname(c2OriginSurname.getText());
        Child2.setmSurname(c2MarriageSurname.getText());
        Child2.setGender(c2Gender.getText());
        Child2.setStreetNumber(c2StNumber.getText());
        Child2.setStreetName(c2StName.getText());
        Child2.setSuburb(c2Suburb.getText());
        Child2.setPostcode(c2PostalCode.getText());
        Child2.setDescription(c2Description.getText());
FamilyTree FT = new FamilyTree();
FT.CreateTree(Owner, Father, Mother, Spouse, Child1, Child2);





public class FamilyTree {
private JTree t1;

/*
Person Owner = new Person("John", "Tan", "Tan", "Male", "10", "Jurong East", "Jurong", "600111", "I'm John");
Person Father = new Person("Johnny", "Tan", "Tan", "Male", "20", "Jurong West", "Jurong", "600222", "I'm John's Father");
Person Mother = new Person("Alice", "Ko", "Tan", "Female", "20", "Jurong West", "Jurong", "600222", "I'm John's Mother");
Person Spouse = new Person("Angela", "Lim", "Tan", "Female", "10", "Jurong East", "Jurong", "600111", "I'm John's Wife");
Person Child1 = new Person("Brandon", "Tan", "Nil", "Male", "10", "Jurong East", "Jurong", "600111", "I'm John's Son");
Person Child2 = new Person("Wendy", "Tan", "Nil", "Female", "10", "Jurong East", "Jurong", "600111", "I'm John's Daughter");
*/    

Vector<Person> Parents;
Vector<Person> Childrens;
Vector<Person> Spouses;

public void CreateTree()
{ same as below 
}

public void CreateTree(Person Owner, Person Father, Person Mother, Person Spouse, Person Child1, Person Child2)
{
    Parents = new Vector();
    Parents.add(Father);
    Parents.add(Mother);
    Spouses = new Vector();
    Spouses.add(Spouse);
    Childrens = new Vector();
    Childrens.add(Child1);
    Childrens.add(Child2);
    Hashtable<String, Object> ht = new Hashtable<String, Object>();

    ht.put("Parents", Parents);
    ht.put("Spouse", Spouses);
    ht.put("Childrens", Childrens);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(Owner);
    JTree.DynamicUtilTreeNode.createChildren(root, ht);
    t1 = new JTree(root);      

}
...