How do I set the row size of a JTable?

I am going to create an application in that for entering an invoice I created a form in which I used a JTable but the end user requirement is that the description can be from 2 to 3 lines (table image) [Me! []]

so i want the code to increase the size of the string

+3


source to share


2 answers


end user requirement is that description can be 2 to 3 lines

I am assuming you are trying to display data across multiple rows in a cell JTable

. An example I tried to show data in multiple rows inside a cell.

As @Sergii suggested using JTable.setRowHeight

to increase cell height. The cell is data column

displayed with JTextArea

.



JTable with a cell rendered as 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;
    }
}

      

+4


source


  • JTable->TableColumnModel->TableColum

    (resize the column and just explore the editor and render)
  • JTable-setRowHeight

    (change line height)


+6


source







All Articles