Takes a float and sets it as a formatted value in a certain JTable cell - Java Swing

Java examples for Swing:JTable Cell

Description

Takes a float and sets it as a formatted value in a certain JTable cell

Demo Code


//package com.java2s;

import java.text.NumberFormat;

import java.util.Locale;

import javax.swing.JTable;

public class Main {
    private static NumberFormat nfGerman;

    /**//from   w ww. j a  v  a2  s. c om
     * Takes a float and sets it as a formatted value in a certain table cell
     *
     * @param table
     * @param row
     * @param col
     * @param f
     */
    public static void setFormattedFloatInTableCell(JTable table, int row,
            int col, float f) {
        // TODO: german format is default!
        table.getModel().setValueAt(nfGerman.format(f), row, col);
    }

    /**
     *
     * @param table
     * @param row
     * @param col
     * @param f
     * @param maxFractionDigits
     */
    public static void setFormattedFloatInTableCell(JTable table, int row,
            int col, float f, int maxFractionDigits) {
        // TODO: german format is default!
        NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
        nf.setMinimumFractionDigits(maxFractionDigits);
        nf.setMaximumFractionDigits(maxFractionDigits);
        table.getModel().setValueAt(nf.format(f), row, col);
    }
}

Related Tutorials