Is it possible to disable the phrase in JTable?

I have a JTable component. In one column, I embed the HTML code, and if the row in this column is longer than the width of the column, the text is wrapped. Is it possible to disable the wrapper?

@sandlex

public Object getValueAt(int row, int col) {
            return new String(
                    "<html>dgdfsgsdfg dfgdsfg sdfgs dfgsdfgsdfgsdfgsd afsdf asdfasd</html>");
        }

Verify the code using this feature. It will display the text in several lines, not in one.

+3
source share
1 answer

Can I have a look at your code?

For example, in this simplest example, nothing is wrapped:

public class TableTest {

    public static void main(String[] args) {
        JFrame frm = new JFrame();

        TableModel dataModel = new AbstractTableModel() {

            public int getColumnCount() {
                return 10;
            }

            public int getRowCount() {
                return 10;
            }

            public Object getValueAt(int row, int col) {
                return new String(
                    "<a href=\"127.0.0.1\">row*col*1000000000</a>");
            }
        };

        JTable table = new JTable(dataModel);

        table.getColumnModel().getColumn(2).setMaxWidth(120);
        table.getColumnModel().getColumn(2).setPreferredWidth(120);
        table.getColumnModel().getColumn(2).setMinWidth(120);

        frm.getContentPane().add(table);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setPreferredSize(new Dimension(300, 300));
        frm.pack();
        frm.setVisible(true);

    }
}

Edited by @latata strong> You can try to create your own CellRenderer and play with it by applying it to the desired column:

table.getColumnModel().getColumn(0).setCellRenderer(new NonWrappedCellRenderer());

Renderer might look like this:

class NonWrappedCellRenderer extends JTextArea implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(
                    JTable table,
                    Object value,
                    boolean isSelected,
                    boolean hasFocus,
                    int row,
                    int column) {
            this.setText((String)value);
            this.setLineWrap(false);                 
            return this;
    }
}

Here the line will not be wrapped since JTextArea uses by default

setLineWrap(false)

, JTextArea html, . - . JTextPane - , , wrapping. , .

+2

Source: https://habr.com/ru/post/1796662/


All Articles