0

So I wrote a code that uses MouseListener to select/highlight data in a JTable. I would like to crop the highlighted data. For example, I would highlight the first column and 3rd column with 20 rows down, click a button and make a new JTable with the highlighted data. This is my MouseListener method that highlights data:

public void DataSelection(JTable table) {
  JTable Table = table;
  JTableHeader columnHeader = Table.getTableHeader();

  //***Row Selection***//

  Table.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent mouseEvent) {
        Table.setColumnSelectionAllowed(true);
        Table.setRowSelectionAllowed(true);

        if (Table.isCellSelected(Table.getSelectedRow(), 0)) {
            Table.setColumnSelectionAllowed(false);
            Table.setRowSelectionAllowed(true);                    
        }

    }
  });

  //***Column Selection***//

  columnHeader.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent mouseEvent) {
        int columnPoint =  columnHeader.columnAtPoint(mouseEvent.getPoint());

        Object columnCursorType = columnHeader.getCursor().getType();

        if (columnCursorType == Cursor.E_RESIZE_CURSOR)
            mouseEvent.consume();
        else {

            if (columnPoint == 0)
                Table.selectAll();
            else {
                Table.setColumnSelectionAllowed(true);
                Table.setRowSelectionAllowed(false);
                Table.clearSelection();
                Table.setColumnSelectionInterval(columnPoint, columnPoint);
            }

        }
    }
  });

}
Abdane
  • 137
  • 1
  • 12
  • 1
    Good problem description - now what have you tried? Show us your attempt at solving the problem (getting the selected rows/columns into a new Table). Otherwise, all we can/will do is comment/criticize your row/column selection. – AJNeufeld Oct 03 '17 at 17:25
  • @AJNeufeld So the button does make a new table, I am having hard time of getting the values from the selection. How can I assign a new JTable the values of the selection done by the code i wrote above? – Abdane Oct 03 '17 at 17:29
  • I am stuck on how would I get the highlighted data into a 2D array for example or a 2D List. – Abdane Oct 03 '17 at 17:31
  • 1
    https://stackoverflow.com/a/14416372/8098743 There are answers to similiar problems, maybe this can help you –  Oct 03 '17 at 17:33
  • I'm kind of stumped over what the problem is. Basically, I'd devise a `TableColumnModel` which supported hiding columns and use the `JTable` filtering support to create a new `JTable`, passing in the existing data, along with the column/row constraints to create the cropped view – MadProgrammer Oct 03 '17 at 21:02

1 Answers1

1

So, you have three pieces of information:

  • The selected columns
  • The selected rows
  • The data

You need to create a way to combine these together to produce a "cropped" view of the data

Now you "could" filter the data directly and simply seed that into a new TableModel, but I think that's more work then is required

Instead, I'd focus on filtering the TableColumnModel and JTable directly.

JTable supports filtering already, so all you need to do is devise a custom filter that supports row ranges. Start by taking a look at How to use JTable: Sorting and Filtering for more details

Filtering a TableColumnModel is not so easy, as there's no built in functionality for it, you'll need to devise the yourself. Lucky for you, lots of other people have had this requirement before you.

For example:

With this is hand, you could easily create a filtered TableModel and custom TableRowFilter, pass the existing data/TableModel to a new instance of JTable and apply the filtered TableModel and TableRowFilter to it

"But where's my runnable example?" you say - Well, there isn't one, why? Because the over all requirement is to broad, you have to answer two questions to achieve the result.

Having said that, there are plenty of examples linked which provide you with the basic information you need to devise a working solution of your own

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366