java - JavaFX TableView - dynamic row and column -
i have code java application; need convert code javafx tableview
please me
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class tablerowcolumn extends jframe { private final static string letters = "abcdefghijklmnopqrstuvwxyz"; jtable table; defaulttablemodel model; jpanel buttonpanel; jbutton button; public tablerowcolumn() { // create table object[][] data = { { "1", "a" }, { "2", "b" }, { "3", "c" } }; string[] columnnames = { "number", "letter" }; model = new defaulttablemodel(data, columnnames); table = new jtable(model); table.putclientproperty("terminateeditonfocuslost", boolean.true); // add table , button panel frame jscrollpane scrollpane = new jscrollpane(table); getcontentpane().add(scrollpane); buttonpanel = new jpanel(); jscrollpane sp = new jscrollpane(buttonpanel); getcontentpane().add(sp, borderlayout.south); // --------------------------------------------------- button = new jbutton("add row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { model.addrow(createrow()); int row = table.getrowcount() - 1; table.changeselection(row, 0, false, false); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("rem sele row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { model.removerow(table.getselectedrow()); } }); button = new jbutton("rem fst row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (table.getrowcount() > 0) { model.removerow(0); } } }); button = new jbutton("rem lst row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (table.getrowcount() > 0) model.removerow(table.getrowcount() - 1); } }); button = new jbutton("insert row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { model.insertrow(0, createrow()); table.changeselection(0, 0, false, false); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("empty row"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { model.setrowcount(model.getrowcount() + 1); int row = table.getrowcount() - 1; table.changeselection(row, 0, false, false); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("add column"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string header = "col" + (table.getcolumncount() + 1); model.addcolumn(header); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("add column & data"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string header = "col" + (table.getcolumncount() + 1); int rows = table.getrowcount(); string[] values = new string[rows]; (int j = 0; j < rows; j++) { values[j] = integer.tostring(j); } model.addcolumn(header, values); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("add column - no reordering"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { /* * use method when don't want existing columns * rebuilt model. ' (ie. moved columns not * reordered) */ table.setautocreatecolumnsfrommodel(false); string header = "col" + (table.getcolumncount() + 1); model.addcolumn(header); // autocreate turned off create table column here tablecolumn column = new tablecolumn(table.getcolumncount()); column.setheadervalue(header); table.addcolumn(column); // these won't work once setautocreate... has been set false buttonpanel.getcomponent(3).setenabled(false); buttonpanel.getcomponent(4).setenabled(false); table.requestfocusinwindow(); } }); // --------------------------------------------------- button = new jbutton("remove last column"); buttonpanel.add(button); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { int columns = model.getcolumncount(); if (columns > 0) { if (!table.getautocreatecolumnsfrommodel()) { int view = table.convertcolumnindextoview(columns - 1); tablecolumn column = table.getcolumnmodel().getcolumn( view); table.getcolumnmodel().removecolumn(column); } model.setcolumncount(columns - 1); } table.requestfocusinwindow(); } }); } private object[] createrow() { object[] newrow = new object[2]; int row = table.getrowcount() + 1; newrow[0] = integer.tostring(row); newrow[1] = letters.substring(row - 1, row); return newrow; } public static void main(string[] args) { tablerowcolumn frame = new tablerowcolumn(); frame.setdefaultcloseoperation(exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }
this complete java application code. please me convert javafx.
Comments
Post a Comment