Is it possible jtable row in optionDialog box

后端 未结 3 591
甜味超标
甜味超标 2020-12-22 01:59

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 s

相关标签:
3条回答
  • 2020-12-22 02:31

    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.

    0 讨论(0)
  • 2020-12-22 02:33

    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.

    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);
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-22 02:34

    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.

    0 讨论(0)
提交回复
热议问题