List of usage examples for javax.swing JTable getColumn
public TableColumn getColumn(Object identifier)
TableColumn
object for the column in the table whose identifier is equal to identifier
, when compared using equals
. From source file:Main.java
public static void setTableCellEditor(JTable table, String columnIdentifier, TableCellEditor cellEditor) { try {//ww w.java 2 s. c om table.getColumn(columnIdentifier).setCellEditor(cellEditor); } catch (IllegalArgumentException e) { } }
From source file:Main.java
public static void sizeWidthToFitData(JTable table, int vc, int buf) { TableColumn tc = table.getColumnModel().getColumn(vc); int max = table.getTableHeader().getDefaultRenderer() .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc) .getPreferredSize().width;/*from w w w . j a v a 2 s . c o m*/ int vrows = table.getRowCount(); for (int i = 0; i < vrows; i++) { TableCellRenderer r = table.getCellRenderer(i, vc); Object value = table.getValueAt(i, vc); Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc); int w = c.getPreferredSize().width; if (max < w) { max = w; } } tc.setPreferredWidth(max + buf); }
From source file:Main.java
public static void setTableColumnsAlignment(int[] colAlignments, JTable table) { for (int i = 0; i < table.getColumnCount(); i++) { if (colAlignments[i] == JLabel.LEFT) continue; TableColumn col = table.getColumn(table.getColumnName(i)); DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setHorizontalAlignment(colAlignments[i]); col.setCellRenderer(renderer);/* w ww . j a v a2 s. c o m*/ } }
From source file:Main.java
@SuppressWarnings("serial") public static void setupTable(JTable jTable, TableCellRenderer tableCellRenderer, String[][] content) { jTable.setModel(new DefaultTableModel(content, content[0]) { @Override/*ww w . ja v a 2s . co m*/ public boolean isCellEditable(int i, int j) { return false; } }); for (int j = 0; j < jTable.getColumnModel().getColumnCount(); j++) { jTable.getColumnModel().getColumn(j).setCellRenderer(tableCellRenderer); } jTable.doLayout(); }
From source file:Main.java
/** * Auto fit the column of a table.//from ww w . j a v a2s .c om * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }
From source file:Main.java
/** * Adjusts the widths and heights of the cells of the supplied table to fit their contents. *///from w w w . jav a 2 s . c o m public static void sizeToContents(JTable table) { TableModel model = table.getModel(); TableColumn column = null; Component comp = null; int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0; for (int cc = 0; cc < ccount; cc++) { int headerWidth = 0, cellWidth = 0; column = table.getColumnModel().getColumn(cc); try { comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; } catch (NullPointerException e) { // getHeaderRenderer() this doesn't work in 1.3 } for (int rr = 0; rr < rcount; rr++) { Object cellValue = model.getValueAt(rr, cc); comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table, cellValue, false, false, 0, cc); Dimension psize = comp.getPreferredSize(); cellWidth = Math.max(psize.width, cellWidth); cellHeight = Math.max(psize.height, cellHeight); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } if (cellHeight > 0) { table.setRowHeight(cellHeight); } }
From source file:TableDemoApplet.java
private static void createGUI(Container contentPane) { Object[][] rowData = new String[][] { { "98-43", "AraAra! SL" }, { "81-31", "Aragones Transports SA" }, { "12-72", "Rocca SL" }, { "99-10", "Rodriguez e Hijos SA" }, { "00-65", "Rimbau Motors SL" } }; JTable table = new JTable(rowData, new String[] { "Part No", "Provider" }); JComboBox companyComboBox = new JComboBox(new Object[] { "AraAra! SL", "Aragones Transports SA", "Rocca SL", "Rodriguez e Hijos SA", "Rimbau Motors SL" }); companyComboBox.setEditable(true);/*from w w w .ja v a 2 s .c o m*/ new S15WorkingBackspace(companyComboBox); // setup the ComboBoxCellEditor, DefaultCellEditor won't work! table.getColumnModel().getColumn(1).setCellEditor(new ComboBoxCellEditor(companyComboBox)); table.setPreferredScrollableViewportSize(new Dimension(400, 100)); JScrollPane scrollPane = new JScrollPane(table); contentPane.setLayout(new java.awt.FlowLayout()); contentPane.add(scrollPane); contentPane.add(new JTextField("HALLO!")); }
From source file:OAT.ui.util.UiUtil.java
public static void setupColumns(JTable table) { for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); TableColumn column = table.getColumnModel().getColumn(i); Class columnClass = table.getColumnClass(i); String columnName = table.getColumnName(i); renderer.setToolTipText(columnName); if (columnClass.getSuperclass().equals(Number.class)) { renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT); }/*from w w w . java2 s. c o m*/ if (table.isEnabled()) { if (columnName.equals("Currency")) { //addComboCell(column, Product.CURRENCIES); } else if (columnName.equals("Holidays")) { //column.setCellEditor(new FileChooserCellEditor()); } } column.setCellRenderer(renderer); } }
From source file:com.sshtools.common.ui.PreferencesStore.java
/** * * * @param table/*www. j av a2 s . c o m*/ * @param pref */ public static void saveTableMetrics(JTable table, String pref) { for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { int w = table.getColumnModel().getColumn(i).getWidth(); put(pref + ".column." + i + ".width", String.valueOf(w)); put(pref + ".column." + i + ".position", String.valueOf(table.convertColumnIndexToModel(i))); } }
From source file:com.stam.batchmove.BatchMoveUtils.java
public static void showFilesFrame(Object[][] data, String[] columnNames, final JFrame callerFrame) { final FilesFrame filesFrame = new FilesFrame(); DefaultTableModel model = new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; @Override/*ww w . j a v a2s . c om*/ public Class<?> getColumnClass(int column) { return getValueAt(0, column).getClass(); } @Override public boolean isCellEditable(int row, int column) { return column == 0; } }; DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setHorizontalAlignment(JLabel.CENTER); final JTable table = new JTable(model); for (int i = 1; i < table.getColumnCount(); i++) { table.setDefaultRenderer(table.getColumnClass(i), renderer); } // table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setRowHeight(30); table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 14)); table.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14)); table.setRowSelectionAllowed(false); table.getColumnModel().getColumn(0).setMaxWidth(35); table.getColumnModel().getColumn(1).setPreferredWidth(350); table.getColumnModel().getColumn(2).setPreferredWidth(90); table.getColumnModel().getColumn(2).setMaxWidth(140); table.getColumnModel().getColumn(3).setMaxWidth(90); JPanel tblPanel = new JPanel(); JPanel btnPanel = new JPanel(); tblPanel.setLayout(new BorderLayout()); if (table.getRowCount() > 15) { JScrollPane scrollPane = new JScrollPane(table); tblPanel.add(scrollPane, BorderLayout.CENTER); } else { tblPanel.add(table.getTableHeader(), BorderLayout.NORTH); tblPanel.add(table, BorderLayout.CENTER); } btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); filesFrame.setMinimumSize(new Dimension(800, 600)); filesFrame.setLayout(new BorderLayout()); filesFrame.add(tblPanel, BorderLayout.NORTH); filesFrame.add(btnPanel, BorderLayout.SOUTH); final JLabel resultsLabel = new JLabel(); JButton cancelBtn = new JButton("Cancel"); cancelBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { filesFrame.setVisible(false); callerFrame.setVisible(true); } }); JButton moveBtn = new JButton("Copy"); moveBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle("Choose target directory"); int selVal = fileChooser.showOpenDialog(null); if (selVal == JFileChooser.APPROVE_OPTION) { File selection = fileChooser.getSelectedFile(); String targetPath = selection.getAbsolutePath(); DefaultTableModel dtm = (DefaultTableModel) table.getModel(); int nRow = dtm.getRowCount(); int copied = 0; for (int i = 0; i < nRow; i++) { Boolean selected = (Boolean) dtm.getValueAt(i, 0); String filePath = dtm.getValueAt(i, 1).toString(); if (selected) { try { FileUtils.copyFileToDirectory(new File(filePath), new File(targetPath)); dtm.setValueAt("Copied", i, 3); copied++; } catch (Exception ex) { Logger.getLogger(SelectionFrame.class.getName()).log(Level.SEVERE, null, ex); dtm.setValueAt("Failed", i, 3); } } } resultsLabel.setText(copied + " files copied. Finished!"); } } }); btnPanel.add(cancelBtn); btnPanel.add(moveBtn); btnPanel.add(resultsLabel); filesFrame.revalidate(); filesFrame.setVisible(true); callerFrame.setVisible(false); }