简体   繁体   English

如何设置JTable的行大小?

[英]How to set row size of JTable?

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 (image of table) [i![]] 我将创建应用程序,以输入发票,我已创建表格,其中我使用了JTable但最终用户要求是描述可能是2到3行(表格的图像)[i![]]

so i want the code to increase the size of row 所以我希望代码增加行的大小

  • JTable->TableColumnModel->TableColum (change size of column, and just investigate editor and render) JTable->TableColumnModel->TableColum (更改列的大小,只调查编辑器和渲染)
  • JTable-setRowHeight (change height of row) JTable-setRowHeight (改变行的高度)

end user requirement is that the the description may be of 2 to 3 lines 最终用户要求是描述可以是2到3行

I assume that you are trying to display data in multiple lines in a JTable cell. 我假设你试图在JTable单元格中以多行显示数据。 An example which I have tried to show data in multiple lines within a cell. 我试图在单元格中的多行显示数据的示例。

As @Sergii has suggested use JTable.setRowHeight to increase the height of the cell. 正如@Sergii建议使用JTable.setRowHeight来增加单元格的高度。 The data column cell is rendered with JTextArea . 使用JTextArea呈现data column单元格。

JTable,单元格呈现为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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM