Results 1 to 2 of 2
  1. #1
    BulletProof47 is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default Swing app MVC problem

    When I click on the add button I'm getting empty string not the value typed in the textfield. Why is that and is my way of coding is correct? Also I haven't included the model because the code was long for these two.Please tell me what should there in model class. Please help thanks.

    **View Class Contact.java**

    public void contactGUI(){

    panel = new JPanel();
    dialog = new JDialog();
    gc= new GridBagConstraints();
    gbl = new GridBagLayout();
    panel.setLayout(gbl);
    gc.anchor=GridBagConstraints.WEST;
    gc.fill=GridBagConstraints.NONE;
    gc.gridx=0;
    gc.gridy=0;
    gc.insets=new Insets(5,5, 5, 5);
    lblFname = new JLabel("First Name");
    gbl.setConstraints(lblFname, gc);

    txtFname = new JTextField(15);
    gc.gridx=1;
    gc.gridy=0;
    gbl.setConstraints(txtFname, gc);

    lblErrFname = new JLabel();
    lblErrFname.setPreferredSize(new Dimension(200, 15));
    lblErrFname.setForeground(Color.red);
    gc.gridx=0;
    gc.gridy=1;
    gc.gridwidth=6;
    gbl.setConstraints(lblErrFname, gc);

    panel.add(lblFname);
    panel.add(txtFname);
    panel.add(lblErrFname);
    panel.add(btnAdd);

    btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

    try {
    ContactController cc= new ContactController();
    cc.btnAddContact();
    } catch (SQLException ex) {
    Logger.getLogger(Contact.class.getName()).log(Leve l.SEVERE, null, ex);
    }
    }
    });
    gc.gridx=0;
    gc.gridy=2;
    gbl.setConstraints(btnAdd, gc);
    panel.setBorder(new EmptyBorder(20, 20, 20, 20));
    dialog.add(panel);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON _CLOSE);
    dialog.pack();
    }

    public void showDialog() throws SQLException{

    dialog.setVisible(true);
    }

    //Getter Setters for all the fields


    public String getTxtFname() {

    return txtFname.getText();
    }

    public void setTxtFname(String txtFname) {

    this.txtFname.setText(txtFname);
    }

    public void setErrFname(String mesg){

    this.lblErrFname.setText(mesg);
    }

    public static void main(String args[]){

    try {
    UIManager.setLookAndFeel(new NimbusLookAndFeel());
    } catch (UnsupportedLookAndFeelException ex) {
    Logger.getLogger(Contact.class.getName()).log(Leve l.SEVERE, null, ex);
    }
    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
    try {
    Contact c = new Contact();
    c.contactGUI();
    c.showDialog();
    } catch (SQLException ex) {
    Logger.getLogger(Contact.class.getName()).log(Leve l.SEVERE, null, ex);
    }
    }
    });
    }

    **View Class ContactController.java**
    public void btnAddContact() throws SQLException{

    if (validateForm()) {
    JOptionPane.showMessageDialog(null, "dsa");
    Contact c = new Contact();
    ContactModel cm = new ContactModel(c.getTxtFname(),c.getTxtMname(),c.get TxtLname(),
    c.getTxtCompany(), c.getTxtEmail(), c.getTxtStreet(),c.getTxtCity(),c.getTxtCountry(),
    c.getTxtPhone(),c.getStatus());
    cm.addContact();
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Swing app MVC problem

    When posting code please wrap it in [code] tags [/code], otherwise it's almost unreadable.

    Put some debug code in there (println), probably in the btnAddContact method, to see what values you are using...because I don't see anywhere in that method where you are reading the values form the fields of the relevant bit of the GUI.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. The Strangest Problem (Swing)
    By Snoman in forum AWT / Swing
    Replies: 2
    Last Post: 09-16-2012, 09:29 AM
  2. Simple Swing problem
    By Arom in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2012, 06:50 PM
  3. Java Swing Problem
    By mohitgupta1989 in forum Advanced Java
    Replies: 7
    Last Post: 06-23-2010, 08:46 AM
  4. problem with JDeveloper and Swing
    By Namrata in forum Other IDEs
    Replies: 3
    Last Post: 09-27-2009, 08:06 PM
  5. SWT & Swing Bridge problem
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 03-26-2008, 02:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •