File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes jtable not refreshing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jtable not refreshing" Watch "jtable not refreshing" New topic
Author

jtable not refreshing

aleem baig
Greenhorn

Joined: Dec 15, 2008
Posts: 6
i am using a jtable that is created dynamically based on selection of a menu item in the menubar. i am able to get the table properly. the second time i select a menu item i am able to get the new table correctly but the problem here is whenever i am clicking on the new table data, the table data is changed to old data which i got for the first time. And also whenever i am minimizing the window i am getting only the old data. that is whatever i select for the first time when the application starts is get catched for the whole application.
hope you all understand my problem......i am unable to post the code as it takes some queries which gets dynamical data...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i am unable to post the code as it takes some queries which gets dynamical data...

well, load it with hard-coded data, and if it displays the same problem, post that code.
aleem baig
Greenhorn

Joined: Dec 15, 2008
Posts: 6
hi As per Michael Dunn i am sending an example of dynamic generation of tables from our oracle database. please go through the code and tell me where i am wrong.
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
public class ContentPanelDemo extends JPanel implements ActionListener{
private JPanel reportsPanel;
private static JMenuBar menuBar;
private String submenu;
private static JMenu tables,objects,users;
private static JMenuItem m,menuItem,ex1;
JTabbedPane tabPane;
JTextArea jtAreaOutput,textArea;
JTable jtable;
Vector v=new Vector();
ResultSet rs;
Statement stmt;
Connection con;
public ContentPanelDemo()
{
initFields();
setReportsPanel();
setTabbedPanels();
}
public void initFields() {
try {
menuBar = new JMenuBar();
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
objects = new JMenu("Objects");
objects.setMnemonic(KeyEvent.VK_O);
menuBar.add(objects);
tables = new JMenu("tab");
objects.add(tables);
getQuery(tables);
users = new JMenu("Users");
users.setMnemonic(KeyEvent.VK_U);
ex1 = new JMenuItem("Example1");
ex1.addActionListener(this);
users.add(ex1);
menuBar.add(users);
}
catch(Exception e){}
}
public void getQuery(JMenu menu)
{ try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc racle:thin:@localhost:1521 rcl","scott","tiger");
Statement stmt=con.createStatement();
System.out.println("connected "+con.getClass());
ResultSet rs=stmt.executeQuery("select * from "+menu.getText());
while(rs.next()){
String name = rs.getString("tname");
m = new JMenuItem(name);
menu.add(m);
m.addActionListener(this);
}
}catch(Exception e){}
}
public void actionPerformed(ActionEvent event){
JMenuItem source=(JMenuItem)event.getSource();
submenu=source.getText();
JScrollPane jp=null;;
JTextArea area=new JTextArea(5,40);
if(submenu.equalsIgnoreCase("Example1")){
area.setVisible(true);
area.setLineWrap(true);
area.setText("hello");
jp=new JScrollPane(area);
JPanel p=new JPanel();
p.add(jp);
reportsPanel.add(p);
repaint();
}
else
{
PopulationRunnable populationRunnable = new PopulationRunnable();
Thread populationThread = new Thread(populationRunnable);
populationThread.start();
}
}
public void setReportsPanel()
{
reportsPanel = new JPanel();
reportsPanel.setBackground(Color.WHITE);
reportsPanel.setLayout(new BorderLayout());
reportsPanel.add(menuBar,BorderLayout.NORTH);
}
private void setTabbedPanels()
{

tabPane=new JTabbedPane();
tabPane.setBackground(Color.LIGHT_GRAY);
tabPane.setFont(new Font("Arial",Font.PLAIN,18));
tabPane.addTab("Reports",reportsPanel);
add(tabPane);
}

