Question

Right now I'm using a JTable for what I'm doing. After looking through the Java API and various web resources, I don't think a JTable is going to cut it anymore. What I'm looking for is a table for which I can specify very strict selection procedures. I want to be able to not only select rows and columns, but also select cells in a diagonal direction. More than that, I need the overall ability to be able to specify which cells can be selected when in another cell.

For example, if I have a 10x10 table and I'm in cell (4, 3) [(row, column)], I want to be able to say, okay, you can select the following intervals from here:

  • (4, 3) to (4, 10)
  • (4, 3) to (4, 1)
  • (4, 3) to (10, 4)
  • (4, 3) to (1, 4)
  • (4, 3) to (10, 10) [diagonally]
  • (4, 3) to (1, 1) [diagonally]
  • (4, 3) to (1, 6) [diagonally]
  • (4, 3) to (6, 1) [diagonally]

Any ideas for how I could do this?

Was it helpful?

Solution

Doesn't sound like you are really modeling a 'table'. (JTable assumes table semantics and uses a List selection model.) However, I don't think it's that far removed from a matrix, if you are willing to hack the JTable code.

An alternative is your own (yep) component: A JPanel that contains the matrix cells. All keyboard/mouse event processing needs to be delegated to the parent JPanel. I would certainly recommend cloning the relevant subsets and design from JTable (data model, selection model, etc.).

So, basically, you will need 3 classes:

JMatrix, JMatrixModel, JMatrixSelectionModel.

The JMatrix is the extended JPanel with its child components. The JMatrixSelectionModel is the class that will implement the selection rules. The JMatrix should call the selection model on selection events (registered on matrix cells, delegated to handler on the parent JMatrix). The data model is fairly straightforward -- you may even be able to use existing JTableModel.

OTHER TIPS

I am in a similar situation. My solution (sorry, don't want to write a huge class) was to add a Cell Renderer for all the columns which was a mouse listener for the table. Since the Renderer knows which buttons have been selected, it can render them differently.

public class MultipleSelectionRenderer extends DefaultTableCellRenderer implements MouseListener {
    private JTable table;
    private Map<String, Boolean> selectedMap = new LinkedHashMap<String, Boolean>();
    TableUpdateIfc updater;
public MultipleSelectionRenderer(TableUpdateIfc updater, JTable table, Map<String, Boolean> selectedMap) {
    this.table = table;
    this.selectedMap = selectedMap;
    this.updater = updater;
}

@Override
public void mouseReleased(MouseEvent e) {
    if(e.getSource() == table){
        try {
            if(!e.isControlDown())
                selectedMap.clear();
            selectedMap.put(table.getSelectedRow()+":"+table.getSelectedColumn(), true);
        } catch (Exception ex) {
            selectedMap.clear();
        }
    }
    updater.updateMultipleSelectionTable(table);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}

/**
 *
 * @param table
 * @param value
 * @param isSelected
 * @param hasFocus
 * @param row
 * @param column
 * @return
 */
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component result =super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if(selectedMap.get(row+":"+column) != null && selectedMap.get(row+":"+column) == true) {
        setText(getHTMLString(value));
    }
    return result;
}

private String getHTMLString(Object value){
    String html = "<html><body><table cellpadding=0><tr>";
    html = html + "<td bgcolor=#bf65a5>";
    html = html + value.toString();
    html = html + "</td><td>&nbsp;" + value+"</td>";
    html = html + "</tr></table></body></html>";
    return html;
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top