Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
JTable not showing automatically at startup
tan kian
Ranch Hand
Posts: 30
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
my jtable will show up normally if i use normal colors in setBackground, but it will not display properly once i use this line: mod.getcell(row,col).getcolor() . is there something wrong with my newmodel or cell class?
public class Test extends JApplet { public void init() { //initialise all stuff here Container cp = getContentPane(); Random rand = new Random(); //table initialisation here DefaultTableModel model = new DefaultTableModel(3,3); JTable table = new JTable(model); //new class for rows JTable table2 = new Mytable(table); JScrollPane sp = new JScrollPane(table); TableColumn tcol = null; Mycellrender mcr; JTableHeader colheader = table.getTableHeader(); int i; //initialise other stuff here Vector vec = new Vector(); newmodel nm = null; cell newcell; nm = new newmodel(vec,3,3); mcr = new Mycellrender(nm); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //initialise cell data here for(i=0;i<9;i++) { //set cell details newcell = new cell(rand.nextInt(100),Color.getHSBColor(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255))); //add cell to original model nm.addcell(newcell); } // change column size here for(i=0;i<3;i++) { tcol = table.getColumnModel().getColumn(i); tcol.setPreferredWidth(40); tcol.setCellRenderer(mcr); } collistener cl = new collistener(table); colheader.addMouseListener(cl); sp.setRowHeaderView(table2); setSize(170,100); //setLocation(200,200); add(sp); cp.add(jtb,BorderLayout.NORTH); } } class newmodel { private Vector cells; private int rows,cols; public newmodel(Vector vec,int row, int col) { cells = vec; rows = row; cols = col; } public void addcell(cell c) { cells.add(c); } public cell getcell(int x, int y) { return (cell)cells.get(y*(cols+x)); } public int getvec() { return cells.size(); } } class cell { private int bl; private Color bgcolor; private ImageIcon imgicon; public cell(int blackl,Color colour) { bl = blackl; bgcolor = colour; } public void setvalues(int blackl,Color colour) { bl = blackl; bgcolor = colour; } public void setpic(ImageIcon icon) { imgicon = icon; } public int getl() { return bl; } public Color getcolor() { return bgcolor; } public ImageIcon getpic() { return imgicon; } } //new original class for own table stuff class Mytable extends JTable { //blah blah } class collistener extends MouseAdapter { //blah blah } class rowlistener extends MouseAdapter { //blah blah } class Mycellrender extends DefaultTableCellRenderer { private newmodel mod; int selectcol = -1, selectrow = -1; public Mycellrender(newmodel newmod) { mod = newmod; } public Component getTableCellRendererComponent(JTable jt,Object value,boolean isselected, boolean hasfocus,int row,int col) { super.getTableCellRendererComponent(jt,value,isselected,hasfocus,row,col); if (col == selectcol || row == selectrow || hasfocus == true) { setBackground(Color.lightGray); } else { setBackground(mod.getcell(row,col).getcolor()); } return this; } public void setcol(int index) { if (selectcol == index) selectcol = -1; else selectcol = index; } public void setrow(int index) { if (selectrow == index) selectrow = -1; else selectrow = index; } }
tan kian
Ranch Hand
Posts: 30
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
its ok, i solved it myself. there was a prob with my cell getting method.
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
JTable
jtable
JTable Columns
Rotating an imageicon in JTable cells
More...