2

I wanted to know if there is a way to do this in a table with Java Swing.

Image for Example

I'm working with tables using Vector<Vector<String>> for passing each column data in a DefaultTableModel Object, but for this I don't know what I have to use.

Note: The table data is added dynamically during runtime.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Guolf3377
  • 97
  • 2
  • 11
  • Take a look at [this](https://www.crionics.com/public/swing_examples/SwingExamples.html), see the section on tables. Now note, this is rather old and the table API has changed, so some of this might not work or might need to some modifications to work properly – MadProgrammer Sep 25 '15 at 05:09
  • Also consider [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513). – trashgod Sep 25 '15 at 10:37

3 Answers3

2

You have to use customized TabelCellRenderer with JList view. Following code should work.

public class VectorTableCellRenderer extends JList<String> implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value instanceof Vector) {
            setListData((Vector) value);
        }

        if (isSelected) {
            setBackground(UIManager.getColor("Table.selectionBackground"));
        } else {
            setBackground(UIManager.getColor("Table.background"));
        }

        return this;
    }
}

EDIT: How to use VectorTableCellRenderer

    VectorTableCellRenderer renderer = new VectorTableCellRenderer();

    //set TableCellRenderer into a specified JTable column class
    table.setDefaultRenderer(Vector.class, renderer);

    //or, set TableCellRenderer into a specified JTable column
    table.getColumnModel().getColumn(columnIndex).setCellRenderer(renderer);

This is my tested output.

enter image description here

Channa Jayamuni
  • 1,876
  • 1
  • 18
  • 29
  • Thanks! But sorry for the ignorance, but how can I use it? I've already add it to my code, but I don't know how to "activate" it. Thanks! – Guolf3377 Sep 26 '15 at 04:13
  • Thanks, and for adding different items to different rows? I got it working, but every row has the same items. I'd like to do something like your example. Sorry if this is trivial, I'm pretty noob. Thanks! – Guolf3377 Sep 26 '15 at 04:45
  • Yes it is, just waiting the answer for my last question to mark it as accepted answer. Thanks. – Guolf3377 Sep 26 '15 at 04:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90666/discussion-between-guolf3377-and-channa-jayamuni). – Guolf3377 Sep 26 '15 at 04:55
0
javax.swing.JTable jTableReport = new javax.swing.JTable();// new JTable
javax.swing.table.DefaultTableModel TableModel = new javax.swing.table.DefaultTableModel(ArrayListTable.size(),Header.length);//new TableModel
//Now you must be add your data inside TableModel
    jTableReport.setModel(TableModel);//setModel inside JTable

int RowWidth = 90;//Width You want to be
int RowHight = 25;//Hight of one line

for (int C = 0; C < jTableReport.getColumnCount(); C++) {
    for (int R = 0; R < jTableReport.getRowCount(); R++) {
        Object RowValue = jTableReport.getValueAt(R, C);
        int lengthRow = String.valueOf(RowValue).length();
        if (lengthRow>RowWidth) {
            int NumLines = lengthRow/RowWidth;
                    if (NumLines == 1) {
                        NumLines = 2;
                    }
            int begainIndex = 0;
            int endIndex = RowWidth;
            String NewRowValue = String.valueOf(RowValue);
            for (int i = 1; i < NumLines; i++) {
                if (i==1) {
                    NewRowValue = "<html>"+(String.valueOf(RowValue).substring(begainIndex, endIndex))+"<br>";
                }else if (i == NumLines) {
                    NewRowValue = NewRowValue + (String.valueOf(RowValue).substring(begainIndex, endIndex))+"</html>";
                }else {
                    NewRowValue = NewRowValue + (String.valueOf(RowValue).substring(begainIndex, endIndex))+"<br>";
                }

                begainIndex = begainIndex + RowWidth;
                endIndex = endIndex + RowWidth;
                i++;
            }
            jTableReport.setValueAt(NewRowValue, R, C);
            jTableReport.setRowHeight(R, NumLines*RowHight);
        }
    }
}
0
public void resizeCellWidthAndHieght(JTable table, int HorizontalAlignment, int VerticalAlignment) {
    DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
        defaultTableCellRenderer.setHorizontalAlignment(HorizontalAlignment);
        defaultTableCellRenderer.setVerticalAlignment(VerticalAlignment);
    table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
    final TableColumnModel columnmodel = table.getColumnModel();
    int HieghtArray[] = new int[table.getRowCount()] ;
    for (int column = 0; column < table.getColumnCount(); column++) {
        table.getColumnModel().getColumn(column).setCellRenderer(defaultTableCellRenderer);
        int width = 15; // Min width
        int height = 25; // Min width
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width +1 , width);
            height = Math.max(comp.getPreferredSize().height +4 , HieghtArray[row]);
            if (HieghtArray[row]< height) {
                HieghtArray[row] = height;
            }

        }
        if(width > 300)
            width=300;
        columnmodel.getColumn(column).setPreferredWidth(width);
    }
    for (int i = 0; i < table.getRowCount(); i++) {
        try {
            table.setRowHeight(i, HieghtArray[i]);
        } catch (Exception e) { }
    }
}