Reputation: 1393
I have mentioned code as given as below.My question is at data.setValueAt(28.7567, 1, 1); the value should be displayed 28.757 but its taking as 28.7567000000000 so is it the Jtable.class which adding extra zero's or any other class is there consider that value as double and how to resove this problem?
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import util.leveleditor.components.LETableModel;
public class JRTest
{
public static void main(String args[])
{
Object[][] datal = new Object[30][30];
Object[] titles = new Object[30];
LETableModel model = new LETableModel(datal, titles);
JTable data = new JTable(model);
data.setFillsViewportHeight(true);
data.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
data.setShowGrid(true);
data.setGridColor(Color.BLACK);
data.setTableHeader(null);
JFrame frame = new JFrame("Test");
frame.add(new JScrollPane(data));
data.setValueAt(28.7567, 1, 1);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 245
Reputation: 256
The first parameter of JTable.setValueAt()
method is just Object type. So, if you want 28.757 then pass it directly as String.
data.setValueAt("28.757", 1, 1);
Or if you want to pass a Double value and if you want that should always be formatted to 3 decimals, then use formatter.
Check the following link: Best way to Format a Double value to 2 Decimal places
Upvotes: 1