java - cannot return Double Value in JTable -
i making ui in read info text file , save text file after user edits in it.i want homecoming integer values columns except column 6 , 7.for column 6 , 7 want homecoming double values.everything in programme works fine column 6 , column 7 when user edit in until come in integer value shows reddish marks in cells whereas should double values column 6 , column 7.please help
code:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class read extends jframe { private jtable table; private defaulttablemodel model; @suppresswarnings("unchecked") public read() { string aline ; vector columnnames = new vector(); vector info = new vector(); seek { fileinputstream fin = new fileinputstream("test1.txt"); bufferedreader br = new bufferedreader(new inputstreamreader(fin)); stringtokenizer st1 = new stringtokenizer(br.readline(), " "); while( st1.hasmoretokens() ) { columnnames.addelement(st1.nexttoken()); } while ((aline = br.readline()) != null) { stringtokenizer st2 = new stringtokenizer(aline, " "); vector row = new vector(); while(st2.hasmoretokens()) { row.addelement(st2.nexttoken()); } data.addelement( row ); } br.close(); } grab (exception e) { e.printstacktrace(); } final jtable table = new jtable(new defaulttablemodel(data, columnnames){ private static final long serialversionuid = 1l; @override public class getcolumnclass(int column) { homecoming integer.class; } }); jscrollpane scrollpane = new jscrollpane( table ); getcontentpane().add( scrollpane ); jpanel buttonpanel = new jpanel(); getcontentpane().add( buttonpanel, borderlayout.south ); jbutton button1 = new jbutton( "save" ); buttonpanel.add( button1 ); button1.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { if ( table.isediting() ) { int row = table.geteditingrow(); int col = table.geteditingcolumn(); table.getcelleditor(row, col).stopcellediting(); } int rows = table.getrowcount(); int columns = table.getcolumncount(); seek { stringbuilder con = new stringbuilder(); (int = 0; < rows; i++) { (int j = 0; j < columns; j++) { object value = table.getvalueat (i, j); con.append(" "); con.append(value); } con.append("\r\n"); } filewriter filewriter = new filewriter(new file("new.txt")); filewriter.write(con.tostring()); filewriter.flush(); } grab (exception ex) { ex.printstacktrace(); } } }); } public static void main(string[] args) { read = new read(); a.setdefaultcloseoperation( exit_on_close ); a.pack(); a.setvisible(true); } }
text file
1 2 3 4 5 6 7 8 78 12 12 45 4 0.0045 0.0078 45 45 45 69 56 7 0.0056 0.0023 21 45 89 76 42 1 0.0036 0.0023 36
tablemodel#getcolumnclass
used determine renderer , editor should used jtable
.
when utilize integer.class
column class, jtable
sets jformattedfield
configured allow whole numbers accepted. need modify getcolumnclass
homecoming right info type of given column, example...
@override public class getcolumnclass(int column) { homecoming column == 5 || column == 6 ? double.class : integer.class; }
you should create sure info entering model capable of meeting contract, example...
while (st2.hasmoretokens()) { object num = null; string value = st2.nexttoken(); number num = numberformat.getnumberinstance().parse(value); row.addelement(num); }
java swing jtable double defaulttablemodel
No comments:
Post a Comment