0

I am having issues populating my JTextfields when I click on a specific row on my JTable. When I click a row in my JTable, the JTextfields are not populated with their respective values. Please help. Thanks.

My codes:

scrollPane = new JScrollPane();
    scrollPane.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {

            int row = table.getSelectedRow();
            String tbl_click = (table.getModel().getValueAt(row, 0).toString());

            try{
                String sql = "SELECT * FROM employee where EmployeeID = '"+tbl_click+"' ";
                stt = con.prepareStatement(sql);
                rs = stt.executeQuery();


                if(rs.next()){
                    int ids = rs.getInt("EmployeeID");
                    id.setText(String.valueOf(ids).trim());

                    String fn = rs.getString("FirstName");
                    fname.setText(fn);

                    String ln = rs.getString("LastName");
                    lname.setText(ln);
                }

                stt.close();

            }catch(Exception e1){
                e1.printStackTrace();
            }

        }
    });
    scrollPane.setBounds(312, 277, 551, 246);
    frame.getContentPane().add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);
    table.setModel(DbUtils.resultSetToTableModel(rs));
yetin
  • 61
  • 11
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Hard code some data to replace the DB. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. 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). – Andrew Thompson Apr 12 '15 at 08:16
  • Euh.. did you understand my question ? – yetin Apr 12 '15 at 08:20
  • *"did you understand my question ?"* I understand your code issue (you forgot to ask a question). I'll give further thought to an answer as soon as I see an MCVE. Voting to close. – Andrew Thompson Apr 12 '15 at 08:25
  • Voting to close ? LOL man ! If you cant help, keep your comments to yourself. I dont know what I've missed that you are talking about MCVE or whatever. I have an issue, so I posted it on the forum with my attempts to get a better insight. Thanks – yetin Apr 12 '15 at 08:28
  • Ask yourself this question. What relationship does `DbUtils.resultSetToTableModel(rs)` have with the version of `rs` in the `MouseListener`. I'm also kind of weirded out with the `MouseListener` on the `JScrollPane` as it's unlikely to ever be triggered – MadProgrammer Apr 12 '15 at 08:41
  • The DbUtils.resultStToTablModel(rs) retrieves the data from the database, and displays them in the JTable. How to make JScollPane to get triggered when I click on a row? – yetin Apr 12 '15 at 08:44

1 Answers1

1

MouseEvents are like rain drops, they fall through the component hierarchy from the top to the bottom until they hit something that is monitoring for them, then they stop.

The likely cause of your problem is the JTable, which resides within the JScrollPane will prevent the JScrollPane from ever been notified of any MouseEvents.

I'm "guessing" you need to add the MouseListener to the JTable instead. In fact, I'd probably use a ListSelectionListener, via the row ListSelectionModel, instead

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah.. When I right click on my JTable component, I have surround it with a JScrollPane.. Now when I right to click to add an event handler, it only directs me to the JScrollPane event handler. There's no options for choosing the JTable MouseListener. What do you suggest? – yetin Apr 12 '15 at 08:46
  • For example ? Give me sa sample – yetin Apr 12 '15 at 09:26
  • 1
    [How to write a MouseListener](https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) – MadProgrammer Apr 12 '15 at 09:29
  • I have removed the ScrollPane.. See my updated codes here http://pastebin.com/H6SU0W2u However, the header is no more visible :( How to achieved that to make it displayed? – yetin Apr 12 '15 at 09:40
  • You don't want to remove the `JScrollPane`, you just want to add the `MouseListener` to the `JTable` – MadProgrammer Apr 12 '15 at 10:02