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));
6
  • 1) For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (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 along with layout padding and borders for white space. Commented Apr 12, 2015 at 8:16
  • Euh.. did you understand my question ? Commented Apr 12, 2015 at 8: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. Commented Apr 12, 2015 at 8: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 Commented Apr 12, 2015 at 8: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 Commented Apr 12, 2015 at 8:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
For example ? Give me sa sample
I have removed the ScrollPane.. See my updated codes here pastebin.com/H6SU0W2u However, the header is no more visible :( How to achieved that to make it displayed?
You don't want to remove the JScrollPane, you just want to add the MouseListener to the JTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.