Select Multiple rows in JTable in Java

2

Good evening, my question is how to select multiple lines in an action, I want to create a condition in a column if the value is bigger select the line, but if you have equal values, you have to select it as well. I tried to do this in the code below but it did not work, someone could help me or already did this ... thanks.

private class btnModa extends AbstractAction {
    public btnModa() {
        putValue(NAME, "Moda");

    }
    public void actionPerformed(ActionEvent e) {


        tblDados.setRowSelectionAllowed(true);

        tblDados.setRowSelectionInterval(1, 1);

        tblDados.setRowSelectionInterval(3, 3);
    }
}
    
asked by anonymous 19.10.2015 / 01:32

1 answer

5

To select multiple rows, you need to change the ListSelectionModel to MULTIPLE_INTERVAL_SELECTION :

tblDados.setRowSelectionAllowed(true);
tblDados.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

In addition, in order to select two lines that are not pasted together, you must use add instead of set , in the second line:

tblDados.setRowSelectionInterval(1, 1);    
tblDados.addRowSelectionInterval(3, 3);

I've modified an Code available in Javadoc to test this - see the result:

Followthecodeused:

importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JButton;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ListSelectionModel;publicclassSelecionarLinhasextendsJPanelimplementsActionListener{privateJButtonbutton;privateJTabletable;publicSelecionarLinhas(){super(newGridLayout(2,0));String[]columnNames={"Comunidade", "Perguntas", "Respostas", "Usuarios"
        };

        Object[][] data = {
            {"Stack Overflow", "10 milhoes", "17 milhoes", "4,8 milhoes"},
            {"SOpt", "31 mil", "41 mil", "22 mil"},
            {"Super User", "287 mil", "440 mil", "375 mil"},
            {"Chess", "2,2 mil", "5,2 mil", "6,1 mil"},
            {"Beer", "435", "1 mil", "2,5 mil"}
        };

        table = new JTable(data, columnNames);
        button = new JButton("Selecionar Linhas");

        button.addActionListener(this);        

        table.setPreferredScrollableViewportSize(new Dimension(512, 127));
        table.setFillsViewportHeight(true);
        table.setRowSelectionAllowed(true);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane scrollPane = new JScrollPane(table);

        add(scrollPane);
        add(button);
    }

    public void actionPerformed(ActionEvent e) {
        table.setRowSelectionInterval(0, 1);    
        table.addRowSelectionInterval(3, 3);    
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Selecionar Linhas JTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SelecionarLinhas newContentPane = new SelecionarLinhas();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

This solution came from this answer with help of this other .

    
23.10.2015 / 06:26