0

Below is the code of my program. JTable is not getting updated with the output of SQL statement. JTable is not getting populated on Swing GUI.

import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.text.DefaultCaret;

public class GUIFrameExtenderImpl extends JFrame implements ActionListener {

    /**
     * 
     */
        public  static AnimatedLabel busyIcon;
        public  static JLabel busyLabelText;
        public  static Thread busyThread = null;

        public static Connection staticConnection=null;
    private static final long serialVersionUID = 1L;
    static JTable table ;

     static QueryTableModel qtm;

    public static String DATABASE_URL_CONNECTION = null;


    private static JButton executeButton = new JButton("Execute");

    private static JTextField machineName = new JTextField(50);

    static JScrollPane scrollBar = new JScrollPane(table,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    private static JTextField machineNameField = new JTextField(20);

    public static void addComponentsToPane(Container pane) {


        pane.setLayout(null);

        // Create all the labels, combo boxes, buttons, and text fields/areas
        JLabel comboAndTextLabel = new JLabel("Infra Deployer");
        JLabel driverLabel = new JLabel("Machine:");

        //JLabel SQLResultLabel = new JLabel("SQL Execution Result");

        /*JButton clearButton = new JButton("Clear Debug Table");
        JButton clearResultArea = new JButton("Clear Result Window");
        JButton executeButton = new JButton("Run Postscripts");*/

        qtm = new QueryTableModel();
         table =  new JTable(qtm);

                busyIcon=new AnimatedLabel("./images/", 100,12) ;
                busyLabelText=new JLabel("Processing...");

        pane.add(machineName);

        pane.add(comboAndTextLabel);
        pane.add(driverLabel);
        //pane.add(table);


        pane.add(machineNameField);
        pane.add(executeButton);               
                //pane.add(new JLabel("HELLOOOO")) ;
                //pane.add(busyIcon);
                //pane.add(busyLabelText);
                ////busyIcon.setVisible(false);
               // busyLabelText.setVisible(false);

        pane.add(table);
        pane.add(scrollBar);        

        machineName.setEditable(false);

        Insets insets = pane.getInsets();
        // The next giant block of code takes each component and moves them to
        // the
        // desired space on the GUI. It uses absolute positioning
        // This section moves the 5 labels on the top left
        comboAndTextLabel
                .setFont(comboAndTextLabel.getFont().deriveFont(15.0f));
        Dimension size = comboAndTextLabel.getPreferredSize();
        comboAndTextLabel.setBounds(8 + insets.left, 0 + insets.top,
                size.width, size.height + 2);
        size = driverLabel.getPreferredSize();
        driverLabel.setBounds(8 + insets.left, 26 + insets.top, size.width,
                size.height);


        // This section moves the combo boxes and the text fields on the top
        // left
        size = machineName.getPreferredSize();
        machineName.setBounds(90 + insets.left, 22 + insets.top,
                size.width - 257, size.height);




        size = machineNameField.getPreferredSize();
        machineNameField.setBounds(4440 + insets.right, 23 + insets.top,
                size.width - 1, size.height + 3);

        machineNameField.setEditable(false);


        table(8 + insets.left, 17 + insets.bottom,
                size.width + 1500, size.height + 950);

        table.setDragEnabled(false);

        // consoleOutput.setText("asdasdasfdsadfsdfsdfsdfsd");
        size = scrollBar.getPreferredSize();
        scrollBar.setBounds(8 + insets.left, 50 + insets.bottom,
                size.width + 1900, size.height + 890);

        size = executeButton.getPreferredSize();
        executeButton.setBounds(400 + insets.right, 20 + insets.top,
                size.width , size.height );

        // size = scrollingArea.getPreferredSize();
        // scrollingArea.setBounds(8 + insets.left, 217 + insets.bottom,
        // size.width + 769, size.height + 249);

        // puts border around result text area
        //
        // size = clearResultButton.getPreferredSize();
        // clearResultButton.setBounds(15 + insets.left, 820 + insets.top,
        // size.width, size.height);

        java.net.InetAddress localMachine = null;

        try {
            localMachine = java.net.InetAddress.getLocalHost();
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        machineName.setText(localMachine.getHostName());

         executeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                

                    //DataModify dm = new DataModify();
                    //dm.viewwindow();

                  qtm.setHostURL("jdbc:oracle:thin:@indlin2338:1521:OPTABP9");
                  qtm.setQuery("select * from ape3_epcext_offer_Details where offer_id = '8455034'");
//                //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                  System.out.println("starting" + qtm.getColumnCount());

                          }
              });





    }

        private static void table(int i, int j, int k, int l) {
        // TODO Auto-generated method stub

    }

        public static Connection getDBConnection(String driver, String url,String username,String password,boolean isDBConnected)
        {
            Connection con = null;

            if (isDBConnected==true){
                try 
                {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url,username,password);                    
                }
                catch (Exception e) 
                {
                    System.out.println("Exception from getDBConnection()");
                    e.printStackTrace();
                }
            }
            return con;
        }



    // Create the GUI and show it.
    static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Infra Deployer Application 1.0");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Either using insets to shape and define size of frame.
        //Insets insets = frame.getInsets();
//      frame.setSize(1200 + insets.left + insets.right + 240, 850 + insets.top
//              + insets.bottom);

        //We are using Dimension and overriding the frame not to pack and set the size according to end user screen resolution.
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setBounds(0,0,screenSize.width, screenSize.height);
          frame.setVisible(true);
          frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
          //frame.pack(); // Don't pack when using Dimension, pack when using insets

        frame.setResizable(false);

        // Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        // Size and display the window.

        table.revalidate();
        frame.setVisible(true);

        // dispose of window when user quits application (this overrides
        // the default of HIDE_ON_CLOSE)
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // ensure database connection is closed when user quits application
        frame.addWindowListener(new WindowAdapter() {
            // disconnect from database and exit when window has closed
            public void windowClosed(WindowEvent event) {
                //tableModel.disconnectFromDatabase();
                System.exit(0);
            }
        });

    }



    public static void main(String[] args) {


        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `e1.printStackTrace();` So... is there any output? – Andrew Thompson Apr 14 '16 at 13:25
  • `static QueryTableModel qtm;` One quick tip. The use of the `static` on class members is more likely causing problems than fixing them. Remove every `static` from all attributes declared and figure how to do it without using static members. – Andrew Thompson Apr 14 '16 at 13:29
  • `pane.setLayout(null);` Uggh.. 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Apr 14 '16 at 13:30
  • removed static members, still same issue :( – enemyofnone Apr 14 '16 at 13:53
  • 1
    What do you expect us to do? Have you narrowed down the problem? You are using a custom TableModel so we have not idea what it does or if it is working properly. Does the table contain data? Did you display the rows/columns of the table? Did you add the table to a scroll pane? Did you add the scrollpane to the frame? What is the size of the table. – camickr Apr 14 '16 at 14:55
  • `removed static members` - well you haven't updated the code you posted. You also are still using a null layout. fix the layout. – camickr Apr 14 '16 at 14:56
  • For [example](http://stackoverflow.com/a/34742409/230513). – trashgod Apr 14 '16 at 17:21
  • its working after i added the jtable after adding scrollpane :) – enemyofnone Apr 15 '16 at 10:05
  • *"its working.."* Glad you got it sorted. Now it would be best to write up an answer (below) on how you solved it, or simply delete the question. – Andrew Thompson Apr 21 '16 at 08:27

0 Answers0