0

I have a GUI which has a textarea and a "save button" on it. I want the text that is written in the textarea to be saved when the button is pressed. I have written this code so far:

   //Creates textbox
   JTextArea text = new JTextArea();
   text.setBounds(48, 44, 160, 16); //int (x,y,width,height)

and

 //Button
 JButton saveButton = new JButton("Save");
 saveButton.setBounds(10, 185, 120, 20); //int (x,y,width,height)

And I have also added it to the JPanel. Everything appears as it should, I just don't know how to save the text that is written in the textarea, I have tried googling it, but it seems like there is many ways to do so and I don't know how I can implement it in a simple way that I understand. Thanks.

EDIT: I want to save the data into a string, so I can save it into a database.

2
  • 1
    Where you want to save? At file or some where else. Commented Nov 29, 2013 at 13:59
  • I answered your question by editting my post, look at the "EDIT". Commented Nov 29, 2013 at 14:02

3 Answers 3

3

You need to add an ActionListener to your button, and save text inside actionPerformed() method:

    JButton saveButton = new JButton("Save");
    saveButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            String areaText = text.getText();
            //saveText(areaText);
        }
    });

If your text variable is a local, you need to set it as final.

Read about ActionListener.

Also use LayoutManager instead of setBounds(), tutorial.

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

3 Comments

That worked, thanks! What is the advantages of using layout manager compared to setbounds()? It works fine, but is it bad coding practice?
Yeah, it's bad practice, for example you will have problems with resizing.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space. See also JTextComponent.write(Writer).
0

This is will give you a rough idea and you can go ahead

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://localhost/yourtablename";

//  Database credentials
static final String USER = "username";
static final String PASS = "password";    
Connection conn = null;
Statement stmt = null;

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String areaText = text.getText();
        Class.forName("com.mysql.jdbc.Driver");


       System.out.println("Connecting to database...");
       conn = DriverManager.getConnection(DB_URL,USER,PASS);


       System.out.println("Creating statement...");
       stmt = conn.createStatement();
       String sql;
       sql = "insert into your_table_name values("+areaText+")";
       ResultSet rs = stmt.executeUpdate(sql);
    }
});

Comments

0

If you want to save at database. Than you need to follow these steps.

  1. Select a database server (MySQL, Derby).
  2. Create a database and connect to database. Learn JDBC how to connect.
  3. Create a table and define columns according to your needs.
  4. Execute insert command at actionPerformed method.

Here is the sample code of DB connection

 Class.forName("com.mysql.jdbc.Driver");// For MySQL
 Connection con = DriverManager.getConnection(url,UN, PW);

Here is the sample code of Button ActionListener.

 JButton saveButton = new JButton("Save");
 saveButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String areaText = text.getText();
        //Execute database insert command to save at Database.
       PreparedStatement stmt=con.createPreaparedStatement(
                          "insert into table_name(col_name) values(?)");
        stmt.setString(1, areaText);
        stmt.executeUpdate();// To save into DB.
    }
});

3 Comments

thats nice, but isn't it considered bad practise to combine GUI with Database? I would prefer to hold these things seperated by using interfaces? Is that possible?
Yes, to keep separate using interface is good practice. That is possible.
@PHPeter, Go through the tutorial of JDBC. There was a good example of CoffeesFrame. Follow that code convention.

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.