public static void main(String[] args)
{ SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("Building the frame");
JFrame frmFrame = new JFrame();
ContentPanelDemo cPanel = new ContentPanelDemo();
frmFrame.setContentPane(cPanel);
frmFrame.setSize(new Dimension(650, 400));
frmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFrame.setVisible(true);
frmFrame.show();
}
});
}
private static void setJMenuBar(JMenuBar menuBar2) {
// TODO Auto-generated method stub

}
private class PopulationRunnable implements Runnable {
public void run() {

JPanel totalGUI = new JPanel();
Vector data=new Vector();
JTable table=null;
String[] title = {"Object Id", "Object Name"};
Vector vtitle=new Vector();
vtitle.addElement("Object Id");
vtitle.addElement("Object Name");
try{

String queryString="select * from "+submenu;
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(queryString);
Vector vdata;
Vector cdata=new Vector();
String id=null;
String name=null,type=null;
while(rs.next()){
id=rs.getString(1);
name=rs.getString(2);
vdata=new Vector();
vdata.addElement(id);
vdata.addElement(name);

data.addElement(vdata);
}
DefaultTableModel model = new DefaultTableModel(data,vtitle);
table=new JTable(model){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
System.out.println("size of data vector"+data.size());
JScrollPane scrollPane = JTable.createScrollPaneForTable(table);
scrollPane.setPreferredSize(new Dimension(600,400));
reportsPanel.add(scrollPane,BorderLayout.CENTER);
reportsPanel.repaint();
reportsPanel.revalidate();
}
catch(Exception e){e.printStackTrace();}
}
}
}


please provide the oracle class,username and password as per your system settings and run the code. in this code if i get the contents of emp table first then click on dept table then i am able to get the contents of dept table but when i click or scroll the table i am getting the contents of emp table only. this happens if i minimize or shuffle the windows. another thing i would like to know that when i click on Example1 menuitem i should get another panel where i can add some components as per my requirements. hope you understand......
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Please Use Code Tags. You can edit your post by clicking the button.

I also advise you to check the "disable smileys" check box.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by aleem baig:
hi As per Michael Dunn i am sending an example of dynamic generation of tables from our oracle database. please go through the code and tell me where i am wrong.


That's not what Mr. Dunn suggested. He suggested that you post code where you have replaced the dynamic generation SQL stuff with hard-coded strings (for example, "a", "b", "c", ...) so we can actually help you. I suggest you follow his advice. (Also follow Mr. Prime's advice to use code tags.)

While I'm here, I'll make a few comments.


