1

I am facing problems in getting the data displayed in a texfield from database when I am selecting a value in combobox. I am only able to display the first item in the combobox into the textfield as it is by default loading as the page loads. Rest when I am selecting from combobox nothing happens. Please help. This is the place in code I need to work on.

cmbProjects = new JComboBox<String>();

cmbProjects.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
    try {
      String query= "SELECT * FROM PROJECT WHERE PROJ_NAME = ?";
      PreparedStatement pst= connection.prepareStatement(query);
      pst.setString(1, (String)cmbProjects.getSelectedItem());

      ResultSet rs= pst.executeQuery();
      while(rs.next()){
        textField.setText(rs.getString("PROJ_ID"));
        textField_1.setText(rs.getString("PROJ_WO_NUMBER"));
        textField_2.setText(rs.getString("PROJ_START_DATE"));
        textField_3.setText(rs.getString("PROJ_END_DATE"));
        //JTextArea.setText(rs.getString("PROJ_COMMENTS"));
        textField_4.setText(rs.getString("PROJ_STATUS"));
        textField_5.setText(rs.getString("PROJ_ADDED_BY"));
        textField_6.setText(rs.getString("PROJ_ADDED_ON"));
      }
    }
    catch(Exception ex){

    }
  }
});

cmbProjects.setToolTipText("well");
cmbProjects.setBounds(139, 94, 394, 27);
panelDetails3_1.add(cmbProjects);
mortalis
  • 2,060
  • 24
  • 34
  • What are the values in `cmbProjects` comboBox? – Pooja Arora Apr 04 '17 at 08:56
  • 2
    `catch(Exception ex){ }` 1) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 04 '17 at 08:56
  • use a `ItemListener` instead of actionListener – XtremeBaumer Apr 04 '17 at 08:56
  • .. 3) `cmbProjects.setBounds(139, 94, 394, 27);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) Adding components at run-time is tricky. Instead add it at start-up and change the model when needed. 5) Use a logical and consistent form of indenting code lines .. – Andrew Thompson Apr 04 '17 at 08:57
  • .. and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Apr 04 '17 at 09:00

0 Answers0