Select Multiple Lines in Jtable in Java

Asked

Viewed 1,507 times

2

Good evening Guys, my doubt is how to select several lines in an action, is that I intend to create a condition in a column if the value is higher select the row, more if have equal values have to select as well. I tried to do it in the code below but it did not work someone could help me or already made it... I thank.

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);
    }
}

1 answer

5

To select multiple lines, you need to change the Listselectionmodel for MULTIPLE_INTERVAL_SELECTION:

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

In addition, in order to select two lines that are not glued together, it is necessary to use add instead of set, in the second row:

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

I modified a code available in Javadoc to test this - see the result:

Saída Programa Selecionar Linhas

Follows the code used:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ListSelectionModel;

public class SelecionarLinhas extends JPanel implements ActionListener {

    private JButton button;
    private JTable table;

    public SelecionarLinhas() {
        super(new GridLayout(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 of this answer, with the help of this other.

  • Show response and given due credits. + 1 =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.