public MainFrame() {
    super("Hello World");

    setLayout(new BorderLayout());

    toolbar = new Toolbar();
    textPanel = new TextPanel();
    formPanel = new FormPanel();
    tablePanel = new TablePanel();

    controller = new Controller();

    tablePanel.setData(controller.getPeople());

    fileChooser = new JFileChooser();

    fileChooser.addChoosableFileFilter(new PersonFileFilter());

    setJMenuBar(createMenuBar());

    toolbar.setStringListener(
        new StringListener() {
          public void textEmitted(String text) {
            textPanel.appendText(text);
          }
        });

    formPanel.setFormListener(
        new FormListener() {
          public void formEventOccurred(FormEvent e) {
            controller.addPerson(e);
            tablePanel.refresh();
          }
        });

    add(formPanel, BorderLayout.WEST);
    add(toolbar, BorderLayout.NORTH);
    add(tablePanel, BorderLayout.CENTER);

    setMinimumSize(new Dimension(500, 400));
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }
示例#2
0
  public MainFrame() {
    super("Intercepting Window Closing");

    setLayout(new BorderLayout());

    toolbar = new Toolbar();
    textPanel = new TextPanel();
    formPanel = new FormPanel();
    tablePanel = new TablePanel();
    /*
     * MainFrame extends JFrame, so 'this' is a JFrame, the right kind of
     * field for the constructor
     */
    prefsDialog = new PrefsDialog(this);
    prefs = Preferences.userRoot().node("db");

    controller = new Controller();

    tablePanel.setData(controller.getPeople());

    tablePanel.setPersonTableListener(
        new PersonTableListener() {
          public void rowDeleted(int row) {
            controller.removePerson(row);
          }
        });

    prefsDialog.setPrefsListener(
        new PrefsListener() {

          @Override
          public void preferencesSet(String user, String password, int port) {
            prefs.put("user", user);
            prefs.put("password", password);
            prefs.putInt("port", port);
            System.out.println(user + ": " + password + ", port: " + port);
          }
        });
    String user = prefs.get("user", "Pedro");
    String password = prefs.get("password", "here");
    int port = prefs.getInt("port", 3306);
    prefsDialog.setDefaults(user, password, port);

    fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new PersonFileFilter());
    setJMenuBar(createMenuBar());

    toolbar.setToolbarListener(
        new ToolbarListener() {

          @Override
          public void saveEventOccurred() {

            connect();

            try {
              controller.save();
            } catch (SQLException e) {
              JOptionPane.showMessageDialog(
                  MainFrame.this,
                  "Cannot save to Database",
                  "Database Connection Problem",
                  JOptionPane.ERROR_MESSAGE);
            }
          }

          @Override
          public void refreshEventOccurred() {

            connect();

            try {
              controller.load();
            } catch (SQLException e) {
              JOptionPane.showMessageDialog(
                  MainFrame.this,
                  "Cannot load from Database",
                  "Database Connection Problem",
                  JOptionPane.ERROR_MESSAGE);
            }

            tablePanel.refresh();
          }
        });

    formPanel.setFormListener(
        new FormListener() {
          public void formEventOccurred(FormEvent ev) {
            // textPanel.appendText(name + ": " + occupation + ": "
            // + ageCategory + ", " + empCat + ", " + gender + "\n");
            controller.addPerson(ev);
            tablePanel.refresh();
          }
        });

    addWindowListener(
        new WindowAdapter() {

          @Override
          public void windowClosing(WindowEvent e) {
            controller.disconnect();
            dispose();
            System.gc();
          }
        });

    add(toolbar, BorderLayout.NORTH);
    add(formPanel, BorderLayout.WEST);
    // add(textPanel, BorderLayout.CENTER);
    add(tablePanel, BorderLayout.CENTER);

    setSize(800, 500);
    // to avoid fields collapsing and disappearing on resizing
    setMinimumSize(new Dimension(500, 500));
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    setVisible(true);
  }
示例#3
0
 @Override
 protected void paintComponent(Graphics g) {
   super.paintComponent(g);
 }