I'm working right now on a project java allowing a manager to create a todo list and an employee to view the different ToDo, process and modify by in exemple adding a comment.
The employee's interface should consist of:
- A lot of JTabbedPane dynamically generated depending on the number of todo in the database.
- Each JtabbedPane must contain the information of the todo ( title, JTextfield to add a comment, a JTextBox to mark the todo as done and a Jbutton to save our changes.
My problem is that when I generate my JTextfield, etc.. in my different JTabbedPane they all have the same id because I use a while loop as follows:
private void init_employee() {
try {
/* create Frame */
setTitle("SUPTODO Employee");
setSize(800, 800);
/* New panel*/
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel);
tabbedPane = new JTabbedPane();
/* Select in my data base */
Statement stmt = (Statement) ConnectionManager.getConnection().createStatement();
String sql="SELECT id, title, contenu, comments FROM sup_todo WHERE done = 0";
ResultSet rset=(ResultSet) stmt.executeQuery(sql);
while(rset.next())
{
/* Get data */
ToDo todo = new ToDo();
todo.setId(rset.getLong(1));
todo.setTitle(rset.getString(2));
todo.setContenu(rset.getString(3));
todo.setComments(rset.getString(4));
/* Create the field */
panel = new JPanel();
panel.setLayout(null);
title = new JLabel(todo.getTitle());
title.setBounds( 200, 0, 100, 100 );
contenu = new JLabel(todo.getContenu());
contenu.setBounds(200, 50, 400, 200 );
employeeTextFieldToDo = new JTextField();
employeeTextFieldToDo.setBounds(200,300,400,200);
employeeCheckBoxToDo = new JCheckBox("Mark as done !");
employeeCheckBoxToDo.setBounds(200, 500, 200, 100);
employeeAddCommentToDo = new JButton("Save");
employeeAddCommentToDo.setBounds(350, 600, 100, 50);
//employeeAddCommentToDo.setBounds(x, y, width, height);
/* Action */
/* Save comments */
employeeAddCommentToDo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/* PROBLEM HERE : Get the text of my last JTextField, not of the JTextField in his JTabbedPane */
String comment = employeeTextFieldToDo.getText();
JOptionPane.showConfirmDialog(SupTodoEmployeeFrame.this, "Votre commentaire est : " + comment, "comment message", JOptionPane.DEFAULT_OPTION);
}
});
/* Mark as done */
employeeCheckBoxToDo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
/* add to panel */
panel.add(title);
panel.add(contenu);
panel.add(employeeTextFieldToDo);
panel.add(employeeAddCommentToDo);
panel.add(employeeCheckBoxToDo);
panel.setVisible(true);
tabbedPane.addTab(todo.getTitle(), panel);
topPanel.add(tabbedPane, BorderLayout.CENTER);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
How can I generate my JTexfield, JButton, ... with a unique name so that my action on the save button and on the checkbox will concern the right ToDo, execute the SQL command with on the right row and not the last field with this name.