Abhinav Khurana
Abhinav Khurana

Reputation: 3

how to determine Parent Class for my javafx vbox

I am novice to javaFX, and i am working on a javaFX application. The error i got is that i am unable to fetch the child node from fxml file, The fxml file is like this

<children>
    <VBox fx:id="sideMenuVBox" alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="600.0" minWidth="125.0">
        <children>
        <Button fx:id="headingButton" maxWidth="1.7976931348623157E308" onAction="#helpButtonOnAction" text="Masters" />
            <Button fx:id="openCompanyButton" maxWidth="1.7976931348623157E308" onAction="#openCompanyButtonOnAction" text="Open Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="newCompanyButton" maxWidth="1.7976931348623157E308" onAction="#newCompanyButtonOnAction" text="New Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="closeCompanyButton" maxWidth="1.7976931348623157E308" onAction="#closeCompanyButtonOnAction" text="Close Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="chartOfAccountsButton" maxWidth="1.7976931348623157E308" onAction="#chartOfAccountsButtonOnAction" text="Chart of Accounts" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="newUserButton" maxWidth="1.7976931348623157E308" onAction="#newUserButtonOnAction" text="New User" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="userSetUpButton" maxWidth="1.7976931348623157E308" onAction="#userSetUpButtonOnAction" text="User SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="unitMasterButton" maxWidth="1.7976931348623157E308" onAction="#unitMasterButtonOnAction" text="Unit Master" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="itemMasterButton" maxWidth="1.7976931348623157E308" onAction="#itemMasterButtonOnAction" text="Item Master" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="narrationSetUpButton" maxWidth="1.7976931348623157E308" onAction="#narrationSetUpButtonOnAction" text="Narration SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="miscellaneousSetUpButton" maxWidth="1.7976931348623157E308" onAction="#miscellaneousSetUpButtonOnAction" text="Miscellaneous SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="numberingSetUpButton" maxWidth="1.7976931348623157E308" onAction="#numberingSetUpButtonOnAction" text="Numbering SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="sundrySetUpButton" maxWidth="1.7976931348623157E308" onAction="#sundrySetUpButtonOnAction" text="Sundry SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="inventoryFlowButton" maxWidth="1.7976931348623157E308" onAction="#inventoryFlowButtonOnAction" text="Inventory Flow" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="challanTypeButton" maxWidth="1.7976931348623157E308" onAction="#challanTypeButtonOnAction" text="Challan Type" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="mergeLedgersButton" maxWidth="1.7976931348623157E308" onAction="#mergeLedgersButtonOnAction" text="Merge Ledgers" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="transferOpeningBalanceButton" maxWidth="1.7976931348623157E308" onAction="#transferOpeningBalanceButtonOnAction" text="Transfer Opening Balance" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="backUpRestoreButton" maxWidth="1.7976931348623157E308" onAction="#backUpRestoreButtonOnAction" text="Back Up &amp; Restore" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="helpButton" maxWidth="1.7976931348623157E308" onAction="#helpButtonOnAction" text="Help" textAlignment="CENTER" wrapText="true" />
        </children>
    </VBox>
</children>
</AnchorPane>