DefaultTableModel model = new DefaultTableModel(data,vtitle);
table=new JTable(model){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
System.out.println("size of data vector"+data.size());
JScrollPane scrollPane = JTable.createScrollPaneForTable(table);


I think I would prefer to override the isCellEditable() method of the model, not of the table, like this:
It works fine either way, so I guess I'm just being picky, but I think it's cleaner this way.

Also, the createScrollPaneForTable() method has been deprecated. It was the right thing to do way back in the stone age, but for the last decade or so the correct approach is to just use JScrollPane's constructor:
JScrollPane scrollPane = new JScrollPane(table);


please provide the oracle class,username and password as per your system settings and run the code.


Do you really expect people to go to that much trouble just to help you with your little problem? While people here are kind and generous not too many are that generous, especially when there are other less-demanding people asking for help.


bitguru blog
aleem baig
Greenhorn

Joined: Dec 15, 2008
Posts: 6
I am sorry if I have made any mistake in posting my problem here. As I am using forums for the first time I am not well versed with the "Do's" and "Dont's". Actually I am keen about my task so asked the people over here to help me out if they can. I know that no one is here to help me in providing the whole code but I gave the code to correct me.
i had overridden isCellEditable() method. still the same problem.....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I am sorry if I have made any mistake in posting my problem here.

posting your problem here is the right thing to do, but if you want a solution
to the problem we need to be able to copy/paste/compile/run your code so we
can see exactly what the problem is. We can't do that because we cannot access
your database, so we suggested you just use hard-coded data
i.e. instead of this

while(rs.next()){
id=rs.getString(1);
name=rs.getString(2);
vdata=new Vector();
vdata.addElement(id);
vdata.addElement(name);
data.addElement(vdata);
}

just manually add some elements to the vectors.
if the problem still occurs, post the code, along with the steps to produce the problem.
aleem baig
Greenhorn

Joined: Dec 15, 2008
Posts: 6
hi i am adding the hard code here as you said....correct me please where I am wrong.
you can directly copy/paste/compile and run....
here is the code....
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
public class ContentPanelDemo extends JPanel implements ActionListener{
private JPanel reportsPanel;
private static JMenuBar menuBar;
private String submenu;
private static JMenu tables,objects,users;
private static JMenuItem m,menuItem,ex1,firstMI,secondMI;
JTabbedPane tabPane;
JTextArea jtAreaOutput,textArea;
JTable jtable;
Vector v=new Vector();
ResultSet rs;
Statement stmt;
Connection con;
public ContentPanelDemo()
{
initFields();
setReportsPanel();
setTabbedPanels();
}
public void initFields() {
try {
menuBar = new JMenuBar();
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
objects = new JMenu("Objects");
objects.setMnemonic(KeyEvent.VK_O);
menuBar.add(objects);
firstMI = new JMenuItem("First");
firstMI.addActionListener(this);
objects.add(firstMI);
secondMI = new JMenuItem("second");
secondMI.addActionListener(this);
objects.add(secondMI);
users = new JMenu("Users");
users.setMnemonic(KeyEvent.VK_U);
ex1 = new JMenuItem("Example1");
ex1.addActionListener(this);
users.add(ex1);
menuBar.add(users);
}
catch(Exception e){}
}

public void actionPerformed(ActionEvent event){
JMenuItem source=(JMenuItem)event.getSource();
submenu=source.getText();
JScrollPane jp=null;;
JTextArea area=new JTextArea(5,40);
if(submenu.equalsIgnoreCase("Example1")){
area.setVisible(true);
area.setLineWrap(true);
area.setText("hello");
jp=new JScrollPane(area);
JPanel p=new JPanel();
p.add(jp);
reportsPanel.add(p);
repaint();
}
else
{
PopulationRunnable populationRunnable = new PopulationRunnable();
Thread populationThread = new Thread(populationRunnable);
populationThread.start();
}
}
public void setReportsPanel()
{
reportsPanel = new JPanel();
reportsPanel.setBackground(Color.WHITE);
reportsPanel.setLayout(new BorderLayout());
reportsPanel.add(menuBar,BorderLayout.NORTH);
}
private void setTabbedPanels()
{

tabPane=new JTabbedPane();
tabPane.setBackground(Color.LIGHT_GRAY);
tabPane.setFont(new Font("Arial",Font.PLAIN,18));
tabPane.addTab("Reports",reportsPanel);
add(tabPane);
}

public static void main(String[] args)
{
System.out.println("Building the frame");
JFrame frmFrame = new JFrame();
ContentPanelDemo cPanel = new ContentPanelDemo();
frmFrame.setContentPane(cPanel);
frmFrame.setSize(new Dimension(650, 400));
frmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFrame.setVisible(true);
frmFrame.validate();
frmFrame.show();
}
public void windowDeiconified(WindowEvent e) {
repaint();

}
private static void setJMenuBar(JMenuBar menuBar2) {
// TODO Auto-generated method stub
}
private class PopulationRunnable implements Runnable {
public void run() {
JPanel totalGUI = new JPanel();
Vector data=new Vector();
JTable table=null;
String[] title = {"Object Id", "Object Name"};
Vector vtitle=new Vector();
vtitle.addElement("Object Id");
vtitle.addElement("Object Name");
Vector vdata;
Vector cdata=new Vector();
String id=null;
String name=null,type=null;

try{
if(submenu.equalsIgnoreCase("first"))
{
System.out.println("hello");
for(int i=0;i<10;i++)
{
id="id1";
name="name1";
vdata=new Vector();
vdata.addElement(id);
vdata.addElement(name);
data.addElement(vdata);
}
}
else if(submenu.equalsIgnoreCase("second"))
{
for(int i=0;i<15;i++)
{
id="id2";
name="name2";
vdata=new Vector();
vdata.addElement(id);
vdata.addElement(name);
data.addElement(vdata);
}
}
DefaultTableModel model = new DefaultTableModel(data,vtitle){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};

table=new JTable(model);
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.repaint();
table.revalidate();
System.out.println("size of data vector"+data.size());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(600,400));
reportsPanel.add(scrollPane,BorderLayout.CENTER);
reportsPanel.repaint();
reportsPanel.revalidate();
}

catch(Exception e){e.printStackTrace();}

}
}
}

if i select "First" menu item in the above program i am getting 10 rows and if i select "second" menu item then i get 15 rows. but when i click on table or minimize the window i am taking back to the 10 rows which i selected for the first time. please correct me ........
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
too many problems to try and fix/explain (mainly in the runnable)

here's a rewrite (leaving the concept of the runnable to query the db in a separate thread)

see if you can follow the changes



note the difference in readability when code tags are used
aleem baig
Greenhorn

Joined: Dec 15, 2008
Posts: 6
Thanks for the reply..................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jtable not refreshing
 
Similar Threads
JTable
JTable(!!!)
JTable
JTable
JTable Problem ....Help!