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.

PHPeter
  • 77
  • 1
  • 8

3 Answers3

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.

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • That worked, thanks! What is the advantages of using layout manager compared to setbounds()? It works fine, but is it bad coding practice? – PHPeter Nov 29 '13 at 14:11
  • Yeah, it's bad practice, for example you will have problems with resizing. – alex2410 Nov 29 '13 at 14:14
  • 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](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). See also [`JTextComponent.write(Writer)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#write%28java.io.Writer%29). – Andrew Thompson Nov 29 '13 at 14:39
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);
    }
});
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.
    }
});
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • 1
    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? – PHPeter Nov 29 '13 at 14:28
  • Yes, to keep separate using interface is good practice. That is possible. – Masudul Nov 29 '13 at 14:32
  • @PHPeter, Go through the tutorial of JDBC. There was a good example of `CoffeesFrame`. Follow that code convention. – Masudul Nov 29 '13 at 14:34