在自定义AbstractTableModel中的所有列设置对齐

发布于 2025-01-17 14:44:36 字数 2607 浏览 3 评论 0原文

我有一个自定义的AbstractTableModel,默认情况下我想将所有列中心。 我知道这是使用tablecellrenderer制成的,但我不知道如何在我的代码中实现它。 这是我的课。

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;

public class BiseccionModel extends AbstractTableModel {

    private String[] columnNames = {
            "i",
            "a",
            "b",
            "xi",
            "error",
            "f(a)",
            "f(xi)"
    };

    private ArrayList<Biseccion> values;

    public BiseccionModel() {
        values = new ArrayList<Biseccion>();
    }

    public BiseccionModel(ArrayList<Biseccion> values) {
        this.values = values;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return values.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> {
                return biseccion.getI();
            }
            case 1 -> {
                return biseccion.getA();
            }
            case 2 -> {
                return biseccion.getB();
            }
            case 3 -> {
                return biseccion.getXi();
            }
            case 4 -> {
                return biseccion.getError();
            }
            case 5 -> {
                return biseccion.getFa();
            }
            case 6 -> {
                return biseccion.getFxi();
            }
            default -> {
                return null;
            }
        }
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> biseccion.setI((Integer) value);
            case 1 -> biseccion.setA((Double) value);
            case 2 -> biseccion.setB((Double) value);
            case 3 -> biseccion.setXi((Double) value);
            case 4 -> biseccion.setError((Double) value);
            case 5 -> biseccion.setFa((Double) value);
            case 6 -> biseccion.setFxi((Double) value);
        }

        fireTableCellUpdated(rowIndex, columnIndex);

    }

    public Biseccion getBiseccion(int row) {
        return values.get(row);
    }
}

这是一个可能的 /一个好主意,还是我应该坚持使用与自定义模型一起使用JTable的班级?

I have a custom AbstractTableModel and I would like to center all the columns by default.
I know this is made using TableCellRenderer but I don't know how to implement it in my code.
Here's my class.

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;

public class BiseccionModel extends AbstractTableModel {

    private String[] columnNames = {
            "i",
            "a",
            "b",
            "xi",
            "error",
            "f(a)",
            "f(xi)"
    };

    private ArrayList<Biseccion> values;

    public BiseccionModel() {
        values = new ArrayList<Biseccion>();
    }

    public BiseccionModel(ArrayList<Biseccion> values) {
        this.values = values;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return values.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> {
                return biseccion.getI();
            }
            case 1 -> {
                return biseccion.getA();
            }
            case 2 -> {
                return biseccion.getB();
            }
            case 3 -> {
                return biseccion.getXi();
            }
            case 4 -> {
                return biseccion.getError();
            }
            case 5 -> {
                return biseccion.getFa();
            }
            case 6 -> {
                return biseccion.getFxi();
            }
            default -> {
                return null;
            }
        }
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        Biseccion biseccion = getBiseccion(rowIndex);

        switch (columnIndex) {
            case 0 -> biseccion.setI((Integer) value);
            case 1 -> biseccion.setA((Double) value);
            case 2 -> biseccion.setB((Double) value);
            case 3 -> biseccion.setXi((Double) value);
            case 4 -> biseccion.setError((Double) value);
            case 5 -> biseccion.setFa((Double) value);
            case 6 -> biseccion.setFxi((Double) value);
        }

        fireTableCellUpdated(rowIndex, columnIndex);

    }

    public Biseccion getBiseccion(int row) {
        return values.get(row);
    }
}

Is this possible / a good idea or should I stick to doing it in the class where I use the JTables with the custom models?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2025-01-24 14:44:36

TableModel 不进行渲染。但是,您确实需要通过实现 getColumnClass(...) 方法来定义每列的数据类型,如上一个问题所示,以便表可以为给定的数据选择适当的渲染器班级。

Double 和 Integer 的默认渲染器将显示右对齐的数字。

如果您确实希望它们居中,则可以使用 JTable.getDefaultRenderer(...) 方法来获取默认渲染器。默认渲染器是 JLabel,因此您可以设置其对齐方式:

DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)table.getDefaultRenderer(Double.class);
renderer.setHorizontalAlignment(JLabel.CENTER);

The TableModel doesn't do the rendering. However you do need to define the type of data for each column by implementing the getColumnClass(...) method, as was demonstrated in your last question, so the table can choose the appropriate renderer for the given class.

The default renderer for Double and Integer will display the numbers right justified.

If you really want them centered then you can use the JTable.getDefaultRenderer(...) method to get the default renderer. The default renderer is a JLabel so you can set its alignment:

DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)table.getDefaultRenderer(Double.class);
renderer.setHorizontalAlignment(JLabel.CENTER);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文