0
두 열의 열을 표시하는 JTable이 있는데 정밀도를 4로 제한하고 싶습니다. 맞춤형 셀 렌더러를 사용하면 간단합니다. 그러나 사용자가 셀을 선택하여 값을 편집 할 때 소수점 네 자리에서자를 필요가 있습니다. 나는 사용자가 1.23423428384와 같은 것을 입력 할 수 있기를 정말로 원하지 않는다.JTable 셀 편집기의 희석화 임계 값
셀 편집기에서 많은 스레드를 읽었지만이 간단한 작업을 수행하기위한 최소 작업 예제를 찾을 수 없습니다. 아래는 최소한의 예제로 된 편집기 예제입니다. 사용자가> 4 소수 자릿수를 입력하지 못하게하기 위해 스텁 방식을 채울 수 있다면 대단히 도움이됩니다.
package sandbox;
import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;
public class TableExample extends JFrame {
public TableExample() {
//headers for the table
String[] columns = new String[]{
"Id", "Name", "Hourly Rate", "Part Time"
};
//actual data for the table in a 2d array
Object[][] data = new Object[][]{
{1, "John", 40.0, false},
{2, "Rambo", 70.0, false},
{3, "Zorro", 60.0, true},};
//create table with data
JTable table = new JTable(data, columns);
/**
* SET EDITOR HERE
*/
table.getColumnModel().getColumn(2).setCellEditor(new DecimalEditor());
//add the table to the frame
this.add(new JScrollPane(table));
this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableExample();
}
});
}
public class DecimalEditor extends AbstractCellEditor implements TableCellEditor {
@Override
public Object getCellEditorValue() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}
감사합니다. 사용자가 입력 할 때 JTextField가 셀에 표시되고 있습니까? –