List of usage examples for javax.swing.table TableColumnModel getColumnCount
public int getColumnCount();
From source file:Main.java
public static int getIndexAtColumnModel(TableColumnModel model, String name) { for (int i = 0; i < model.getColumnCount(); i++) { String headStr = model.getColumn(i).getHeaderValue().toString(); if (name.equals(headStr)) { return i; }//from w w w .j a va2 s .c om } return 0; }
From source file:Main.java
public static int getPreferredTableWidth(final JTable table) { TableColumnModel columnModel = table.getColumnModel(); int width = 0; for (int col = 0; col < columnModel.getColumnCount(); col++) { width += columnModel.getColumn(col).getPreferredWidth(); if (col != 0) { width += columnModel.getColumnMargin(); }/*from w w w . j ava2s . co m*/ } return width; }
From source file:Main.java
/** * Save the contents of a table to a TSV file * Note: uses toString() on the header cells as well as the data cells. If you've got funny columns, * expect funny behavior/*w w w. j ava 2s.c om*/ * @param table * @param outFile * @throws IOException */ public static void SaveTableAsTSV(JTable table, File outFile) throws IOException { PrintWriter outPW = new PrintWriter(outFile); TableModel tableModel = table.getModel(); TableColumnModel columnModel = table.getColumnModel(); StringBuffer headerLineBuf = new StringBuffer(); for (int i = 0; i < columnModel.getColumnCount(); i++) { if (i > 0) headerLineBuf.append("\t"); headerLineBuf.append(columnModel.getColumn(i).getHeaderValue().toString()); } outPW.println(headerLineBuf.toString()); outPW.flush(); for (int i = 0; i < tableModel.getRowCount(); i++) { StringBuffer lineBuf = new StringBuffer(); for (int j = 0; j < tableModel.getColumnCount(); j++) { if (j > 0) lineBuf.append("\t"); lineBuf.append(tableModel.getValueAt(i, j).toString()); } outPW.println(lineBuf.toString()); outPW.flush(); } outPW.close(); }
From source file:Main.java
/** * Maps the index of the column in the {@code cm} at * {@code modelColumnIndex} to the index of the column * in the view. Returns the index of the * corresponding column in the view; returns {@code -1} if this column * is not being displayed. If {@code modelColumnIndex} is less than zero, * returns {@code modelColumnIndex}./*w ww. j ava 2 s. c o m*/ * * @param cm the table model * @param modelColumnIndex the index of the column in the model * @return the index of the corresponding column in the view * * @see JTable#convertColumnIndexToView(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToView(TableColumnModel cm, int modelColumnIndex) { if (modelColumnIndex < 0) { return modelColumnIndex; } for (int column = 0; column < cm.getColumnCount(); column++) { if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { return column; } } return -1; }
From source file:edu.ku.brc.stats.StatGroupTable.java
/** * Calculates and sets the each column to it preferred size * @param table the table to fix ups/*from ww w .j a va2 s. c o m*/ */ public static void calcColumnWidths(JTable table) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; 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, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { size.height = Math.min(size.height, table.getRowHeight() * visibleRows); size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } }
From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java
/** * @param table//w w w . j ava 2 s . co m */ public static void calcColumnWidths(JTable table) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; 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, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } else { // ??? } totalWidth += column.getPreferredWidth(); } }
From source file:TableIt.java
public TableIt() { JFrame f = new JFrame(); TableModel tm = new MyTableModel(); final JTable table = new JTable(tm); TableColumnModel tcm = table.getColumnModel(); TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1); TableCellRenderer renderer = new MyTableCellRenderer(); column.setCellRenderer(renderer);//w w w . jav a 2s . c om JButton selectionType = new JButton("Next Type"); selectionType.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean rowSet = table.getRowSelectionAllowed(); boolean colSet = table.getColumnSelectionAllowed(); boolean cellSet = table.getCellSelectionEnabled(); boolean setRow = !rowSet; boolean setCol = rowSet ^ colSet; boolean setCell = rowSet & colSet; table.setCellSelectionEnabled(setCell); table.setColumnSelectionAllowed(setCol); table.setRowSelectionAllowed(setRow); System.out.println("Row Selection Allowed? " + setRow); System.out.println("Column Selection Allowed? " + setCol); System.out.println("Cell Selection Enabled? " + setCell); table.repaint(); } }); JButton selectionMode = new JButton("Next Mode"); selectionMode.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel lsm = table.getSelectionModel(); int mode = lsm.getSelectionMode(); int nextMode; String nextModeString; if (mode == ListSelectionModel.SINGLE_SELECTION) { nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION; nextModeString = "Single Interval Selection"; } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) { nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; nextModeString = "Multiple Interval Selection"; } else { nextMode = ListSelectionModel.SINGLE_SELECTION; nextModeString = "Single Selection"; } lsm.setSelectionMode(nextMode); System.out.println("Selection Mode: " + nextModeString); table.repaint(); } }); JPanel jp = new JPanel(); jp.add(selectionType); jp.add(selectionMode); JScrollPane jsp = new JScrollPane(table); Container c = f.getContentPane(); c.add(jsp, BorderLayout.CENTER); c.add(jp, BorderLayout.SOUTH); f.setSize(300, 250); f.show(); }
From source file:ThreadViewer.java
public ThreadViewer() { JTable table = new JTable(tableModel); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); TableColumnModel colModel = table.getColumnModel(); int numColumns = colModel.getColumnCount(); for (int i = 0; i < numColumns - 1; i++) { TableColumn col = colModel.getColumn(i); col.sizeWidthToFit();// www.j a va 2 s.c o m col.setPreferredWidth(col.getWidth() + 5); col.setMaxWidth(col.getWidth() + 5); } JScrollPane sp = new JScrollPane(table); setLayout(new BorderLayout()); add(sp, BorderLayout.CENTER); }
From source file:edu.ucla.stat.SOCR.chart.demo.BoxAndWhiskerChartDemo2.java
public void setXLabel(String xLabel) { domainLabel = xLabel;//from w w w. ja v a 2s .c o m TableColumnModel columnModel = dataTable.getColumnModel(); for (int i = 0; i < columnModel.getColumnCount(); i++) columnModel.getColumn(i).setHeaderValue("serie" + i); dataTable.setTableHeader(new EditableHeader(columnModel)); }
From source file:fxts.stations.util.preferences.PreferencesSheetPanel.java
/** * init table// w ww .j a v a 2 s . co m */ public void initTable() { int width = -1; TableColumnModel columnModel = mTable.getColumnModel(); if (columnModel.getColumnCount() > 1) { width = columnModel.getColumn(0).getPreferredWidth(); } mTableModel.init(); mTableModel.fireTableStructureChanged(); if (width >= 0) { columnModel = mTable.getColumnModel(); columnModel.getColumn(0).setPreferredWidth(width); } }