0

My code is working when I directly use jtable, but when I bound it with scroll panel, it runs one time. After another click on button it hides the data. Here is the code:

import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.mysql.jdbc.Statement;

class Scrl extends JFrame implements ActionListener 
{
    JScrollPane sp;
    Icon back,front;
    JLabel l,l1;
    JTable tb=new JTable();
    DefaultTableModel model;
    String column[]={"JobcardNo","Customer Name","Brand","Date","PhoneNo"};
    JTextField jobt=new JTextField("");
    JButton b=new JButton("click");


    public Scrl()

    {
        b.setBounds(200,140,100,20);
        jobt.setBounds(114,50,100,20);
        model = new DefaultTableModel();
        tb.setModel(model); 
        model.setColumnIdentifiers(column);
        add(jobt);
        add(b);


        b.addActionListener(this);


        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        setLayout(null);
        setSize(700,600);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==b)
        {
            String job_no=jobt.getText();

            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                Connection    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ac_data","root","root");
                java.sql.Statement st=con.createStatement();
                ResultSet rs=st.executeQuery("select * from main");
                while(rs.next())
                {
                    String hey=rs.getString(1);
                    if(hey.equals(job_no))
                    {
                         System.out.println(rs.getString(2));
                        String jobno=rs.getString(1);
                        String names=rs.getString(2);
                        String brand=rs.getString(21);
                        String date=rs.getString(6);
                        String phoneno=rs.getString(4);
                        model.addRow(new Object[]{jobno,names,brand,date,phoneno});                         
                    }
                }   
            }

            catch(Exception ee)
            {                   
            }

        }
        JScrollPane sp=new JScrollPane(tb);
        sp.setBounds(1,165,687,440);
        add(sp);
    }
    public static void main(String[] args) 
    {
        new Scrl();
    }
}

Scroll panel is not refreshing data itslef.

Jojo
  • 1,875
  • 3
  • 29
  • 29
Davin
  • 91
  • 1
  • 3
  • 9
  • 1. disadvantage of NullLayout is that childs aren't resizable with container/parent, 2. disadvantage of NullLayout is that dynamic JComponents doesn't works propetly when they are the slave of setBounds – mKorbel May 30 '14 at 11:26
  • setLocationRelativeTo(null); should be before setVisible(true); – mKorbel May 30 '14 at 11:26
  • model.addRow(new Object[]{jobno,names,brand,date,phoneno}); should be wrapped into invokeLater, maybe easier will to create a new 2d array and add this array to DefaultTableModel, or to create a new model and add to JTable, btw you never to resets old model, then a new rows are appended to the end of model/JTable – mKorbel May 30 '14 at 11:29
  • 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). – Andrew Thompson May 30 '14 at 11:32

2 Answers2

2
  1. You don't close the Connection when you're finished.

  2. You should read the information from your database once, before you construct the GUI, and put the information into a TableModel.

  3. You're trying to place all of the Swing components by hand when you should be using a layout manager.

  4. You're not putting your Swing components on the Event Dispatch thread.

That should be enough reading and studying to keep you busy for a while.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

The reason why it's not showing is that you're instantiating a new JScrollPane every single time your click the button. Instead, you should only instantiate it once somewhere it won't be re-created every time (ie: in your constructor).

Move the following to the end of your constructor:

JScrollPane sp=new JScrollPane(tb);
sp.setBounds(1,165,687,440);

Also, do not leave your catch exception empty because you will never know if your program is throwing an exception or not. Add the following to the catch exception clause:

ee.printStackTrace();

and deal with any errors that it prints out.

David Yee
  • 3,515
  • 25
  • 45