public void setCellRendered(JTableIbp table) { Enumeration<TableColumn> tcs = table.getColumnModel().getColumns(); while (tcs.hasMoreElements()) { TableColumn tc = tcs.nextElement(); if (table.getColumnClass(tc.getModelIndex()).toString().contains("java.lang.")) { tc.setCellRenderer(new CustomTableCellRenderer()); } } }
private void initIbpPane() { ibpPane = new JPanel(); setLayout(new BorderLayout()); buildIbpTm(); ibpTb = new JTableIbp(ibpTm); ibpTb.setAutoCreateRowSorter(true); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(ibpTb.getModel()); ibpTb.setRowSorter(sorter); // ibpTb.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); setCellRendered(ibpTb); ibpTb.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { // if (e.getClickCount() > 0) { // int x = e.getX(); // int y = e.getY(); // int currentRow = ibpTb.rowAtPoint(new Point(x, y)); // if (currentRow > -1) { // currentIbpUser = (String) ibpTb.getValueAt(currentRow, 0); // currentIbp = ibps.get(currentRow); // // if (e.getClickCount() == 2) // // showIbp(); // } else { // currentIbp = null; // currentIbpUser = null; // } // } else // super.mouseClicked(e); } @Override public void mousePressed(MouseEvent e) { // TODO to-generated method stub maybeIbpShowPopup(e); } @Override public void mouseReleased(MouseEvent e) { // TODO to-generated method stub maybeIbpShowPopup(e); } }); // ibpTb.setIbptoResizeMode(JTable.AUTO_RESIZE_OFF); // aggiunta tabella scrollIbpTable = new JScrollPane(ibpTb); pack(); }
private void calcSize() { scrollIbpTable.setPreferredSize(new Dimension(calcColumnWidths(ibpTb) + 20, 240)); ibpPane.add(scrollIbpTable, BorderLayout.CENTER); int rowCownt = ibps == null ? 0 : ibps.size(); ibpTb.setToolTipText(rowCownt + " jobs"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds( (int) (screenSize.getWidth() - frameWidth) / 2, (int) ((screenSize.getHeight() - frameHeight) / 2), frameWidth, frameHeight); }
public int calcColumnWidths(JTableIbp table) { int panelsWidth = 0; JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); TableColumnModel columns = table.getColumnModel(); // tableModel = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = table.getModel().getRowCount(); for (int i = columns.getColumnCount() - 1; i >= 0; --i) { TableColumn column = columns.getColumn(i); int columnIndex = column.getModelIndex(); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) // Not explicitly impossible { Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i); width = c.getPreferredSize().width; } for (int row = rowCount - 1; row >= 0; --row) { TableCellRenderer r = table.getCellRenderer(row, i); Component c = r.getTableCellRendererComponent( table, table.getModel().getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width); } if (width >= 0) column.setPreferredWidth(width + margin); // <1.3: without // margin else ; // ??? panelsWidth += column.getPreferredWidth(); } panelsWidth = panelsWidth > (screenWidth - 30) ? screenWidth : panelsWidth; frameWidth = panelsWidth > (frameWidth - 30) ? (panelsWidth - 30) : frameWidth; return panelsWidth; }
private JPopupMenu createIbpContextMenu(final int rowIndex, final int columnIndex) { JPopupMenu contextMenu = new JPopupMenu(); ItemsByProject itemsByProject = ibps.get((String) ibpTb.getValueAt(curRow, 0)); JMenuItem countAllMenuItem = new JMenuItem(); countAllMenuItem.setText("Count All"); countAllMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { countAll(); } }); contextMenu.add(countAllMenuItem); JMenuItem copyAsHtmlMenuItem = new JMenuItem(); copyAsHtmlMenuItem.setText("Export to HTML"); copyAsHtmlMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { GestConfFrame.getInstance().saveAsHtml("Items Per Project Report", ibpTb); } }); contextMenu.add(copyAsHtmlMenuItem); JMenuItem printMenuItem = new JMenuItem(); printMenuItem.setText("Print"); printMenuItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { ibpTb.print(JTable.PrintMode.FIT_WIDTH); } catch (PrinterException e1) { // TODO to-generated catch block e1.printStackTrace(); } } }); contextMenu.add(printMenuItem); return contextMenu; }
private void maybeIbpShowPopup(MouseEvent e) { if (e.isPopupTrigger() && ibpTb.isEnabled()) { currentPoint = new Point(e.getX(), e.getY()); curCol = ibpTb.columnAtPoint(currentPoint); curRow = ibpTb.rowAtPoint(currentPoint); if (curRow < 0 || curRow >= ibpTb.getRowCount()) { return; } // translate table index to model index int mcol = ibpTb.getColumn(ibpTb.getColumnName(curCol)).getModelIndex(); // .. create popup menu... JPopupMenu contextMenu = createIbpContextMenu(curRow, mcol); // ... and show it if (contextMenu != null && contextMenu.getComponentCount() > 0) { contextMenu.show(ibpTb, currentPoint.x, currentPoint.y); } } }