954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java simple update button

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;
}
androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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");
NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.79KB
androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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");

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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? ):

androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Sorry, I don't understand what your problem is now.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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)

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
ndrichim
Newbie Poster
20 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

Please don't hijack someone else's thread.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

will try it, thank you.:)

androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

use getText() method in action listener and then execute SQL's update query using that text.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 

Thank you just tried it:)

androidf
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: