JTable Right Align Header

Here’s an alternate approach to modifying the TableCellRenderer of a table’s JTableHeader. It’s not strictly necessary for this usage, but it minimizes the impact on the UI delegate’s appearance. Typical usage: JTable table = new JTable(…); JTableHeader header = table.getTableHeader(); header.setDefaultRenderer(new HeaderRenderer(table)); Custom header renderer: private static class HeaderRenderer implements TableCellRenderer { DefaultTableCellRenderer renderer; public … Read more

How to center in JTable cell a value?

You need to customize the renderer. To center the first column you can do: DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); centerRenderer.setHorizontalAlignment( JLabel.CENTER ); table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer ); To center all columns with String data you can do: DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); centerRenderer.setHorizontalAlignment( JLabel.CENTER ); table.setDefaultRenderer(String.class, centerRenderer);

How to wrap lines in a jtable cell?

The problem is that the height of rows in JTable is fixed, so it’s not just a matter of having a renderer that wraps; I’m not sure why it doesn’t, but if it did, the wrapped text would be cropped – or maybe that’s exactly what you’re seeing. To adjust row heights, you need to … Read more

Deleting all the rows in a JTable

We can use DefaultTableModel.setRowCount(int) for this purpose, refering to Java’s Documentation: public void setRowCount(int rowCount) Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows … Read more

Auto resizing the JTable column widths

You can try the next: public void resizeColumnWidth(JTable table) { final TableColumnModel columnModel = table.getColumnModel(); for (int column = 0; column < table.getColumnCount(); column++) { int width = 15; // Min width for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer renderer = table.getCellRenderer(row, column); Component comp = table.prepareRenderer(renderer, row, column); width = … Read more

Java Swing JTable; Right Click Menu (How do I get it to “select” aka highlight the row)

like this: table.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { int r = table.rowAtPoint(e.getPoint()); if (r >= 0 && r < table.getRowCount()) { table.setRowSelectionInterval(r, r); } else { table.clearSelection(); } int rowindex = table.getSelectedRow(); if (rowindex < 0) return; if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) { JPopupMenu popup = createYourPopUp(); popup.show(e.getComponent(), e.getX(), e.getY()); … Read more

JTable – Selected Row click event

Here’s how I did it: table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event) { // do some actions here, for example // print first column value from selected row System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString()); } }); This code reacts on mouse click and item selection from keyboard.

How do I drag and drop a row in a JTable?

The following allows JTable re-ordering of a single dragged row: table.setDragEnabled(true); table.setDropMode(DropMode.INSERT_ROWS); table.setTransferHandler(new TableRowTransferHandler(table)); Your TableModel should implement the following to allow for re-ordering: public interface Reorderable { public void reorder(int fromIndex, int toIndex); } This TransferHandler class handles the drag & drop, and calls reorder() on your TableModel when the gesture is completed. /** … Read more

How can I put a control in the JTableHeader of a JTable?

The article How to Use Tables: Using Custom Renderers offers TableSorter as an example of how to detect mouse events on a column header. Using a similar approach, SelectAllHeader extends JToggleButton and implements TableCellRenderer in the example below to achieve a similar effect. A TableModelListener is used to condition the toggle button when all the … Read more

Disable user edit in JTable [duplicate]

You can create a JTable using following code: JTable jTable = new JTable() { private static final long serialVersionUID = 1L; public boolean isCellEditable(int row, int column) { return false; }; }; Basically what we are doing here is overriding isCellEditable and always returning false from it. This will make a non editabe JTabel.