1

I have a jtable.

Some of the cells contain very long strings and trying to scroll left and right through it is difficult. My question is whether it is possible to show a row from a JTable in a pop-up eg showDialog type box (ie where the selected row is organised as a column).

Even a link to a tutorial would do.

I have scoured the internet but I don't think I'm really using the correct keywords as I get a lot of right-click options.

If this is not possible are there any other suggestions for how to do this?

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • 2
    `where the selected row is organised as a column` - not sure what that means but yes it is possible. However, you still haven't replied to my question for you when I answered one of your previous questions: http://stackoverflow.com/a/37606317/131872. – camickr Jun 10 '16 at 17:44
  • Wow. That's impressive persistence. I added an answer now to that other unrelated question but the workaround worked so being lazy (I know people like me should be...) I haven't pursued it (in the end its only me who will suffer I know....) – Sebastian Zeki Jun 10 '16 at 17:54

3 Answers3

2

You may use the following ListSelectionListener:

final JTable dialogTable =new JTable();     
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent event) {
            int selectedRow = table.getSelectedRow();
            if (selectedRow > -1) {
                int columnCount = table.getModel().getColumnCount();
                Object[] column = new Object[]{"Row "+(selectedRow+1)};
                Object[][] data = new Object[columnCount][1];
                for (int i = 0; i < columnCount; i++) {
                    Object obj = table.getModel().getValueAt(selectedRow, i);
                    data[i][0] = obj;
                }
                dialogTable.setModel(new DefaultTableModel(data, column));                  
                JOptionPane.showMessageDialog(null, new JScrollPane(dialogTable));
            }
        }
});

This is going to show a message dialog which contains a JTable with data that is derived from the selected row. Hope this helps you.

Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19
  • I want to show all the values in the row, each in their cel, organised vertically- that's what I meant by 'in a column'. I think this code will show me just an individual cell value – Sebastian Zeki Jun 10 '16 at 17:55
  • Why not a `ListSelectionListener` instead of a `MouseListener`? – trashgod Jun 10 '16 at 20:05
  • @SebastianZeki Could you please take a look at my answer. I have edited it to suit your needs. – Sanjeev Saha Jun 11 '16 at 01:58
  • @SanjeevSaha: Good catch on empty selection, but don't forget to [translate cell coordinates](http://stackoverflow.com/a/25317943/230513). – trashgod Jun 11 '16 at 22:20
2

I want to show all the values in the row, each in their cel, organised vertically- that's what I meant by 'in a column'.

That should be in the question, not in the comment.

There is no default functionality for this but you can do it yourself.

You could create a JPanel (maybe using a GridBagLayout), with two labels in a row to represent the data in a column of the selected row of the table.

For the data in the first label you would use the getColumnName(...) method of the TableModel.

For the data in the second label you would use the getValueAt(...) method of the TableModel.

Another option is to simply display a tool tip for the cell. See the section from the Swing tutorial on Specifying ToolTips For Cells for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

As shown here, the JOptionPane factory methods will display the Object passed in the message parameter. If that message is a one column JTable, you can recycle any custom renderers and editors that were applied to the original table.

In outline,

  • Add a ListSelectionListener to your table and get the selectedRow.

  • Iterate through the table's model and construct a newModel whose rows are the columns of the selectedRow.

  • Create a JTable newTable = new JTable(newModel).

  • Apply any non-default renderers and editors.

  • Pass a new JScrollPane(newTable) as the message parameter to your chosen JOptionPane method.

Starting from this example, the following listener displays the dialog pictured.

image

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        int selectedRow = table.convertRowIndexToModel(table.getSelectedRow());
        if (selectedRow > -1) {
            DefaultTableModel newModel = new DefaultTableModel();
            String rowName = "Row: " + selectedRow;
            newModel.setColumnIdentifiers(new Object[]{rowName});
            for (int i = 0; i < model.getColumnCount(); i++) {
                newModel.addRow(new Object[]{model.getValueAt(selectedRow, i)});
            }
            JTable newTable = new JTable(newModel) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return new Dimension(140, 240);
                }
            };
            // Apply any custom renderers and editors
            JOptionPane.showMessageDialog(f, new JScrollPane(newTable),
                rowName, JOptionPane.PLAIN_MESSAGE);
        }
    }
});
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045