编程语言
首页 > 编程语言> > java-JTabel单个单元格文本对齐方式

java-JTabel单个单元格文本对齐方式

作者:互联网

基本上,我有一个JTable,这个JTabel将在一个单元格中有一个产品,然后在其直接下方的单元格中有一个产品.

产品名称应与左侧对齐.
产品成本应与右边保持一致.

我实际上并不在乎每行中其他单元格的对齐方式.

因此,我需要设置单个单元格或单个行的对齐方式.我找到了设置表格对齐方式的方法,以及设置列对齐方式的方法,但是没有找到行/单个单元格.

SSCCE:

public class Main extends JFrame{
    public static void main(String args[]){
        new Main();
    }
    public Main(){
        super("Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
        setLayout(new BorderLayout());
        TableModel dataModel = new AbstractTableModel() {
             Object rows[] = new Object[50];
             public int getColumnCount(){return 1;}
             public int getRowCount(){return rows.length;}
             public Object getValueAt(int row, int col){ 
                 return rows[row];
             }
             public boolean isCellEditable(int row, int col){
                 return false; 
             }
             public void setValueAt(Object value, int row, int col) {
                 rows[row] = value;
                 fireTableCellUpdated(row,0);
             }
         };

        JTable receipt = new JTable(dataModel);
        receipt.setBorder(BorderFactory.createEtchedBorder());
        receipt.setShowGrid(false);
        add(receipt,BorderLayout.CENTER);
        for(int i = 0; i < 10; i+=2){
            receipt.setValueAt("ProductNameHere",i,0);
            receipt.setValueAt("Cost",i+1,0);
        }
        validate();
        repaint();
    }
}

解决方法:

数字的default renderer是右对齐的标签.在此example中,不需要特殊的渲染器即可将INT_COL右对齐,该标记为Index且类型为Integer.class.

如果这没有帮助,请编辑您的问题,以包括一个sscce,以显示您当前的方法和成本数据类型.

附录:或者,重写prepareRender(),如here所示.

JTable receipt = new JTable(dataModel) {
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        JLabel c = (JLabel) super.prepareRenderer(renderer, row, col);
        if (row % 2 == 0) {
            c.setHorizontalAlignment(JLabel.LEFT);
        } else {
            c.setHorizontalAlignment(JLabel.RIGHT);

        }
        return c;
    }
};

SSCCE:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class Main extends JFrame {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }

    public Main() {
        super("Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TableModel dataModel = new AbstractTableModel() {
            Object rows[] = new Object[10];

            @Override
            public int getColumnCount() {
                return 1;
            }

            @Override
            public int getRowCount() {
                return rows.length;
            }

            @Override
            public Object getValueAt(int row, int col) {
                return rows[row];
            }

            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }

            @Override
            public void setValueAt(Object value, int row, int col) {
                rows[row] = value;
                fireTableCellUpdated(row, 0);
            }
        };

        JTable receipt = new JTable(dataModel) {
            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
                JLabel c = (JLabel) super.prepareRenderer(renderer, row, col);
                if (row % 2 == 0) {
                    c.setHorizontalAlignment(JLabel.LEFT);
                } else {
                    c.setHorizontalAlignment(JLabel.RIGHT);

                }
                return c;
            }
        };
        receipt.setBorder(BorderFactory.createEtchedBorder());
        receipt.setShowGrid(false);
        add(receipt, BorderLayout.CENTER);
        for (int i = 0; i < 10; i += 2) {
            receipt.setValueAt("ProductNameHere", i, 0);
            receipt.setValueAt(Integer.valueOf(i + 1), i + 1, 0);
        }
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }
}

标签:java,alignment,swing,jtable,tablecellrenderer
来源: https://codeday.me/bug/20191013/1907893.html