简体   繁体   中英

Jtable with double cell type precision

I have to make one of my columns in JTable which is double type to get only numbers and to round all with precision 2. Not only to show them with this precision but instead to write the number into data with precision 2. In fact if I write 2.456 it should write 2.46 in the cell and in the data. How is the best way to do this ? With custom cell renderer or with custom cell editor ? I have this code but its dont change the data it only shows correct data on the cell

public class DoubleCellRenderer extends DefaultTableCellRenderer {

int precision = 0;
Number numberValue;
NumberFormat nf;

public DoubleCellRenderer(int aPrecision) {
    super();
    precision = aPrecision;
    setHorizontalAlignment(SwingConstants.RIGHT);
    nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(aPrecision);
    nf.setMaximumFractionDigits(aPrecision);
}

@Override
public void setValue(Object value) {
    if ((value != null) && (value instanceof Number)) {
        numberValue = (Number) value;
        value = nf.format(numberValue.doubleValue());
    }
    super.setValue(value);
}


}

You should use both.

CellRenderer: how the data is displayed (what you are going to show)

CellEditor: how the data is edited (eventually you can choose what value to set in your model)

In both case you can choose the precision. See more info here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender

Note: I would recommend to rather override getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) than setValue(Object value)

If I understood your question correctly, you want to store the doubles with limited precision in your model, even when the user fills in a value with more digits.

In that case you only need a custom editor to round the value before storing it in your model. Override/adjust the getCellEditorValue() method to perform the necessary rounding on the input value

I agree with Guillaume use both. Here is a link with a good implementation for the renderer that uses the getTableCellRendererComponent override mentioned in the other answer.

http://helpdesk.objects.com.au/java/how-to-control-decimal-places-displayed-in-jtable-column

I modified it slightly to enforce 3 digits only in rounding. Also added a check to ensure that the data type is double

public static class DecimalFormatRenderer extends DefaultTableCellRenderer {
    private static final long serialVersionUID = 1L;
    private static final DecimalFormat formatter = new DecimalFormat( "#.###" );

    public DecimalFormatRenderer(){
        super();
        formatter.setMinimumFractionDigits(3);
    }

    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    // First format the cell value as required

        if(value instanceof Double){
            value = formatter.format((Number)value);
        }

        return super.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, column );
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM