This blog presents custom cell selection in jtable. I spent a lot of time searching with google but i did not find nothing similar. I hope this blog will help you.
I have to implemented a custom selection in jtable. I have a table with 8 rows and 4 columns. When I select a cell c2 and end the selection in cell e3 the following cells should be selected (c2,d2,e2,f2,g2,h2,a3,b3,c3,d3,e3). Please see the picture below.

And how the Swing works? I start selection in cell c2. When I move to cell e3 default swing funcionality selects cells (c2,d2,e2,c3,d3,e3). If I would move to cell d3 only cells (c2,d2,c3,d3) would be selected.
How to solve this problem? See the code below. I am sure the code could be optimized a little bit. I will be very glad if you give me some ideas to improve the code. Would you like to have selection like this ?

Run the code below!
import java.awt.*;
import java.util.HashSet;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListSelectionModel;
import javax.swing.*;
public class CustomTableSelection extends JFrame {
HashSet cellSelection = new HashSet();
JTable table;
int iRowMin;
int iColMin;
public CustomTableSelection() {
super(“Custom Table Selection”);
table = new JTable(10, 10) {
public void changeSelection(
int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
}
public boolean isCellSelected(int row, int column)
{
boolean isSel = cellSelection.contains(new Point(row, column));
return isSel;
}
public boolean isColumnSelected(int column)
{
return false;
}
public boolean isRowSelected(int row)
{
return false;
}
};
table.getSelectionModel().addListSelectionListener (new ListSelectionListener()
{
public void valueChanged (ListSelectionEvent e)
{
iColMin = table.getColumnModel().getSelectionModel().getMinSelectionIndex();
if(iColMin == table.getColumnModel().getSelectionModel().getLeadSelectionIndex()) {
iRowMin = table.getSelectionModel().getMinSelectionIndex();
}
int li_rowMax = table.getSelectionModel().getLeadSelectionIndex();
int li_colMax = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
selectCells(iRowMin, iColMin, li_rowMax, li_colMax);
}
});
table.getColumnModel().getSelectionModel().addListSelectionListener (new ListSelectionListener()
{
public void valueChanged (ListSelectionEvent e)
{
iColMin = table.getColumnModel().getSelectionModel().getMinSelectionIndex();
if(iColMin == table.getColumnModel().getSelectionModel().getLeadSelectionIndex()) {
iRowMin = table.getSelectionModel().getMinSelectionIndex();
}
int li_rowMax = table.getSelectionModel().getLeadSelectionIndex();
int li_colMax = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
selectCells(iRowMin, iColMin, li_rowMax, li_colMax);
}
});
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
}
private void selectCells(int pRowMin, int pColMin, int pRowMax, int pColMax) {
cellSelection = new HashSet();
for(int i=pColMin;i<=pColMax;i++) {
int fromRow = 0;
int toRow = 0;
if (i==pColMin) {
fromRow=pRowMin;
if (pColMin==pColMax) {
toRow=pRowMax;
} else {
toRow=table.getRowCount()-1;
}
} else if (i==pColMax) {
fromRow=0;
toRow=pRowMax;
} else if (pColMin<pColMax) {
fromRow=0;
toRow=table.getRowCount()-1;
}
for (int j=fromRow;j<=toRow;j++) {
cellSelection.add(new Point(j, i));
}
}
//refresh the table
table.repaint();
}
public static void main(String[] args) {
new CustomTableSelection().setVisible(true);
}
}
Do you find this post useful? Do not hesitate to leave a reply! Go to the bottom of this page.