I am getting VBox,but not any other button. Here is the corresponding controller code. public class ModifyMenu implements Initializable{

@FXML
private Label titleOfPage;
@FXML
private ComboBox<String> combo;
@FXML
private VBox vbox;
@FXML
private AnchorPane anchor;

private List<String> mastersList;
private List<String> addressBookList;
private List<String> vouchersList;
private List<String> salesPurchaseList;
private List<String> financialRegistersList;
private List<String> inventoryRegistersList;
private List<String> finalReportsList;
private List<String> misReportsList;
@FXML
private AnchorPane content;


public void comboOnAction()
{   

    String selectedItem=this.combo.getSelectionModel().getSelectedItem();
    List<String> resultantList;
    if(selectedItem.equals("Masters"))
    {

        resultantList=this.mastersList;
        this.vbox.getChildren().clear();

        show(resultantList);

    }

    else if(selectedItem.equals("Address Book"))
    {   
        resultantList=this.addressBookList;
        this.vbox.getChildren().clear();

        show(resultantList);
    }

    else if(selectedItem.equals("Vouchers"))
    {
        resultantList=this.vouchersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Sales Purchase"))
    {
        resultantList=this.salesPurchaseList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Financial Registers"))
    {
        resultantList=this.financialRegistersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Inventory Registors"))
    {
        resultantList=this.inventoryRegistersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Final Reports"))
    {
        resultantList=this.finalReportsList;
        this.vbox.getChildren().clear();
        show(resultantList);

    }

    else if(selectedItem.equals("MIS Reports"))
    {
        resultantList=this.misReportsList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }


}

public void show(List<String> menuTypeInString)
{
    ObservableList menuObservaleArrayList=
            FXCollections.observableArrayList(menuTypeInString);
    for(int i=0;i<menuObservaleArrayList.size();i++)
    {
        HBox hbox=new HBox();
        Label dynamicListLabel=new Label();
    //  dynamicListLabel.setMaxSize(100,10);
        dynamicListLabel.setText(menuObservaleArrayList.get(i).toString());
        TextField newNameTextField=new TextField();
    //  newNameTextField.setMaxSize(100,10);
        Button makeChangeButton=new Button();
        makeChangeButton.setText("Change Name");
        makeChangeButton.setOnAction(new EventHandler<ActionEvent>() 
                {
                    @Override
                    public void handle(ActionEvent event)
                    {   
                        String ButtonName=dynamicListLabel.toString();
                        String inputButtonName = newNameTextField.getText();
                        System.out.println(getClass().getResource(""));

                        try {
                            content=FXMLLoader.load(getClass().getResource
                                    ("/com/openlx/kalculate/views/masters/SideMenu.fxml"));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                //  AnchorPane  childHolder;
                //  childHolder.getChildren().addAll(content);

                //content.getChildrenUnmodifiable().listIterator(i).toString();
                    System.out.println(content.getChildren().toString());   
            //      ((Pane) content.getChildren()).getChildren();
                    System.out.println(((Pane) content.getChildren()).getChildren().toString());

                /*  content.getChildren().clear();
                    content.getChildren().addAll();
                    Button btn=(Button) content.lookup(ButtonName);
                    hbox.getChildren().add(dynamicListLabel);
                    hbox.getChildren().add(newNameTextField);
                    hbox.getChildren().add(makeChangeButton);
                    hbox.getChildren().add(btn);

                    */
                    }
                });
        hbox.getChildren().add(dynamicListLabel);
        hbox.getChildren().add(newNameTextField);
        hbox.getChildren().add(makeChangeButton);
        vbox.getChildren().add(hbox);

        if(newNameTextField.getText()!=null)
        {

        }
    }
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

List<String> mainList=new ArrayList<String>();
    mainList.add("Masters");
    mainList.add("Address Book");
    mainList.add("Vouchers");
    mainList.add("Sales Purchase");
    mainList.add("Financial Registers");
    mainList.add("Inventory Registers");
    mainList.add("Final Reports");
    mainList.add("MIS Reports");

ObservableList menuObservaleArrayList=
    FXCollections.observableArrayList(mainList);
combo.setItems(menuObservaleArrayList);

    mastersList=new ArrayList<String>();
    mastersList.add("Open Company");
    mastersList.add("New Company");
    mastersList.add("Close Company");
    mastersList.add("Charts Of Accounts");
    mastersList.add("New User");
    mastersList.add("User Setup");
    mastersList.add("Unit Master");
    mastersList.add("Item Master");
    mastersList.add("Narration Setup");
    mastersList.add("Miscellaneous Setup");
    mastersList.add("Numbering Setup");
    mastersList.add("Sundry Setup");
    mastersList.add("Inventory Flow");
    mastersList.add("Challan Type");
    mastersList.add("Merge Ledgers");
    mastersList.add("Transfering Opening Balance");
    mastersList.add("Back Up & Restore");
    mastersList.add("Help");

addressBookList=new ArrayList<String>();
    addressBookList.add("Show Address Book");
    addressBookList.add("Supplier Entry");
    addressBookList.add("Customer Entry");
    addressBookList.add("Help");

vouchersList=new ArrayList<String>();
    vouchersList.add("Cash Reciept");
    vouchersList.add("Cash Payement");
    vouchersList.add("Bank Reciept");
    vouchersList.add("Bank Payement");
    vouchersList.add("Journal Voucher");
    vouchersList.add("Journal Sales");
    vouchersList.add("Journal Purchase");
    vouchersList.add("Debit Note");
    vouchersList.add("Credit Note");
    vouchersList.add("Contra Entry");
    vouchersList.add("Help");

salesPurchaseList=new ArrayList<String>();
    salesPurchaseList.add("Quotation");
    salesPurchaseList.add("Order Acknowledgement");
    salesPurchaseList.add("Challan Cum Sales Invoice");
    salesPurchaseList.add("Challan");
    salesPurchaseList.add("Sales Invoice");
    salesPurchaseList.add("Sales Return");
    salesPurchaseList.add("Enquiry");
    salesPurchaseList.add("Purchase Order");
    salesPurchaseList.add("Purchase Invoice");
    salesPurchaseList.add("Purchase Return");
    salesPurchaseList.add("GRN Cum Purchase Invoice");
    salesPurchaseList.add("Goods Reciept Note");
    salesPurchaseList.add("Process Existing Voucher");
    salesPurchaseList.add("Stock Adjustment Voucher(Add)");
    salesPurchaseList.add("Stock Adjustment Voucher(Deduct)");
    salesPurchaseList.add("Help");

financialRegistersList=new ArrayList<String>();
    financialRegistersList.add("Cash book");
    financialRegistersList.add("Cash Reciept Book");
    financialRegistersList.add("Cash Payement Book");
    financialRegistersList.add("Bank Book");
    financialRegistersList.add("Bank Payement Book");
    financialRegistersList.add("Bank Reciept Book");
    financialRegistersList.add("Day Book");
    financialRegistersList.add("Ledger");
    financialRegistersList.add("Journal Book");
    financialRegistersList.add("Debit Note Book");
    financialRegistersList.add("Credit Note Book");
    financialRegistersList.add("Cheque Book");
    financialRegistersList.add("Customer Book");
    financialRegistersList.add("Supplier Book");
    financialRegistersList.add("Help");

inventoryRegistersList=new ArrayList<String>();
    inventoryRegistersList.add("Quotation Registers");
    inventoryRegistersList.add("Order Acknowledgement Registers");
    inventoryRegistersList.add("Challan Registers");
    inventoryRegistersList.add("Invoice Registers");
    inventoryRegistersList.add("Sales Return Registers");
    inventoryRegistersList.add("Sales Registers");
    inventoryRegistersList.add("Sales Summary");
    inventoryRegistersList.add("Enquiry Registers");
    inventoryRegistersList.add("Purchase Order Registers");
    inventoryRegistersList.add("Purchase Invoice  Registers");
    inventoryRegistersList.add("Purchase Return Registers");
    inventoryRegistersList.add("Goods Reciept Note Registers");
    inventoryRegistersList.add("Purchase Registers");
    inventoryRegistersList.add("Purchase Summary");
    inventoryRegistersList.add("Stock Registers");
    inventoryRegistersList.add("Stock Movement");
    inventoryRegistersList.add("Stock Adjustment Details");
    inventoryRegistersList.add("Custom Stock BAlance");
    inventoryRegistersList.add("Help");

finalReportsList=new ArrayList<String>();
    finalReportsList.add("Trial Balance");
    finalReportsList.add("Income And Expenditure A/C");
    finalReportsList.add("Profit And Loss A/C");
    finalReportsList.add("Balance Sheet A/C");
    finalReportsList.add("Help");

misReportsList=new ArrayList<String>();
    misReportsList.add("Group Summary");
    misReportsList.add("Statement Of Debtors With Ageing");
    misReportsList.add("Statement Of Creditors With AGeing");
    misReportsList.add("Sundry Ledgers");
    misReportsList.add("Account Status Confirmation");
    misReportsList.add("Payement Reminders");
    misReportsList.add("Negative Cash Warning");
    misReportsList.add("Depraction Statement");
    misReportsList.add("Item Ladger");
    misReportsList.add("Re-Order Level Report");
    misReportsList.add("Rate List");
    misReportsList.add("Customer Ledger");
    misReportsList.add("Supplier Ledger");
    misReportsList.add("Bank Reconciliation");
    misReportsList.add("Help");

}

}

The error is coming in basicaaly this part,

                       @Override
                        public void handle(ActionEvent event)
                        {   
                            String ButtonName=dynamicListLabel.toString();
                            String inputButtonName = newNameTextField.getText();
                            System.out.println(getClass().getResource(""));

                            try {
                                content=FXMLLoader.load(getClass().getResource
                                        ("/com/openlx/kalculate/views/masters/SideMenu.fxml"));
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    //  AnchorPane  childHolder;
                    //  childHolder.getChildren().addAll(content);

                    //content.getChildrenUnmodifiable().listIterator(i).toString();
                        System.out.println(content.getChildren().toString());   
                //      ((Pane) content.getChildren()).getChildren();
                        System.out.println(((Pane) content.getChildren()).getChildren().toString());

                    /*  content.getChildren().clear();
                        content.getChildren().addAll();
                        Button btn=(Button) content.lookup(ButtonName);
                        hbox.getChildren().add(dynamicListLabel);
                        hbox.getChildren().add(newNameTextField);
                        hbox.getChildren().add(makeChangeButton);
                        hbox.getChildren().add(btn);

                        */
                        }
                    });
            hbox.getChildren().add(dynamicListLabel);
            hbox.getChildren().add(newNameTextField);
            hbox.getChildren().add(makeChangeButton);
            vbox.getChildren().add(hbox);

I tried many things but it only says that PArent$2 can't be cast into javafx.scene.(any).

Upvotes: 0

Views: 533

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

Your content variable is an AnchorPane, so the line

((Pane) content.getChildren()).getChildren();

is wrong, since content.getChildren() returns a child list (ObservableList<Node>) and it cannot be cast to Pane.

The VBox with fx:id="sideMenuVBox" is the only child of AnchorPane. You can get it as

VBox vb = (VBox) content.getChildren().get(0);

then you can get button children of vbox by

vb.getChildren();

However, all controls in your FXML file have fx:id attribute defined. You can directly inject and access them with these ids. For more info, do research on for what fx:id is used for.

Upvotes: 0

Related Questions