Current Position:Home > Currently have JTextArea, but would like the output to be in a JTable...

Currently have JTextArea, but would like the output to be in a JTable...

Update:10-11Source: network consolidation
Advertisement
Hi there. I asked this question in the database section, but I ended up getting more questions and separating code, etc. Needless to say, my initial question went unanswered. So, for the purposes of this question, let me get this one thing out of the way:
Yes, I know this is one big file, and that I should have the GUI and DB separated. However, this is for a small project that I'm working on, and for the moment, I'm not too worried about separating class files. As long as the program works as I want it too, so much the better for me.
Now, on to the question at hand. Currently, I have a project that connects to a MySQL DB, and it displays the output from an SQL command in a JTextArea. It looks horribly ugly, as all the columns are not formatted properly. Take a look:
http://img508.imageshack.us/img508/2193/testxe4.jpg
Sure I can see columns, but I would love for the output to be displayed in a neat JTable, which is also much easier on the eyes.
Here is the current code:
package classes;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
public class SQLClient extends JApplet {
     private Connection connection;
     private Statement statement;
     private JTextArea jtasqlCommand = new JTextArea();
     private JTextArea jtaSQLResult = new JTextArea();
     JTextField jtfUsername = new JTextField();
     JPasswordField jpfPassword = new JPasswordField();
     JButton jbtExecuteSQL = new JButton("Execute SQL Command");
     JButton jbtClearSQLCommand = new JButton("Clear");
     JButton jbtConnectDB1 = new JButton("Connect to Database");
     JButton jbtClearSQLResult = new JButton("Clear Result");
     Border titledBorder1 = new TitledBorder("Enter a SQL Command");
     Border titledBorder2 = new TitledBorder("SQL Execution Result");
     Border titledBorder3 = new TitledBorder("Enter Database Information");
     JLabel jlblConnectionStatus1 = new JLabel("");
     JLabel jlblConnectionStatus2 = new JLabel("Not Connected");
     public void init() {
          JScrollPane jScrollPane1 = new JScrollPane(jtasqlCommand);
          jScrollPane1.setBorder(titledBorder1);
          JScrollPane jScrollPane2 = new JScrollPane(jtaSQLResult);
          jScrollPane2.setBorder(titledBorder2);
          JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
          jPanel1.add(jbtClearSQLCommand);
          jPanel1.add(jbtExecuteSQL);
          JPanel jPanel2 = new JPanel();
          jPanel2.setLayout(new BorderLayout());
          jPanel2.add(jScrollPane1, BorderLayout.CENTER);
          jPanel2.add(jPanel1, BorderLayout.SOUTH);
          jPanel2.setPreferredSize(new Dimension(100, 100));
          JPanel jPanel3 = new JPanel();
          jPanel3.setLayout(new BorderLayout());
          jPanel3.add(jlblConnectionStatus1, BorderLayout.CENTER);
          jPanel3.add(jbtConnectDB1, BorderLayout.EAST);
          JPanel jPanel4 = new JPanel();
          jPanel4.setLayout(new GridLayout(4, 1, 10, 5));
          jPanel4.add(jtfUsername);
          jPanel4.add(jpfPassword);
          JPanel jPanel5 = new JPanel();
          jPanel5.setLayout(new GridLayout(4, 1));
          jPanel5.add(new JLabel("Username"));
          jPanel5.add(new JLabel("Password"));
          JPanel jPanel6 = new JPanel();
          jPanel6.setLayout(new BorderLayout());
          jPanel6.setBorder(titledBorder3);
          jPanel6.add(jPanel4, BorderLayout.CENTER);
          jPanel6.add(jPanel5, BorderLayout.WEST);
          JPanel jPanel7 = new JPanel();
          jPanel7.setLayout(new BorderLayout());
          jPanel7.add(jPanel3, BorderLayout.SOUTH);
          jPanel7.add(jPanel6, BorderLayout.CENTER);
          JPanel jPanel8 = new JPanel();
          jPanel8.setLayout(new BorderLayout());
          jPanel8.add(jPanel2, BorderLayout.CENTER);
          jPanel8.add(jPanel7, BorderLayout.WEST);
          JPanel jPanel9 = new JPanel();
          jPanel9.setLayout(new BorderLayout());
          jPanel9.add(jlblConnectionStatus2, BorderLayout.EAST);
          jPanel9.add(jbtClearSQLResult, BorderLayout.WEST);
          this.add(jPanel8, BorderLayout.NORTH);
          this.add(jScrollPane2, BorderLayout.CENTER);
          this.add(jPanel9, BorderLayout.SOUTH);
          jbtExecuteSQL.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          executeSQL();
          jbtConnectDB1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          connectToDB();
          jbtClearSQLCommand.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jtasqlCommand.setText(null);
          jbtClearSQLResult.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jtaSQLResult.setText(null);
     private void connectToDB() {
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost:3306/petstore2002";
          String username = jtfUsername.getText().trim();
          String password = new String(jpfPassword.getPassword());
          try {
               Class.forName(driver);
               connection = DriverManager.getConnection(url, username, password);
               jlblConnectionStatus2.setText("Connected To Database");
          catch (java.lang.Exception ex) {
               ex.printStackTrace();
     private void executeSQL() {
          if (connection == null) {
               jtaSQLResult.setText("Please connect to a database first");
               return;
          else {
               String sqlCommands = jtasqlCommand.getText().trim();
               String[] commands = sqlCommands.replace('\n', ' ').split(";");
               for (String aCommand: commands) {
                    if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
                         processSQLSelect(aCommand);
                    else {
                         processSQLNonSelect(aCommand);
     private void processSQLSelect(String sqlCommand) {
          try {
               statement = connection.createStatement();
               ResultSet resultSet = statement.executeQuery(sqlCommand);
               int columnCount = resultSet.getMetaData().getColumnCount();
               String row = "";
               for (int i = 1; i <= columnCount; i++) {
                    row += resultSet.getMetaData().getColumnName(i) + "\t";
               jtaSQLResult.append(row + '\n');
               while (resultSet.next()) {
                    row = "";
                    for (int i = 1; i <= columnCount; i++) {
                         row += resultSet.getString(i) + "\t";
                    jtaSQLResult.append(row + '\n');
          catch (SQLException ex) {
               jtaSQLResult.setText(ex.toString());
     private void processSQLNonSelect(String sqlCommand) {
          try {
               statement = connection.createStatement();
               statement.executeUpdate(sqlCommand);
               jtaSQLResult.setText("SQL command executed");
          catch (SQLException ex) {
               jtaSQLResult.setText(ex.toString());
     public static void main(String[] args) {
          SQLClient applet = new SQLClient();
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle("Interactive SQL Client");
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          applet.init();
          applet.start();
          frame.setSize(800, 600);
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation((d.width - frame.getSize().width) / 2,
          (d.height - frame.getSize().height) / 2);
          frame.setVisible(true);
}Right now it works fine, and I am planning on using this as an initial prototype for the presentation. Can I get some help on getting the output displayed into a JTable? How would I go abouts doing the conversion? I'm fairly new to Java (taking a course on it), and I'm worried that if I change one line of code the whole thing will get ruined and I'll need to start from scratch.

The Best Answer

Advertisement
-> It sounds like you're getting annoyed with the amount of people asking perfectly legitimate questions
Your expectations are not legitimate. We attempt to understand your problem and give you the resources to solve the problem. Many times those resources are in the form of a tutorial or a reference to the API. We are not here to write code for you.
I pointed you in the right direction. Its up to you do to some learning on your own. You have not made the slightest effort to understand how to use a JTable. You have not made the slightest effort to search the forum to see if you question has been asked before (it has).
Your attitude and effort determines how much effort we make. Frankly you have made no effort whatsoever to solve you problem so as far as I am concerned you are on you own. Apparently I am not the only one who thinks you should be making more of an effort based on all the other advice you have received.
  • Currently have JTextArea, but would like the output to be in a JTable... Update:10-11

    Hi there. I asked this question in the database section, but I ended up getting more questions and separating code, etc. Needless to say, my initial question went unanswered. So, for the purposes of this question, let me get this one thing out of the

  • Saving JTextArea's data  into an output file. Update:11-30

    What is the implementation code i should add into save button in order to get all the contents of my JTextArea and send them in an external file ?What is the implementation code i should add into save button in order to get all the contents of my JTe

  • Output Resultset in JTextArea Update:11-30

    Hi, I have encountered a problem which catch the sql exception... when i try to display my selected data in a jTextArea of another tab pane. displayResultsQ(res); when this method is called it goes to exception and couldnt go thru to display my resul

  • Very slow JTextArea updates even as separate thread Update:11-30

    Hi, I am trying to report output of a dynamic stream as a JFrame that contains JTextArea. The module is part of my big SWING application. The problem is JTextArea only shows update after the stream is empty. I have tried the following things * Stream

  • How to add JTextArea in JTable Update:11-30

    Hi.. I want to add JTextArea in my table in one of the column of JTable. I have already written a code which implements TablecellEditor and TableCellRenderer. I have already succeeded to set textarea in one cell for multiple line but when i go to ano

  • How to write code to print and save the output in my GUI? Update:11-30

    I had been searching on code to program print and save commands to print and save the output from the GUI but to no avail. Can someone help me?The output will be link from the previous GUI page. Hence the output is a page with Jtable and a button for

  • How to send string data through socket! Update:11-24

    Is there any method to send string data over socket. and if client send string data to server, How to get that data in server? Comments please!Thank for your kind answer, stoopidboi. I solved the ploblem. ^^; I open the source code ^^; wow~~~~~! It w

  • SQL (JDBC and JAVA) Update:11-30

    Hi I have have problem in generating a results from my SQL command. Apparently the query run successfully but it fails to oick up the data in the table for display. Please find the code below:- public class QueryTableModel extends AbstractTableModel

  • Is there a way to clear the shell Update:11-30

    hi there, i am programming a game. since now i want to output the gameloop data with a shell. is there some way of clearing the shell? i would like to clear the shell and then output the data onto an emty shell? thxWhy not just create another JFrame

  • Redirecting stdout of third party API? Update:11-30

    Good Morning (or afternoon), I have an interesting problem with an application that I am writing. I am using my own classes, as well as a third party package that somebody developed, in this application. I have developed a GUI for ease of use and in