public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); int firstIndex = e.getFirstIndex(); int lastIndex = e.getLastIndex(); boolean isAdjusting = e.getValueIsAdjusting(); output.append( "Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting + "; selected indexes:"); if (lsm.isSelectionEmpty()) { output.append(" <none>"); } else { // Find out which indexes are selected. int minIndex = lsm.getMinSelectionIndex(); int maxIndex = lsm.getMaxSelectionIndex(); for (int i = minIndex; i <= maxIndex; i++) { if (lsm.isSelectedIndex(i)) { output.append(" " + i); } } } output.append(newline); output.setCaretPosition(output.getDocument().getLength()); }
/** * Recursive traverse of tree to determine selections A leaf is selected if rsm is selected. * Nonleaf nodes are selected if all children are selected. * * @param tn node in the tree for which to determine selection * @param nodeidx the ordinal postion in the segments array * @param gsm the graph segments selection model * @param rsm the table row selection model * @return true if given node tn is selected, else false */ private boolean selTraverse( TreeNode tn, int[] nodeidx, ListSelectionModel gsm, ListSelectionModel rsm) { boolean selected = true; if (!tn.isLeaf()) { // A nonleaf node is selected if all its children are selected. for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); selected &= selTraverse(cn, nodeidx, gsm, rsm); } } else { if (tn instanceof RowCluster) { // get the row index of the leaf node int ri = ((RowCluster) tn).getIndex(); // A leaf is selected if its row is selected in the row selection rsm. selected = rsm.isSelectedIndex(ri); } } // Get the offset into the segments array int idx = nodeidx[0] * segOffset; if (selected) { gsm.addSelectionInterval(idx, idx + (segOffset - 1)); } else { gsm.removeSelectionInterval(idx, idx + (segOffset - 1)); } // Increment the nodeidx in the tree nodeidx[0]++; return selected; }
/** * Constructs a ClutoTree diaplay which is initialized with tableModel as the data model, and the * given selection model. * * @param clutoSolution The clustering solution * @param tableContext The context which manages views and selections. * @param tableModel the data model for the parallel coordinate display */ public ClutoTree(ClutoSolution clutoSolution, TableContext tableContext, TableModel tableModel) { ctx = tableContext; tm = tableModel; lsm = ctx.getRowSelectionModel(tm); // labelModel int ncol = tm.getColumnCount(); for (int i = 0; i < ncol; i++) { labelModel.addElement(tm.getColumnName(i)); } setLayout(new BorderLayout()); btnP = new JToolBar(); add(btnP, BorderLayout.NORTH); labelChoice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { labelColumn = 0; String ln = (String) e.getItem(); if (ln != null) { for (int c = 0; c < tm.getColumnCount(); c++) { if (ln.equals(tm.getColumnName(c))) { labelColumn = c; break; } } } graph.invalidate(); validate(); repaint(); } } }); btnP.add(labelChoice); graph = new SimpleGraph(); graph.getGraphDisplay().setOpaque(true); graph.getGraphDisplay().setBackground(Color.white); graph.getGraphDisplay().setGridColor(new Color(220, 220, 220)); graph.showGrid(false); graph.showAxis(BorderLayout.WEST, false); graph.showAxis(BorderLayout.EAST, true); graph.getAxisDisplay(BorderLayout.EAST).setZoomable(true); graph.getAxisDisplay(BorderLayout.EAST).setAxisLabeler(labeler); ((LinearAxis) graph.getYAxis()).setTickIncrement(-1.); graph.getAxisDisplay(BorderLayout.SOUTH).setZoomable(true); gs = new GraphSegments(); gs.setColor(Color.blue); idxSelColor = new IndexSelectColor(Color.cyan, null, new DefaultListSelectionModel()); gs.setIndexedColor(idxSelColor); graph.addGraphItem(gs); graph.getGraphDisplay().addMouseListener(ma); add(graph); if (lsm != null) { lsm.addListSelectionListener(selListener); } display(makeTree(clutoSolution)); }
/** * Traververse the tree selecting rows coresponding to leaf nodes of the given node tn * * @param tn the node from which to start traversing * @param rsm the selection model in which to mark selected rows */ private void selectTraverse(TreeNode tn, ListSelectionModel rsm) { if (!tn.isLeaf()) { for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); selectTraverse(cn, rsm); } } else { if (tn instanceof RowCluster) { int ri = ((RowCluster) tn).getIndex(); rsm.addSelectionInterval(ri, ri); } } }
private void maybeGrabSelection() { if (table.getSelectedRow() != -1) { // Grab selection ListSelectionModel rowSel = table.getSelectionModel(); ListSelectionModel colSel = table.getColumnModel().getSelectionModel(); if (!haveAnchor()) { // System.err.println("Updating from table's selection"); setSelection( rowSel.getAnchorSelectionIndex(), rowSel.getLeadSelectionIndex(), colSel.getAnchorSelectionIndex(), colSel.getLeadSelectionIndex()); } else { // System.err.println("Updating lead from table's selection"); setSelection( getRowAnchor(), rowSel.getLeadSelectionIndex(), getColAnchor(), colSel.getLeadSelectionIndex()); } // printSelection(); } }
public SimpleTable() { JFrame frame = new JFrame("Table"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); final String[] names = {"First Name", "Last Name", "Id"}; final Object[][] data = { {"Mark", "Andrews", new Integer(1)}, {"Tom", "Ball", new Integer(2)}, {"Alan", "Chung", new Integer(3)}, }; TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return names.length; } public int getRowCount() { return data.length; } public Object getValueAt(int row, int col) { return data[row][col]; } public String getColumnName(int column) { return names[column]; } public Class getColumnClass(int col) { return getValueAt(0, col).getClass(); } public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; } }; aTable = new JTable(dataModel); ListSelectionModel listMod = aTable.getSelectionModel(); listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listMod.addListSelectionListener(this); JScrollPane scrollpane = new JScrollPane(aTable); scrollpane.setPreferredSize(new Dimension(300, 300)); frame.getContentPane().add(scrollpane); frame.pack(); frame.setVisible(true); aTable.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { System.out.println(" double click"); } } }); }
/** Initializes the table. */ protected void init() { dataTable .getColumnModel() .addColumnModelListener( new TableColumnModelListener() { public void columnAdded(TableColumnModelEvent e) { /** empty block */ } public void columnRemoved(TableColumnModelEvent e) { /** empty block */ } public void columnSelectionChanged(ListSelectionEvent e) { /** empty block */ } public void columnMarginChanged(ChangeEvent e) { refreshTable(); } public void columnMoved(TableColumnModelEvent e) { refreshTable(); } }); // assemble statistics data array Dataset workingData = dataTable.getWorkingData(); double[] xstats = getStatistics(workingData.getXPoints()); double[] ystats = getStatistics(workingData.getValidYPoints()); if (statsData == null) { statsData = new Object[xstats.length][0]; } for (int i = 0; i < xstats.length; i++) { String label = ToolsRes.getString("Table.Entry.Count"); // $NON-NLS-1$ if (i == 5) { statsData[i] = new Object[] {label, new Integer((int) xstats[i]), new Integer((int) ystats[i])}; } else { switch (i) { case 0: label = ToolsRes.getString("Table.Entry.Max"); break; //$NON-NLS-1$ case 1: label = ToolsRes.getString("Table.Entry.Min"); break; //$NON-NLS-1$ case 2: label = ToolsRes.getString("Table.Entry.Mean"); break; //$NON-NLS-1$ case 3: label = ToolsRes.getString("Table.Entry.StandardDev"); break; //$NON-NLS-1$ case 4: label = ToolsRes.getString("Table.Entry.StandardError"); // $NON-NLS-1$ } statsData[i] = new Object[] {label, new Double(xstats[i]), new Double(ystats[i])}; } } // set and configure table model and header setModel(tableModel); setGridColor(Color.blue); setEnabled(false); setTableHeader(null); // no table header labelRenderer = new LabelRenderer(); setAutoResizeMode(JTable.AUTO_RESIZE_OFF); setDefaultRenderer(Double.class, new ScientificRenderer(3)); ListSelectionModel selectionModel = dataTable.getSelectionModel(); selectionModel.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { Dataset workingData = dataTable.getWorkingData(); double[] xstats = getStatistics(workingData.getXPoints()); double[] ystats = getStatistics(workingData.getValidYPoints()); int i = 0; for (; i < xstats.length - 1; i++) { statsData[i][1] = new Double(xstats[i]); statsData[i][2] = new Double(ystats[i]); } statsData[i][1] = new Integer((int) xstats[i]); statsData[i][2] = new Integer((int) ystats[i]); refreshTable(); } }); refreshCellWidths(); }
public ListSelectionDemo() { super(new BorderLayout()); String[] listData = {"one", "two", "three", "four", "five", "six", "seven"}; String[] columnNames = {"French", "Spanish", "Italian"}; list = new JList(listData); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); JScrollPane listPane = new JScrollPane(list); JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2); comboBox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String newMode = (String) comboBox.getSelectedItem(); if (newMode.equals("SINGLE_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } else { listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } output.append("----------" + "Mode: " + newMode + "----------" + newline); } }); controlPane.add(new JLabel("Selection mode:")); controlPane.add(comboBox); // Build output area. output = new JTextArea(1, 10); output.setEditable(false); JScrollPane outputPane = new JScrollPane( output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // Do the layout. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); add(splitPane, BorderLayout.CENTER); JPanel topHalf = new JPanel(); topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS)); JPanel listContainer = new JPanel(new GridLayout(1, 1)); listContainer.setBorder(BorderFactory.createTitledBorder("List")); listContainer.add(listPane); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); // topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(100, 50)); topHalf.setPreferredSize(new Dimension(100, 110)); splitPane.add(topHalf); JPanel bottomHalf = new JPanel(new BorderLayout()); bottomHalf.add(controlPane, BorderLayout.PAGE_START); bottomHalf.add(outputPane, BorderLayout.CENTER); // XXX: next line needed if bottomHalf is a scroll pane: // bottomHalf.setMinimumSize(new Dimension(400, 50)); bottomHalf.setPreferredSize(new Dimension(450, 135)); splitPane.add(bottomHalf); }
public void cleanUp() { if (lsm != null) { lsm.removeListSelectionListener(selListener); } }