private Vector<String> getColumnNames() { Vector<String> columnNames = new Vector<String>(); for (int i = 0; i < jTable1.getColumnCount(); i++) columnNames.add(jTable1.getColumnName(i)); return columnNames; }
private void saveColumnSpecification() { final TableColumnModel columnModel = table.getColumnModel(); final int numColumns = table.getColumnCount(); final List<String> columns = new ArrayList<String>(numColumns); final List<Integer> columnWidths = new ArrayList<Integer>(numColumns); for (int i = 0; i < numColumns; i++) { final String columnName = table.getColumnName(i); columns.add(columnName); final TableColumn column = columnModel.getColumn(i); final int columnWidth = column.getWidth(); columnWidths.add(Integer.valueOf(columnWidth)); } tableSpecification.setColumnOrder(columns); tableSpecification.setColumnWidths(columnWidths); MetricsProfileRepository.getInstance().persistCurrentProfile(); }
@Override public void mouseClicked(MouseEvent e) { int col = table.columnAtPoint(new Point(e.getX(), e.getY())); String header = table.getColumnName(col); if (columnHeader.contains(header)) { int row = table.rowAtPoint(new Point(e.getX(), e.getY())); TableModel tableModel = table.getModel(); Object val = tableModel.getValueAt( table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col)); if (val != null) { String text = val.toString(); textDialog.setText(text); textDialog.setVisible(true); } } }
public static void makeFace(JTable table) { try { DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (row % 2 == 0) setBackground(Color.white); // 设置奇数行底色 else if (row % 2 == 1) setBackground(new Color(206, 231, 255)); // 设置偶数行底色 return super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); } }; for (int i = 0; i < table.getColumnCount(); i++) { table.getColumn(table.getColumnName(i)).setCellRenderer(tcr); } } catch (Exception ex) { ex.printStackTrace(); } }
@Override public JComponent createComponent() { myPanel = new JPanel(new GridBagLayout()) { @Override public Dimension getPreferredSize() { return new Dimension(-1, 400); } }; myExtPanel = new AddEditRemovePanel<NameLocationPair>( new ExtUrlsTableModel(), myPairs, XmlBundle.message("label.edit.external.resource.configure.external.resources")) { @Override protected NameLocationPair addItem() { return addExtLocation(); } @Override protected boolean removeItem(NameLocationPair o) { setModified(true); return true; } @Override protected NameLocationPair editItem(NameLocationPair o) { return editExtLocation(o); } }; myExtPanel.getTable().setShowColumns(true); myExtPanel.setRenderer(1, new PathRenderer()); JTable table = myExtPanel.getTable(); if (myProject != null) { TableColumn column = table.getColumn(table.getColumnName(2)); column.setMaxWidth(50); column.setCellEditor(JBTable.createBooleanEditor()); } table .getModel() .addTableModelListener( new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { setModified(true); } }); myIgnorePanel = new AddEditRemovePanel<String>( new IgnoredUrlsModel(), myIgnoredUrls, XmlBundle.message("label.edit.external.resource.configure.ignored.resources")) { @Override protected String addItem() { return addIgnoreLocation(); } @Override protected boolean removeItem(String o) { setModified(true); return true; } @Override protected String editItem(String o) { return editIgnoreLocation(o); } }; myPanel.add( myExtPanel, new GridBagConstraints( 0, 0, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); myPanel.add( myIgnorePanel, new GridBagConstraints( 0, 1, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); myExtPanel.setData(myPairs); myIgnorePanel.setData(myIgnoredUrls); myExtPanel.getEmptyText().setText(XmlBundle.message("no.external.resources")); myIgnorePanel.getEmptyText().setText(XmlBundle.message("no.ignored.resources")); return myPanel; }