I need to have a update button to update a edited textfield and save it to the database. I have created the button but have no idea on how to make it update the edited textfield and update the SQL database automatically. Pls help!!):

This is my code:

private JButton getJButtonUpdate() {
    int centralize_width = (screenSize.width/2) - 150;
    if (jButton_Update == null) {
        jButton_Update = new JButton();
        jButton_Update.setText("Update");
        jButton_Update.setLocation(new Point(centralize_width, 300));
        jButton_Update.setSize(new Dimension(150, 30));
        jButton_Update.setHorizontalTextPosition(SwingConstants.CENTER);
        jButton_Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                System.out.println("Moving on to Existing Panel!");
                if(Selected != null) {                          
                    JPanel panel = new Case_Details(frame, Analyst_ID, Selected);
                    frame.getContentPane().removeAll();
                    frame.getContentPane().add(panel);
                    frame.getContentPane().validate();
                    frame.getContentPane().repaint();
                }
            }
        });
    }
    return jButton_Update;
}

Edited 4 Years Ago by androidf: n/a

how to make it update

Can you explain your problem a bit more?
Is the problem that the code to do the update is NOT being called
or is the problem that the code is not doing the update correctly?

The update is not being called as I don't know how to implement the code to update the textfield and save it in the database.

Are you saying the button's action listener method is being called?

But the code in the action listener method is not doing what you want.

code to update the textfield

The text field has a method you need to call to update it.

What is the name of the textfield variable? I see this code that looks like it would update a textfield:

jButton_Update.setText("Update");

The above code i posted is just to generate a button labelled as 'update'. The textarea which the user can type in to edit the current description is this, 'jTextArea_EvidenceNotes', this is the detailed UI for better reference.

I need to update the new input added in the text area by the user.

Attachments Capture2.PNG 11.79 KB

Where do you try to update jTextArea_EvidenceNotes in the action listener method?
If you want to update date the contents of that field, you need to call one of its methods just like you do here:
jButton_Update.setText("Update");
Do something like this:
jTextArea_EvidenceNotes.setText("THE DATA TO BE SHOWN");

Like this 'jTextArea_EvidenceNotes.setText(devices.getEvidence_Notes(Analyst_ID));' Ive done this to retrieve the current db content to the textarea. After retrieving the content, the user is able to change and update the content to the db. Am i missing out something? ):

I'm suppose to make the 'update' button work. To update the new input by the user and save the update input to the database. Sorry it sounded confusing. Thanks for all your suggestions tho.

Where is your problem "making the button work"?
Here are some of the steps you need to take:
1) create a button
2) add an actionlistner to the button
3) add the button to the GUI
4) user presses the button
5) the actionlistener method is called
6) get the data to put into the text field
7) put the data in the text field.

Which of the above steps are you having a problem with?

I don't know the source of the data for step 6)

This question has already been answered. Start a new discussion instead.