harry-potter
harry-potter

Reputation: 2059

From JTable to Histogram - Swing

I have a JTable with three columns. For each rows I have a word, its type and the number of occurrences; for example in the next picture, the String "Rosing Prize" is present two times.

enter image description here

Starting from this JTable I want to build an histogram that takes as input the first and the last column. The first column is the name of bars and the last is its height; when the user selects some rows, they are represent in the histogram.

For example in this situation I have 4 rows selected:

enter image description here

The output are four J-Frames: the first with just one bar (that represents the first row); in the second J-Frame I have two bars (first and second row); in the third JFrame there are 3 bars for first, second and third row and, finally in the forth and last JFrame I have the correct output: enter image description here

I thought about two possibilities to fix this problem:

  1. to add a Jbutton and after one presses it the selected rows are drawn in the histogram
  2. to add all JFrame to an ArrayList and to print only the last.

Are there better solutions?

Upvotes: 1

Views: 384

Answers (2)

rdonuk
rdonuk

Reputation: 3956

If I understand your question right, ListSelectionListener will solve your problem.

Define a selection listener first:

class MySelectionListener implements ListSelectionListener {

Then add it to your table's selection model:

MySelectionListener selectionListener = new MySelectionListener();
table.getSelectionModel().addListSelectionListener(selectionListener);

Edit:

Create a MouseListener. Then add it to your table. Here is a working sample code:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JTable;

public class TableTest {
    JFrame window = new JFrame();

    private TableTest() {
        createWindow();
    }

    public void createWindow() {
        Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
                { "Row2-Column1", "Row2-Column2", "Row2-Column3" },
                { "Row3-Column1", "Row3-Column2", "Row3-Column3" } };
        Object columnNames[] = { "Column One", "Column Two", "Column Three" };
        JTable table = new JTable(rowData, columnNames);
        table.addMouseListener(new SelectionListener(table));

        window.add(table);
        window.pack();
        window.setVisible(true);

    }

    public static void main(String[] args) {
        new TableTest().createWindow();
    }
}

class SelectionListener extends MouseAdapter {
    JTable table;

    public SelectionListener(JTable table) {
        this.table = table;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int[] rows = table.getSelectedRows();

        for (int i = 0; i < rows.length; i++) {
            System.out.println(rows[i]);
        }
    }
}

Upvotes: 2

trashgod
trashgod

Reputation: 205865

I added a ListSelectionListener listener to my table model.

In your ListSelectionListener, update the chart's dataset only when getValueIsAdjusting() is false. This will defer updates until the selection is stable.

Upvotes: 2

Related Questions