jscrollpane - Scroll Pane is visible but doesn't work when dynamically updating rows in java table -
i have java table update rows dynamically. here example created. mimics actual program line line creating table , adding scrollpane. actual program huge , sorry can't share it.
import java.awt.*; import java.awt.event.*; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import javax.swing.*; import javax.swing.table.defaulttablemodel; class simpletableexample extends jframe { private jpanel toppanel; private jtable table; private jscrollpane scrollpane; private defaulttablemodel model = null; private jpanel pmainmenu = null; public simpletableexample() { pmainmenu = new jpanel(); borderlayout thislayout = new borderlayout(); pmainmenu.setlayout(thislayout); { jpanel pbuttons = new jpanel(); pmainmenu.add(pbuttons, borderlayout.north); gridbaglayout pbuttonslayout = new gridbaglayout(); pbuttonslayout.columnwidths = new int[]{7, 7, 7}; pbuttonslayout.rowheights = new int[]{25}; pbuttonslayout.columnweights = new double[]{0.1, 0.1, 0.1}; pbuttonslayout.rowweights = new double[]{0.1}; { jbutton btnew = new jbutton(); pbuttons.add(btnew, new gridbagconstraints(2, 0, 1, 1, 0.0, 0.0, gridbagconstraints.center, gridbagconstraints.none, new insets(0, 5, 0, 0), 0, 0)); btnew.settext("<html><center>run</center><center>configuration</center></html>"); btnew.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { btrunconfiguration(evt); } }); } } toppanel = new jpanel(); toppanel.setlayout( new borderlayout() ); { model = new defaulttablemodel(); table = new jtable(model); scrollpane = new jscrollpane( table ); toppanel.add( scrollpane, borderlayout.center ); model.addcolumn(" "); model.addcolumn(" "); model.addcolumn(" "); } getcontentpane().add(pmainmenu); getcontentpane().repaint(); pack(); setsize(520, 130); setlocationrelativeto(null); setvisible(true); } private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); setvisible(true); string columnnames[] = { "column 1", "column 2", "column 3" }; for(int i=0;i<300;i++) { try { thread.sleep(1000); } catch(interruptedexception ie) { // ignore! ie.printstacktrace(); } model.addrow(new object[]{i,i,i}); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); scheduledexecutorservice s = executors.newsinglethreadscheduledexecutor(); s.schedule(new runnable() { public void run() { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }, 500, timeunit.milliseconds); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ } } public static void main( string args[] ) { // create instance of test application simpletableexample mainframe = new simpletableexample(); mainframe.setvisible( true ); } }
scroll pane doesn't work. can please help. thanks
***************************new code***************************************
import java.awt.*; import java.awt.event.*; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import javax.swing.*; import javax.swing.table.defaulttablemodel; class simpletableexample extends jframe { private jpanel toppanel; private jtable table; private jscrollpane scrollpane; private defaulttablemodel model = null; private jpanel pmainmenu = null; public simpletableexample() { pmainmenu = new jpanel(); borderlayout thislayout = new borderlayout(); pmainmenu.setlayout(thislayout); { jpanel pbuttons = new jpanel(); pmainmenu.add(pbuttons, borderlayout.north); gridbaglayout pbuttonslayout = new gridbaglayout(); pbuttonslayout.columnwidths = new int[]{7, 7, 7}; pbuttonslayout.rowheights = new int[]{25}; pbuttonslayout.columnweights = new double[]{0.1, 0.1, 0.1}; pbuttonslayout.rowweights = new double[]{0.1}; { jbutton btnew = new jbutton(); pbuttons.add(btnew, new gridbagconstraints(2, 0, 1, 1, 0.0, 0.0, gridbagconstraints.center, gridbagconstraints.none, new insets(0, 5, 0, 0), 0, 0)); btnew.settext("<html><center>run</center><center>configuration</center></html>"); btnew.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { btrunconfiguration(evt); } }); } } toppanel = new jpanel(); toppanel.setlayout( new borderlayout() ); { model = new defaulttablemodel(); table = new jtable(model); scrollpane = new jscrollpane( table ); toppanel.add( scrollpane, borderlayout.center ); model.addcolumn(" "); model.addcolumn(" "); model.addcolumn(" "); } getcontentpane().add(pmainmenu); getcontentpane().repaint(); pack(); setsize(520, 130); setlocationrelativeto(null); setvisible(true); } private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); setvisible(true); string columnnames[] = { "column 1", "column 2", "column 3" }; for(int i=0;i<300;i++) { try { thread.sleep(1000); } catch(interruptedexception ie) { // ignore! ie.printstacktrace(); } new myworker(model).execute(); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); actionlistener timeraction = new actionlistener() { @override public void actionperformed(actionevent ae) { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }; timer timer = new timer(500, timeraction); timer.start(); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ } } class myworker extends swingworker<void, void> { defaulttablemodel model; myworker(defaulttablemodel model){ this.model=model; } @override public void doinbackground() { model.addrow(new object[]{1,1,1}); return null; } @override protected void done() { } } public static void main( string args[] ) { // create instance of test application simpletableexample mainframe = new simpletableexample(); mainframe.setvisible( true ); }}
*************************************new code 2 timer problem*******************
import java.awt.*; import java.awt.event.*; import java.io.ioexception; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import javax.swing.*; import javax.swing.table.defaulttablemodel; class simpletableexample extends jframe { private jpanel toppanel; private jtable table; private jscrollpane scrollpane; private defaulttablemodel model = null; private jpanel pmainmenu = null; private timer timer = null; public simpletableexample() { pmainmenu = new jpanel(); borderlayout thislayout = new borderlayout(); pmainmenu.setlayout(thislayout); { jpanel pbuttons = new jpanel(); pmainmenu.add(pbuttons, borderlayout.north); gridbaglayout pbuttonslayout = new gridbaglayout(); pbuttonslayout.columnwidths = new int[]{7, 7, 7}; pbuttonslayout.rowheights = new int[]{25}; pbuttonslayout.columnweights = new double[]{0.1, 0.1, 0.1}; pbuttonslayout.rowweights = new double[]{0.1}; { jbutton btnew = new jbutton(); pbuttons.add(btnew, new gridbagconstraints(2, 0, 1, 1, 0.0, 0.0, gridbagconstraints.center, gridbagconstraints.none, new insets(0, 5, 0, 0), 0, 0)); btnew.settext("<html><center>run</center><center>configuration</center></html>"); btnew.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { btrunconfiguration(evt); } }); } } toppanel = new jpanel(); toppanel.setlayout( new borderlayout() ); { model = new defaulttablemodel(); table = new jtable(model); scrollpane = new jscrollpane( table ); toppanel.add( scrollpane, borderlayout.center ); model.addcolumn(" "); model.addcolumn(" "); model.addcolumn(" "); } getcontentpane().add(pmainmenu); getcontentpane().repaint(); pack(); setsize(520, 130); setlocationrelativeto(null); setvisible(true); string columnnames[] = { "column 1", "column 2", "column 3" }; //new myworker().execute(); } private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); showonscreen(1,this); setvisible(true); new myworker().execute(); } class myworker extends swingworker<void, void> { @override public void doinbackground() { actionlistener taskperformer = new actionlistener() { @override public void actionperformed(actionevent e) { (int = 0; < 300; i++) { try { runtime rt = runtime.getruntime(); string url = "http://stackoverflow.com"; rt.exec("rundll32 url.dll,fileprotocolhandler " + url); thread.sleep(1000); } catch (interruptedexception ie) { // ignore! ie.printstacktrace(); } catch (ioexception le) { le.printstacktrace(); } model.addrow(new object[]{i, i, i}); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); scheduledexecutorservice s = executors.newsinglethreadscheduledexecutor(); s.schedule(new runnable() { public void run() { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }, 500, timeunit.milliseconds); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ } } }; timer = new timer(20, taskperformer); timer.start(); return null; } @override protected void done() { } } public static void main( string args[] ) { // create instance of test application simpletableexample mainframe = new simpletableexample(); mainframe.setvisible( true ); } public void showonscreen( int screen, jframe frame ) { graphicsenvironment ge = graphicsenvironment.getlocalgraphicsenvironment(); graphicsdevice[] gd = ge.getscreendevices(); if( screen > 0 && screen < 2 ) { int x = frame.getwidth()/2; int y = frame.getheight()/2; frame.setlocation((gd[screen].getdefaultconfiguration().getbounds().width * 3/2) - x, (gd[screen].getdefaultconfiguration().getbounds().height *1/2) - y); // jframe dummy = new jframe(gd[screen].getdefaultconfiguration()); // setlocationrelativeto(dummy); // dummy.dispose(); } else if( gd.length > 0 ) { frame.setlocationrelativeto(null); } else { throw new runtimeexception( "no screens found" ); } } }
always use swing threads when interacting swing components.try below solution works
import java.awt.*; import java.awt.event.*; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import javax.swing.*; import javax.swing.table.defaulttablemodel; class simpletableexample extends jframe { private jpanel toppanel; private jtable table; private jscrollpane scrollpane; private defaulttablemodel model = null; private jpanel pmainmenu = null; public simpletableexample() { pmainmenu = new jpanel(); borderlayout thislayout = new borderlayout(); pmainmenu.setlayout(thislayout); { jpanel pbuttons = new jpanel(); pmainmenu.add(pbuttons, borderlayout.north); gridbaglayout pbuttonslayout = new gridbaglayout(); pbuttonslayout.columnwidths = new int[]{7, 7, 7}; pbuttonslayout.rowheights = new int[]{25}; pbuttonslayout.columnweights = new double[]{0.1, 0.1, 0.1}; pbuttonslayout.rowweights = new double[]{0.1}; { jbutton btnew = new jbutton(); pbuttons.add(btnew, new gridbagconstraints(2, 0, 1, 1, 0.0, 0.0, gridbagconstraints.center, gridbagconstraints.none, new insets(0, 5, 0, 0), 0, 0)); btnew.settext("<html><center>run</center><center>configuration</center></html>"); btnew.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { btrunconfiguration(evt); } }); } } toppanel = new jpanel(); toppanel.setlayout( new borderlayout() ); { model = new defaulttablemodel(); table = new jtable(model); scrollpane = new jscrollpane( table ); toppanel.add( scrollpane, borderlayout.center ); model.addcolumn(" "); model.addcolumn(" "); model.addcolumn(" "); } getcontentpane().add(pmainmenu); getcontentpane().repaint(); pack(); setsize(520, 130); setlocationrelativeto(null); setvisible(true); } private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); setvisible(true); string columnnames[] = { "column 1", "column 2", "column 3" }; new myworker(model).execute(); } class myworker extends swingworker<void, void> { defaulttablemodel model; myworker(defaulttablemodel model){ this.model=model; } @override public void doinbackground() { for(int i=0;i<300;i++) { try { thread.sleep(1000); } catch(interruptedexception ie) { // ignore! ie.printstacktrace(); } model.addrow(new object[]{i,i,i}); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); actionlistener timeraction = new actionlistener() { @override public void actionperformed(actionevent ae) { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }; timer timer = new timer(500, timeraction); timer.start(); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ } return null; } @override protected void done() { } } public static void main( string args[] ) { // create instance of test application simpletableexample mainframe = new simpletableexample(); mainframe.setvisible( true ); }}
update according problem updated design.you have make sure loop needs run through swing thread.for example if have method of update table
private void updatetable(){ for(int i=0;i<300;i++) { try { thread.sleep(1000); } catch(interruptedexception ie) { // ignore! ie.printstacktrace(); } model.addrow(new object[]{i,i,i}); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); scheduledexecutorservice s = executors.newsinglethreadscheduledexecutor(); s.schedule(new runnable() { public void run() { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }, 500, timeunit.milliseconds); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ }}
make sure call swingworker
private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); setvisible(true); new myworker().execute(); } class myworker extends swingworker<void, void> { @override public void doinbackground() { updatetable(); return null; } @override protected void done() { } }
according update 2
private void btrunconfiguration(actionevent evt) { getcontentpane().removeall(); getcontentpane().add( toppanel ); getcontentpane().repaint(); settitle("simple table application"); setsize(300, 100); setbackground(color.gray); pack(); showonscreen(1,this); setvisible(true); final atomicboolean flag = new atomicboolean(false); actionlistener taskperformer = new actionlistener() { @override public void actionperformed(actionevent e) { if(!flag.get()){ flag.set(true); new myworker(flag).execute(); //flag.set(false); } } }; timer timer = new timer(20, taskperformer); timer.setrepeats(true); timer.start(); } class myworker extends swingworker<void, void> { atomicboolean flag; myworker(atomicboolean flag){ this.flag=flag; } @override public void doinbackground() { (int = 0; < 300; i++) { try { runtime rt = runtime.getruntime(); string url = "http://stackoverflow.com"; rt.exec("rundll32 url.dll,fileprotocolhandler " + url); thread.sleep(1000); } catch (interruptedexception ie) { // ignore! ie.printstacktrace(); } catch (exception le) { le.printstacktrace(); } model.addrow(new object[]{i, i, i}); /**************this message dialog box show row updated refresh table viewing***********/ //must schedule close before dialog becomes visible jframe closedialog = new jframe(); final jdialog dialog = new jdialog(closedialog, "updated", true); scheduledexecutorservice s = executors.newsinglethreadscheduledexecutor(); s.schedule(new runnable() { public void run() { dialog.setvisible(false); //should invoked on edt dialog.dispose(); } }, 500, timeunit.milliseconds); dialog.setvisible(true); // if modal, application pause here //system.out.println("dialog closed"); /**************this message dialog box show row updated refresh table viewing***********/ } return null; } @override protected void done() { flag.set(false); } }
Comments
Post a Comment