private int[] getWidths(TableModel tableModel) { int[] widths = new int[tableModel.getColumnCount()]; for (int r = 0; r < Math.min(tableModel.getRowCount(), 500); r++) { // 500 is not for performance, but for using only a sample of data with huge table for (int c = 0; c < tableModel.getColumnCount(); c++) { Object o = tableModel.getValueAt(r, c); if (o instanceof String) { String s = ((String) o).trim(); if (s.length() > widths[c]) widths[c] = s.length(); } } } return widths; }
/** * 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)); }
private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) { final TableModel model = table.getModel(); final TableColumnModel columnModel = table.getColumnModel(); final List<Integer> columnWidths = tableSpecification.getColumnWidths(); final List<String> columnOrder = tableSpecification.getColumnOrder(); if (columnWidths != null && !columnWidths.isEmpty()) { final int columnCount = model.getColumnCount(); for (int i = 0; i < columnCount; i++) { final String columnName = model.getColumnName(i); final int index = columnOrder.indexOf(columnName); if (index != -1) { final Integer width = columnWidths.get(index); final TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(width.intValue()); } } } else { final Graphics graphics = table.getGraphics(); final Font font = table.getFont(); final FontMetrics fontMetrics = table.getFontMetrics(font); final int rowCount = model.getRowCount(); int maxFirstColumnWidth = 100; for (int i = 0; i < rowCount; i++) { final String name = (String) model.getValueAt(i, 0); if (name != null) { final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics); final double stringWidth = stringBounds.getWidth(); if (stringWidth > maxFirstColumnWidth) { maxFirstColumnWidth = (int) stringWidth; } } } final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5); final TableColumn column = columnModel.getColumn(0); column.setPreferredWidth(allocatedFirstColumnWidth); } }
/** * Return a label for the given point on the graph axis * * @param value the value on the graph * @return the label for the given value */ private String getLeafLabel(double value) { String label = Double.toString(value); try { int v = (int) value; int r = idxMap.getSrc(v); if (r < 0 || r >= tm.getRowCount()) { return ""; } if (labelColumn >= 0 && labelColumn < tm.getColumnCount()) { Object o = tm.getValueAt(r, labelColumn); return o != null ? o.toString() : ""; } } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } try { label = Integer.toString((int) value); } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } return label; }
@Override public void downloadBatch(DownloadBatch_Result result) { table = new JTable(); tableModel = new TableModel(bState, result); table.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { cells = bState.cells; JTable target = (JTable) e.getSource(); int row = target.getSelectedRow(); int column = target.getSelectedColumn() - 1; for (ArrayList<Cell> cellRow : cells) { for (Cell cell : cellRow) { if (cell.record == row && cell.field == column) { bState.setSelectedCell(cell); } } } } }); table.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { JTable target = (JTable) e.getSource(); int row = target.getSelectedRow(); int column = target.getSelectedColumn() - 1; if (e.getKeyCode() == KeyEvent.VK_TAB) { for (ArrayList<Cell> cellRow : cells) { for (Cell cell : cellRow) { if (cell.record == row && cell.field == column) { bState.setSelectedCell(cell); } } } } } }); table.setModel(tableModel); table.setRowHeight(20); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setCellSelectionEnabled(true); table.getTableHeader().setReorderingAllowed(false); TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < tableModel.getColumnCount(); ++i) { TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(100); } for (int i = 0; i < tableModel.getColumnCount(); ++i) { TableColumn column = columnModel.getColumn(i); column.setCellRenderer(new CellRenderer()); // column.setCellEditor(new CellEditor()); } BoxLayout box = new BoxLayout(this, BoxLayout.Y_AXIS); setLayout(box); add(table.getTableHeader()); add(table); revalidate(); }
public int getColumnCount() { return columnCountLimit == null ? original.getColumnCount() : columnCountLimit; }
public int getColumnCount() { return (tableModel == null) ? 0 : tableModel.getColumnCount(); }