java - How to create a JTable cell with scrollable content -
i'd use jtable display data. data can little larger, neccessary able scroll inside cells of jtable.
i try accomplish code below. table created looks fine, unfortunatly doesn't work. scrollbar doens't seem respond mouse clicks.
how can without scrollbars "freezing" ?
thanks,
thorsten
package main; import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; public class scrollablecelltabletest extends jpanel { jtable table; string val = "this totally cool test data. "; public scrollablecelltabletest() { setlayout(new borderlayout()); (int t=0; t<10; t++) val+=val; table = new jtable(new string[][]{{val}}, new string[]{"colname"}) { public boolean iscelleditable(int row, int column) { return false; } }; table.getcolumnmodel().getcolumn(0) .setcellrenderer(new scrollablecellrenderer()); add(new jscrollpane(table), borderlayout.center); } public static void main(string[] args) { jframe frame = new jframe(); scrollablecelltabletest scrollablecelltable = new scrollablecelltabletest(); frame.getcontentpane().setlayout(new borderlayout()); frame.getcontentpane().add(scrollablecelltable, borderlayout.center); frame.pack(); frame.setlocation(500, 300); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
the cell renderer class:
package main; import java.awt.borderlayout; import java.awt.component; import java.awt.dimension; import java.util.arraylist; import java.util.list; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.jtextarea; import javax.swing.table.tablecellrenderer; /** * scrollable cell renderer. */ public class scrollablecellrenderer extends jpanel implements tablecellrenderer { private list rowcolheight = new arraylist(); jscrollpane jscrollpane; jtextarea textarea = new jtextarea(); public scrollablecellrenderer() { textarea.setlinewrap(true); textarea.setwrapstyleword(true); setlayout(new borderlayout()); add(textarea, borderlayout.center); jscrollpane = new jscrollpane(this); } public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { textarea.settext(value.tostring()); adjustrowheight(table, row, column); return new jscrollpane(this); } /** * calculate new preferred height given row, , sets height * on table. */ private void adjustrowheight(jtable table, int row, int column) { int cwidth = table.gettableheader().getcolumnmodel().getcolumn(column) .getwidth(); setsize(new dimension(cwidth, 1000)); int prefh = getpreferredsize().height; // limit height, scrollbars become neccessary prefh = math.min(prefh, 100); while (rowcolheight.size() <= row) { rowcolheight.add(new arraylist(column)); } list colheights = (list) rowcolheight.get(row); while (colheights.size() <= column) { colheights.add(new integer(0)); } colheights.set(column, new integer(prefh)); int maxh = prefh; (int t = 0; t < colheights.size(); t++) { int colheight = ((integer) colheights.get(t)).intvalue(); if (colheight > maxh) maxh = colheight; } if (table.getrowheight(row) != maxh) { table.setrowheight(row, maxh); } } }
your approach never work because renderer used render cell content. no interaction renderer possible. can provide interaction cell content using editor. unfortunately can have 1 editor @ same time. way provide scrolling in actual cell install mouse listener makes pointed cell editable (probably little delay). in listener must monitor following events: moving (make cell @ point editable), entering (same moving) , exiting (remove current editor if exists). approach work without listener: interface celleditor defines method iscelleditable(eventobject anevent)
. here should try check whether event mouse event , id entering, moving or exiting. , don't forget define appropriate cells editable in table model!
Comments
Post a Comment