JTextPane中的垂直和水平分配 [英] Vertical and Horizontal Allignment in JTextPane

查看:65
本文介绍了JTextPane中的垂直和水平分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JTable中插入一个值.但是我遇到了一些问题(设计).当我尝试添加值时,通常单词/换行不起作用,因此我尝试插入使用HTML标记嵌入的值

I am trying to insert a value in a JTable. But I come across some issues(Design). When I tried to add a value normally word/Line wrapping is not working, so I tried inserting the value embedded with HTML tag

<html>Value</html>

但这是问题所在,顶部有一些额外的空间(第一行恰好位于中间).因此,我尝试使用渲染器将JTextpane添加到JTable中,但它只能使水平居中,而不能使垂直居中(字/行换行正确工作).

but the issue with this is there occurs some extra space on top(the first line comes exactly middle). So I tried with adding JTextpane to the JTable using renderer but it only makes horizontal center and not vertical center(Word/Line wrapping is working correctly).

简而言之,我的需求是

  1. 我需要在JTable中输入一个值.
  2. 如果超过列宽,我需要进行自动换行.
  3. 我需要将值准确显示在单元格的中间(水平和垂直居中)

  1. I need to enter a value in JTable.
  2. I need to Word/Line Wrap if it exceeds the column width.
  3. I need the value to be displayed exactly in middle of cell(Horizontally and vertically centered)

class TableCellLongTextRenderer extends JTextPane implements TableCellRenderer {    
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    this.setText((String) value);
    StyledDocument doc = this.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    StyleConstants.setSpaceAbove(center, StyleConstants.getSpaceBelow(center) / 2);
    StyleConstants.setSpaceBelow(center, StyleConstants.getSpaceAbove(center));
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    return this;
}}

我的代码有什么问题,有人可以帮助我纠正它吗?预先感谢.

What is wrong in my code can someone help me to rectify it? Thanks in advance.

推荐答案

您可以仅使用普通HTML和默认的TableCellRenderer,该默认值基于JLabel来实现此基本原理.

You can just use plain HTML and the default TableCellRenderer, which is based on a JLabel to achieve this basic principles.

下面的示例使用html table,该文本进一步将文本(折线)与标签的中心对齐.它使用JLabelhorizontalAlignmentverticalAlignment属性来使内容在标签内对齐.

The following example uses a html table, which further aligns the text (broken of lines) to the center of the label. It uses the horizontalAlignment and verticalAlignment properties of the JLabel to get the contents aligned within the label.

它对行高也有一些欺骗性;)

It also does a little trickery with the row height ;)

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String text
                                = "<html><div align=center>"
                                + "And Caesar's spirit, raging for revenge,<br>"
                                + "With Ate by his side come hot from hell,<br>"
                                + "Shall in these confines with a monarch's voice<br>"
                                + "Cry \"Havoc!\" and let slip the dogs of war,<br>"
                                + "That this foul deed shall smell above the earth<br>"
                                + "With carrion men, groaning for burial.";

                DefaultTableModel model = new DefaultTableModel(0, 1);
                model.addRow(new String[]{text});

                JTable table = new JTable(model) {
                    @Override
                    public int getRowHeight(int row) {
                        Component comp = prepareRenderer(getCellRenderer(row, 0), row, 0);
                        return comp.getPreferredSize().height;
                    }
                };
                ((JLabel)table.getDefaultRenderer(Object.class)).setHorizontalAlignment(JLabel.CENTER);
                ((JLabel)table.getDefaultRenderer(Object.class)).setVerticalAlignment(JLabel.CENTER);
                table.setRowHeight(100);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

现在,我预包装了文本并将其应用于模型.您可以创建一个自定义TableCellRenderer(使用DefaultTableCellRenderer)并根据需要对文本进行后包装

Now, I pre-wrapped my text and applied it to the model. You could create a custom TableCellRenderer (using a DefaultTableCellRenderer) and post-wrap the text, depending on your needs

这篇关于JTextPane中的垂直和水平分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