java - Why is my table not visible when it's in a JScrollPane? -


i've been trying hours, different things , searching everywhere solution, can not table in addscorecardupper() show up. horizontal scrollbar, no content, 2 pixels worth of border.

import java.awt.borderlayout; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*;  public class yahtzeegui extends jframe{     /**      *       */     private static final long serialversionuid = 3255683022699487295l;     private jframe frame;     private jpanel panel;     private jpanel dicepanel;     private jscrollpane scrollpane;     private jbutton btnroll;     private jbutton[] btndice = new jbutton[5];     private jtable table;      private yahtzee y = new yahtzee();      public yahtzeegui(){         createwindow();         addbuttonroll();         addbuttondice();         addscorecardupper();         //addscorecardlower();           frame.add(panel);         frame.setvisible(true);     }      public void createwindow(){         frame = new jframe();         frame.settitle("yahtzee");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setsize(1000,700);         frame.setvisible(true);         panel = new jpanel(new borderlayout());         dicepanel = new jpanel();         scrollpane = new jscrollpane(scrollpaneconstants.vertical_scrollbar_always,scrollpaneconstants.horizontal_scrollbar_always);     }      public void addbuttonroll(){         btnroll = new jbutton ("roll dice");         btnroll.addactionlistener(new rollhandler());          dicepanel.add (btnroll);         panel.add(dicepanel, borderlayout.south);     }      public void addbuttondice(){          (int = 0; < btndice.length; i++){             btndice[i] = new jbutton(string.valueof(y.dice[i].getfacevalue()));             btndice[i].addactionlistener(new holdhandler());             dicepanel.add (btndice[i]);         }         panel.add(dicepanel, borderlayout.south);     }      public void addscorecardupper(){         string tableheader[] = {"upper section", "how score", "score" };         string tablevalues[][] = {                 {"ones", "total of ones",""},                 {"twos", "total of twos",""},                 {"threes", "total of threes",""},                 {"fours", "total of fours",""},                 {"fives", "total of fives",""},                 {"sixes", "total of sixes",""},                 {"total score", "",""},                 {"bonus", "",""},                 {"total + bonus", "",""}         };          table = new jtable(tablevalues, tableheader);         table.setenabled(false);         setcolumnwidths();         scrollpane.add(table); // here offender         panel.add(scrollpane, borderlayout.east);     }      /*public void addscorecardlower(){         string tableheader[] = {"lower s", "how score", "score" };         string tablevalues[][] = {                 {"ones", "total of ones",""},                 {"twos", "total of twos",""},                 {"threes", "total of threes",""},                 {"fours", "total of fours",""},                 {"fives", "total of fives",""},                 {"sixes", "total of sixes",""},                 {"total score", "",""},                 {"bonus", "",""},                 {"total + bonus", "",""}         };          table = new jtable(tablevalues, tableheader);         table.setenabled(false);         setcolumnwidths();         scoresubpanel.add(table);         panel.add(scoresubpanel, borderlayout.west);     }*/      public void setcolumnwidths(){          tablecolumn = table.getcolumnmodel().getcolumn(0);         tablecolumn b = table.getcolumnmodel().getcolumn(1);         a.setpreferredwidth(100);         b.setpreferredwidth(100);      }      class rollhandler implements actionlistener {          public void actionperformed(actionevent event) {              for(int = 0; < y.dice.length; i++){                 if (y.dice[i].getholdstate() != true){                     y.dice[i].roll();                     btndice[i].settext(string.valueof(y.dice[i].getfacevalue()));                 }             }         }     }      class holdhandler implements actionlistener {          public void actionperformed(actionevent event) {             (int = 0; < btndice.length; i++){                 if (event.getsource()== btndice[i]){ // picks pressed button after running through them all.                     if (y.dice[i].getholdstate() == false){                         y.dice[i].setholdstate(true);                     } else if (y.dice[i].getholdstate() == true){                         y.dice[i].setholdstate(false);                     }                 }             }         }     }  } 

this problem:

scrollpane.add(table); 

this not add jtable jscrollpane's viewport's view want it, rather replaces jscrollpane's viewport jtable, making jscrollpane nonfunctional. instead set table jscrollpane's viewportview:

scrollpane.setviewportview(table); 

do either or pass table jscrollpane's constructor pretty same thing:

jscrollpane scrollpane = new jscrollpane(table,           scrollpaneconstants.vertical_scrollbar_always,           scrollpaneconstants.horizontal_scrollbar_always); 

this explained in jscrollpane api, , want give important details.


edit
code calls setvisible(true) on jframe before adding components can lead trouble. you'll want avoid doing this.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -