Java标签中的文本对齐方式

时间:2010-12-19 02:11:07

标签: java jtable

我正在开发一个以充满数字的表为中心的Java应用程序。在某些列中,我想在列中对齐数字(当然是文本),以便小数点在单元格的中心垂直对齐。

我没有看到任何适用于这种情况的对齐常量。 Java中是否有任何官方机制来处理这种情况?

提前感谢您的帮助。

John Doner

2 个答案:

答案 0 :(得分:0)

我不是这方面的专业人士,但它确实有效,因为我在上面提到过使用JTextPane作为自定义单元格渲染器并将tab属性设置为十进制对齐。如果执行此操作,则必须注意数据表示为字符串,并且在编辑数据时,标签字符应预先添加到数据字符串的开头。

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

@SuppressWarnings("serial")
public class TabStopDecimalAlign extends JPanel {
    private static final String[][] DATA = {{"One", "1.234"}, {"Two", "232.3223"},
    {"Three", "0.2323"}, {"Four", "12.345"}, {"Five", "10000.0"}};
    private static final String[] COLUMNS = {"Count", "Data"};
    private static final int ROW_HEIGHT = 24;
    private DecimalAlignRenderer decimalAlignRenderer = new DecimalAlignRenderer();
    private DefaultTableModel model = new DefaultTableModel(DATA, COLUMNS);
    private JTable table = new JTable(model) {
        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == 1) {
                return decimalAlignRenderer; 
            }
            return super.getCellRenderer(row, column);
        }
    };

    public TabStopDecimalAlign() {
        setLayout(new BorderLayout());
        add(new JScrollPane(table), BorderLayout.CENTER);
        table.setRowHeight(ROW_HEIGHT);
    }

    private static class DecimalAlignRenderer implements TableCellRenderer {
        private static final float POS = 40f;
        private static final int ALIGN = TabStop.ALIGN_DECIMAL;
        private static final int LEADER = TabStop.LEAD_NONE;
        private static final SimpleAttributeSet ATTRIBS = new SimpleAttributeSet();
        private static final TabStop TAB_STOP = new TabStop(POS, ALIGN, LEADER);
        private static final TabSet TAB_SET = new TabSet(new TabStop[] { TAB_STOP });

        private StyledDocument document = new DefaultStyledDocument();
        private JTextPane pane = new JTextPane(document);

        public DecimalAlignRenderer() {
            StyleConstants.setTabSet(ATTRIBS, TAB_SET);
            pane.setParagraphAttributes(ATTRIBS, false);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
            if (value == null) {
                pane.setText("\t0.0");
            } else {
                pane.setText("\t" + value.toString());
            }
            return pane;
        }


    }



    private static void createAndShowUI() {
        JFrame frame = new JFrame("TabStopDecimalAlign");
        frame.getContentPane().add(new TabStopDecimalAlign());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

答案 1 :(得分:0)

如何使用DecimalFormat对象使每个数字在小数点后具有相同的位数,并恰好对齐列。