Current position:  Home > Default > 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...

Time:October 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 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:
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);
          JScrollPane jScrollPane2 = new JScrollPane(jtaSQLResult);
          JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
          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));
          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.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) {
          jbtConnectDB1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jbtClearSQLCommand.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jbtClearSQLResult.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
     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 {
               connection = DriverManager.getConnection(url, username, password);
               jlblConnectionStatus2.setText("Connected To Database");
          catch (java.lang.Exception ex) {
     private void executeSQL() {
          if (connection == null) {
               jtaSQLResult.setText("Please connect to a database first");
          else {
               String sqlCommands = jtasqlCommand.getText().trim();
               String[] commands = sqlCommands.replace('\n', ' ').split(";");
               for (String aCommand: commands) {
                    if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
                    else {
     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 ( {
                    row = "";
                    for (int i = 1; i <= columnCount; i++) {
                         row += resultSet.getString(i) + "\t";
                    jtaSQLResult.append(row + '\n');
          catch (SQLException ex) {
     private void processSQLNonSelect(String sqlCommand) {
          try {
               statement = connection.createStatement();
               jtaSQLResult.setText("SQL command executed");
          catch (SQLException ex) {
     public static void main(String[] args) {
          SQLClient applet = new SQLClient();
          JFrame frame = new JFrame();
          frame.setTitle("Interactive SQL Client");
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          frame.setSize(800, 600);
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation((d.width - frame.getSize().width) / 2,
          (d.height - frame.getSize().height) / 2);
}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.
-> 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.

Saving JTextArea's data  into an output file.


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


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


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


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?


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!


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



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


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?


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

JScrollPane will not appear


I'm trying to add a scroll bar to a JTextArea. The textarea appears but there is no scroll bar. Here's what I'm doing. I've included the whole class in case there is something relevant that I don't realise. Any ideas on what I'm doing wrong? import j

Cannot add popup to JTree


Have implemented JTree treePanel using DefaultTreeModel. Created popupMenu. Created JTextArea output. In the end of createPopupMenu() method add following: treePanel.addMouseListener(popupListener); output.addMouseListener(popupListener); Result: Pop

Simple Socket Programming, But,.......


This code is Server, Client Socket program code. Client send data to server and then, Server get data and save it to file in server. I think client works well. But, file have some problem. It generate ClassNotFoundException! So the result

I have  NullPointerException error


Hello, I am new to this forum, so if I make any code-of-conduct blunders please forgive me. I am doing a program in java for an intro to programming class at the University of South Alabama. The objective is to create a program that will manage a fic

Java SubStrings


Hey guyz, I really need some help here on the logic/ algorithm for thiz question...Now I have to identify whether the text that I have typed in the textArea is a java operator or not n extract all the operator found in the textarea...Seems to me like

Invisible JScrollPane?


I have a JList matched up with a JTextArea. Inside the JList I want it to list numbers corresponding to the line in the JTextArea. I have the JList's and JTextArea's JScrollPanes synched up. But, I only want the JTextArea's JScrollPane to show up. Is

JNI printf messages display


Hi, I'm writing a Java application that use native code written in C using JNI. I would to display messages from a printf in a JTextPane o JTextArea. I redirected standard output and error stream but I don't see anything within these components. If I

Scrollpane dosnt added to JTree(DefaultTreeModel)....


Hi, I am creating jtree using DefaultTreeModel, and i am adding to it a JScrollpane...but this isn�t showed in my applet.... when my jtree grows.....this is a code snnipet......has somebody a idea or suggestion?.... Thanks in advance...... Mary Defau

Convert to windowed application-JFrame


I need to convert this program into a windowed application of the subclass JFrame and use Graphics method drawString to output the totals. I know I need to import java awt.* and java. awt.event.* but other than that I need help. Thanks. // Roll a six



I have a class project that the user is to only input 10 numbers. I thought it would be cool to after the 10 numbers where entered to change the Editable field to false. When I did that I get an errors after the 10th number entered. How can I stop th