Easy way to get the data from selected columns in JTable

I have a JTable and want to get the data from each selected column. The columns are selected by mouse clicks. So, if there are 5 columns selected, the output has to be 5 string arrays.

I'm trying to do this by MouseListener, but I can get only the clicked cells, not the entire column.

Answers


You need JTable.getSelectedColumns(), but it returns the selected column indexes, so you need to access the TableModel (package javax.swing.table)

int[] columns = jtable.getSelectedColumns();
TableModel model = jtable.getModel();
int rowcount = model.getRowCount();
String[][] output = new String[columns.length][rowcount];
for (int i = 0; i < columns.length; i++)
    for (int row = 0; row < rowcount; row++){
        int column = jtable.convertColumnIndexToModel(columns[i]);
        output[i][row] = model.getValueAt(row, column).toString();
    }

Need Your Help

Background Subtraction for Road Traffic Monitoring

image-processing computer-vision background-subtraction

I have to detect cars on a video stream (recorded) and provide some traffic data. I have read that background subtraction is the most important step so that we can be able to extract foreground ob...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.