java - Accessing parent class object through thread -
my question is, in code have made jtable in main class. making thread task , collect data want fill in jtable thread collects it. thread should run while. can access jtable of main class through thread created.
edit: i'm providing code here.
test.java
package test; import java.awt.event.*; import javax.swing.*; import javax.swing.table.abstracttablemodel; import java.awt.*; public class test implements actionlistener { thread t; jtable table; jscrollpane scrollpane; jbutton b; jframe frame; public void body() { frame = new jframe("tabledemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new flowlayout()); table = new jtable(new mytablemodel()); table.setpreferredscrollableviewportsize(new dimension(500, 70)); table.setfillsviewportheight(true); scrollpane = new jscrollpane(table); frame.add(scrollpane); b = new jbutton("ok"); frame.add(b); thread = new thread(new mytablemodel()); t = new thread(a); frame.pack(); frame.setvisible(true); b.addactionlistener(this); } public static void main(string[] args) { new test().body(); } @override public void actionperformed(actionevent e) { t.start(); } } class mytablemodel extends abstracttablemodel { private string[] columnnames = { "first name", "last name", "sport" }; private object[][] data = { { "a", "a", "a" } }; @override public int getcolumncount() { return columnnames.length; } public int getrowcount() { return data.length; } public string getcolumnname(int col) { return columnnames[col]; } public object getvalueat(int row, int col) { return data[row][col]; } public class getcolumnclass(int c) { return getvalueat(0, c).getclass(); } public boolean iscelleditable(int row, int col) { if (col < 3) { return false; } else { return true; } } public void setvalueat(object value, int row, int col) { data[row][col] = value; firetablecellupdated(row, col); } }
thread.java
package test; public class thread implements runnable { mytablemodel model; thread(mytablemodel model){ this.model = model; } @override public void run() { object aa = "new value"; this.model.setvalueat(aa, 0, 0); system.out.println(this.model.getvalueat(0, 0)); } }
what want value @ 0,0 on display screen of jtable should change "a" "new value".
please give help.
when create thread create runnable class, either extending thread or implementing runnable. have own class such this
myclass implements runnable { public void run() { ... work here ... } }
simply pass objects constructor of class
myclass implements runnable { jtable m_jtable; public myclass( jtable thetable ) { m_jtable = thetable; } public void run() { things m_jtable } }
Comments
Post a Comment