Java 如何设置JTable的行大小?

Java 如何设置JTable的行大小?,java,swing,jtable,Java,Swing,Jtable,我将创建一个应用程序 为了输入发票,我创建了使用JTable的表单 但最终用户的要求是,描述可能有2到3行 (表格图像) [i![] 所以我希望代码增加行的大小 JTable->TableColumnModel->TableColum(更改列的大小,然后 只需调查编辑器和渲染) JTable setRowHeight(更改行的高度) 最终用户的要求是,描述可能是 2到3行 我假设您试图在JTable单元格中以多行显示数据。我尝试在一个单元格中的多行中显示数据的示例 @Sergii建议使用JTa

我将创建一个应用程序 为了输入发票,我创建了使用JTable的表单 但最终用户的要求是,描述可能有2到3行 (表格图像) [i![]

所以我希望代码增加行的大小

  • JTable->TableColumnModel->TableColum
    (更改列的大小,然后 只需调查编辑器和渲染)
  • JTable setRowHeight
    (更改行的高度)
最终用户的要求是,描述可能是 2到3行

我假设您试图在
JTable
单元格中以多行显示数据。我尝试在一个单元格中的多行中显示数据的示例

@Sergii建议使用
JTable.setRowHeight
增加单元格的高度。
数据列
单元格用
JTextArea
呈现

import java.awt.Component;
import java.awt.EventQueue;
import java.util.EventObject;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;


public class JTableCellTest {
    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                Object[] columnNames = {"S.No", "Data"};
                Object[][] data = {
                                        {"1", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"},
                                        {"2", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"},
                                        {"3", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"}
                                  };

                JFrame frame = new JFrame();
                JTable table = new JTable(data, columnNames);
                table.setRowHeight(70);

                table.getColumnModel().getColumn(1).setCellRenderer(new CustomCellRenderer());
                table.getColumnModel().getColumn(1).setCellEditor(new CustomEditor());

                frame.setTitle("JTable with JTextArea");
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setVisible(true);

            }
        };

        EventQueue.invokeLater(r);
    }
}

class CustomCellRenderer extends DefaultTableCellRenderer {

        private JTextArea textArea;
        private JScrollPane scrollPane;

        public CustomCellRenderer() {
            textArea = new JTextArea();
            scrollPane = new JScrollPane(textArea);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {

            if(null != value)
                textArea.setText(value.toString());

            return scrollPane;
        }
}

class CustomEditor implements TableCellEditor {

    private JTextArea textArea;
    private JScrollPane scrollPane;

    public CustomEditor() {
        textArea = new JTextArea();
        scrollPane = new JScrollPane(textArea);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        if(null != value)
            textArea.setText(value.toString());

        return scrollPane;
    }

    @Override
    public void addCellEditorListener(CellEditorListener arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void cancelCellEditing() {
        // TODO Auto-generated method stub

    }
    @Override
    public Object getCellEditorValue() {
        // TODO Auto-generated method stub
        return textArea.getText();
    }
    @Override
    public boolean isCellEditable(EventObject arg0) {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public void removeCellEditorListener(CellEditorListener arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean shouldSelectCell(EventObject arg0) {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public boolean stopCellEditing() {
        // TODO Auto-generated method stub
        return true;
    }
}


看见顺便说一句ITYM是2行或3行的“数量”,而不是每行的“大小”。此外,图像丢失。为了更快地获得更好的帮助,请发布一个.thankx,先生,我想要一种方法,如果客户端输入文本,那么在特定的字数或字长之后,游标将自动转到下一行