Home > Enterprise >  Where would I place my main method in this application?
Where would I place my main method in this application?

Time:12-16

I am currently working on an assignment where I have to create an application that uses dynamic data to fill in trees and lists. I have tried placing the main method in this class before and the only time it looked like I could compile is if i changed all of my variables to static. Can I place my main method in this class (where would I be able to?) or do I need another class? thank you.

import javax.swing.DefaultListModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;

import Accounts.Client;
import Accounts.ClientLoader;
import Accounts.Account;
import Accounts.Bootstrap;
import Accounts.Portfolio;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

import java.awt.Font;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JTextArea;

public class FormWindow {
    protected JFrame frame;
    private DefaultListModel < String > clientDetail = new DefaultListModel < String > ();
    private Client person;
    private JTextField firstName;
    private JTextField lastName;
    private JTextField phoneNumber;

    public FormWindow(Client person) {
        this.person = person;
        initialize();
    }

    private void initialize() {

        frame = new JFrame("Client Manager");
        frame.setBounds(100, 100, 903, 719);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setHorizontalScrollBarPolicy
           (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy
           (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setBounds(10, 11, 619, 192);
        frame.getContentPane().add(scrollPane);

        JTextArea textArea = new JTextArea();
        scrollPane.setViewportView(textArea);

        firstName = new JTextField(person.getFirstName());
        firstName.setBounds(730, 11, 144, 26);
        frame.getContentPane().add(firstName);
        firstName.setColumns(10);

        lastName = new JTextField(person.getLastName());
        lastName.setBounds(730, 66, 144, 26);
        frame.getContentPane().add(lastName);
        lastName.setColumns(10);

        phoneNumber = new JTextField(person.getPhoneNumber());
        phoneNumber.setBounds(730, 123, 144, 26);
        frame.getContentPane().add(phoneNumber);
        phoneNumber.setColumns(10);

        JButton btnNewButton = new JButton("Update Interface");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                person.setFirstName(firstName.getText());
                person.setLastName(lastName.getText());
                person.setPhoneNumber(phoneNumber.getText());
                repaintForm();
            }
        });
        btnNewButton.setBounds(639, 168, 235, 35);
        frame.getContentPane().add(btnNewButton);

        JLabel lblNewLabel = new JLabel("First Name");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel.setBounds(649, 11, 71, 20);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Last Name");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel_1.setBounds(649, 70, 71, 14);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("      Phone");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel_2.setBounds(634, 127, 71, 14);
        frame.getContentPane().add(lblNewLabel_2);

        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setHorizontalScrollBarPolicy
            (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane_1.setBounds(10, 214, 425, 436);
        frame.getContentPane().add(scrollPane_1);

        //needs work: TREE
        DefaultMutableTreeNode portfolios = new DefaultMutableTreeNode("Portfolios");
        JTree tree = new JTree(portfolios);
        scrollPane_1.setViewportView(tree);

        JScrollPane scrollPane_2 = new JScrollPane();
        scrollPane_2.setHorizontalScrollBarPolicy
            (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane_2.setBounds(445, 214, 429, 436);
        frame.getContentPane().add(scrollPane_2);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Client");
        menuBar.add(mnNewMenu);

        JMenuItem mntmNewMenuItem = new JMenuItem("Load Client");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        mnNewMenu.add(mntmNewMenuItem);

        JMenuItem mntmNewMenuItem_1 = new JMenuItem("Save Client");
        mnNewMenu.add(mntmNewMenuItem_1);

        JMenu mnNewMenu_1 = new JMenu("Portfolios");
        menuBar.add(mnNewMenu_1);

        JMenuItem mntmNewMenuItem_2 = new JMenuItem("Add Portfolio");
        mnNewMenu_1.add(mntmNewMenuItem_2);

        JMenuItem mntmNewMenuItem_3 = new JMenuItem("Remove Portfolio");
        mnNewMenu_1.add(mntmNewMenuItem_3);

        JMenu mnNewMenu_2 = new JMenu("Accounts");
        menuBar.add(mnNewMenu_2);

        JMenuItem mntmNewMenuItem_4 = new JMenuItem("Add Account");
        mnNewMenu_2.add(mntmNewMenuItem_4);

        JMenuItem mntmNewMenuItem_5 = new JMenuItem("Remove Account");
        mnNewMenu_2.add(mntmNewMenuItem_5);
    }
    void repaintForm() {
        clientDetail.clear();
        clientDetail.addElement("First Name: "   person.getFirstName());
        clientDetail.addElement("Last Name: "   person.getLastName());
        clientDetail.addElement("Phone Number: "   person.getPhoneNumber());
        clientDetail.addElement("Total Value: "   person.getClientPortfolioValue());
        clientDetail.addElement("Portfolio Count: "   person.getPortfolioCount());
        clientDetail.addElement("Portfolio Names: "   person.getClientPortfolioNames());

    }
}

CodePudding user response:

Consider to create an instance of FormWindow inside main method.

  1. Put main into the FormWindow class

  2. Create instance by "new" operator inside main method

    class FormWindow {
    
        public static void main(String[] args) {
            FormWindow form = new FormWindow(...);
        }
        ...
    }
    

CodePudding user response:

in a static method you can only call other static methods. Thats why the compiler changes your methodes to static. What you want is to create another class with a the main method and then create an object of your FormWindow class. Since your constructor needs a Client object , you need to create that object first.

FormWindow fw = new FormWindow(someClientObject)

CodePudding user response:

you need another class and call new instance of FormWindow class if variable is static then you need to assign method that return value to that particular static variable

CodePudding user response:

I think you have to create a default constructor for this class either wich provides some default values:

public class FormWindow {
    public FormWindow() {
        Client personWithDefaultValues = new Client();
        //Fill personWithDefaultValues with some data here
    }

    public static void main(String[] args) {
        FormWindow h = new FormWindow();
        h.initialize();
    }
}
  • Related