Java JTable change cell color

Say that the cell you would like to render with a different color represents a status (I’ll take Rejected and Approved as examples). I’d then implement a method in my table model called getStatus(int row) which returns the status for any given row. Then, when that is in place, I’d go about creating a cell … Read more

How to sort JTable in shortest way?

As per How to Use Tables: Sorting and Filtering JTable table = new JTable(tableModel); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()); table.setRowSorter(sorter); List<RowSorter.SortKey> sortKeys = new ArrayList<>(25); sortKeys.add(new RowSorter.SortKey(4, SortOrder.ASCENDING)); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); Updated Are you sure it will sort? …Yes import java.awt.EventQueue; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowSorter; import … Read more

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

Put JTable in the JTree

Get rid of the scrollPane, it’s dysfunctional anyway (so far I agree with Russell 🙂 and add the table and its header to the panel, using an appropriate LayoutManager: public MyTableInTreeCellRenderer() { super(); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); table = new JTable(); add(table.getTableHeader()); add(table); } you’ll probably need to tweak the visuals a bit, the left and … Read more

JTable getSelectedRow does not return the selected row index

Look to your code private void jbInit() throws Exception { … jScrollPane1.getViewport().add(jTable1, null); this.getContentPane().add(jButton2, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jScrollPane1, null); jTable1 = new JTable (model); … You add jTable1 to the JScrollPane at first and only then you create jTable1. It’s incorrect way. jTable1 variable doen’t linked now with table you place at the form. I … Read more

How to center cells in JTable

I’ve pared your example down to the essentials: Your custom renderer, r, should condition the alignment, as well as size and color. Override model methods in the model, not in the view. Swing GUI objects should be constructed and manipulated only on the event dispatch thread. Use deriveFont() as required. See also this tutorial section … Read more

JTable + Sorting specific field

Because java.util.Date implements Comparable<Date>, it should be sufficient to let your TableModel return Date.class from getColumnClass() for column two. Use a Custom Renderer to format the date as desired. Addendum: Here’s an example using setDefaultRenderer(). import java.awt.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; /** @see http://stackoverflow.com/questions/4553448 */ … Read more