gpt4 book ai didi

java - 渲染单元格中的 double 值

转载 作者:行者123 更新时间:2023-12-01 23:08:39 24 4
gpt4 key购买 nike

我使用自定义表格模型 (MyTableModel.java) 创建了一个 JTable ,并使用了一些 CellRenderer 类来查看单元格的效果。我使用 CustomTableCellRenderer 类将当前单元格更改为绿色背景。效果非常好。同时,我创建了一个 RenderPrice 类来以十进制类型显示单元格,如果我编辑未更新的单元格,它会在我第一次执行时获取十进制值$ 符号作为前缀。我想解决这个问题。请尽快给出一些解决方案。谢谢

RenderPrice类代码

class RenderPrice extends DefaultTableCellRenderer {
Object result;
RenderPrice() {
setHorizontalAlignment(SwingConstants.RIGHT);
}

public void setValue(Object aValue) {
result = aValue;
if ((aValue != null) && (aValue instanceof Number)) {
Number numberValue = (Number)aValue;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
result = formatter.format(numberValue.doubleValue());
}
super.setValue(result);
}
}

我的Tableclass(Accept.java)的前述代码

gradeColumn = jTable1.getColumnModel().getColumn(2);
gradeColumn.setCellRenderer(new RenderPrice());

gradeColumn = jTable1.getColumnModel().getColumn(0);
gradeColumn.setCellEditor(new IntegerEditor(1,100));

gradeColumn = jTable1.getColumnModel().getColumn(0);
gradeColumn.setCellRenderer(new CustomTableCellRenderer());

gradeColumn = jTable1.getColumnModel().getColumn(1);
gradeColumn.setCellEditor(new ComboBoxCellEditor(comboBox));

最佳答案

您不应该在此类渲染器中存储任何状态。您可能正在寻找类似的东西

class RenderPrice extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);

if ((value != null) && (value instanceof Number))
{
Number numberValue = (Number)value;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String valueString = formatter.format(numberValue.doubleValue());
setText(valueString);
}

return this;
}
}
<小时/>

编辑:回应评论和示例

import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.NumberFormatter;

public class CurrencyCellEditorTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTable table = new JTable(
new Object[][]{
{ 1.2, 3.4 },
{ 5.6, 7.8 }
},
new Object[] {
"A", "B"
});


TableColumn column0 = table.getColumnModel().getColumn(0);
column0.setCellEditor(new PriceEditor());
column0.setCellRenderer(new PriceRenderer());

TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new PriceEditor());
column1.setCellRenderer(new PriceRenderer());

f.getContentPane().add(table);
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);

}
}

class PriceRenderer extends DefaultTableCellRenderer
{
PriceRenderer()
{
setHorizontalAlignment(SwingConstants.RIGHT);
}

@Override
public void setValue(Object aValue)
{
Object result = aValue;
if ((aValue != null) && (aValue instanceof Number))
{
Number numberValue = (Number) aValue;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
result = formatter.format(numberValue.doubleValue());
}
super.setValue(result);
}
}

class PriceEditor extends DefaultCellEditor
{
public PriceEditor()
{
super(createComponent());
delegate = new EditorDelegate()
{
@Override
public void setValue(Object value)
{
Number numberValue = 0.0;
if (value != null && value instanceof Number)
{
numberValue = (Number)value;
}
getFormattedTextField().setValue(numberValue);
}

@Override
public Object getCellEditorValue()
{
try
{
NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
String text = getFormattedTextField().getText();
Number number = numberFormat.parse(text);
return number;
}
catch (ParseException e)
{
e.printStackTrace();
return new Double(0.0);
}
}
};
}

private static JTextField createComponent()
{
NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
NumberFormatter numberFormatter = new NumberFormatter(numberFormat);
JFormattedTextField formattedTextField = new JFormattedTextField(numberFormatter);
formattedTextField.setHorizontalAlignment(SwingConstants.RIGHT);
return formattedTextField;
}

private JFormattedTextField getFormattedTextField()
{
return (JFormattedTextField)getComponent();
}

}

关于java - 渲染单元格中的 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22381305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com