how to print selected rows JTable

Seems to work okay for me… import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.print.PrinterException; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; public class TestPrint { public static void main(String[] args) { new TestPrint(); } public TestPrint() { EventQueue.invokeLater(new … Read more

How to print a JTable object in the Java application

You obviously didn’t read the links provided in your previous question. From the Printing section of How to use Tables Printing JTable provides a simple API for printing tables. The easiest way to print out a table is to invoke JTable.print with no arguments: try { if (! table.print()) { System.err.println(“User cancelled printing”); } } … Read more

How to refresh data in JTable I am using TableModel

You’ve done it the hard way. First of all, you’ve implemented directly from TableModel and secondly you’ve failed to implement the listener requirements… Instead, try extending from the AbstractTableModel instead, which already includes the implementations of the listener registration and notification. You will need to provide a method that will allow you to add a … Read more

problem formatting fields in a JTable – differences between Integer and Double

how did Walter Laan says in his thread Never give up! Never surrender! EDIT: I can’t resist, but due to my poor English I dare not to commenting why, where and how is that possible, nor works correctly, for confirmations I added Rob’s two (little bit) modified class for TableColumnRendering …, import java.awt.EventQueue; import java.math.RoundingMode; … Read more

JTable enter key

The default Key Binding for Enter is the selectNextRowCell action in the table’s WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map. You can substitute your own action, as outlined below. private static final String solve = “Solve”; KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve); table.getActionMap().put(solve, new EnterAction()); … private class EnterAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { … Read more

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

JTable Multiple Header Rows

We had the same requirement in our last project. What I have found is an Implementation for a GroupableTableHeader on java2s.com. However, I have pimped it a bit, although I cannot recall what exactly. Beneath is the implementation of the three classes as how we use them. ColumnGroup.java import java.awt.Component; import java.awt.Dimension; import java.util.ArrayList; import … Read more

Putting JComboBox into JTable

Extend JTable with this code: @Override public TableCellEditor getCellEditor(int row, int column) { Object value = super.getValueAt(row, column); if(value != null) { if(value instanceof JComboBox) { return new DefaultCellEditor((JComboBox)value); } return getDefaultEditor(value.getClass()); } return super.getCellEditor(row, column); } This will create a unique JComboBox cell editor for each combo box you get the a value for.

How to keep a single column from being reordered in a JTable?

This is the solution that I used to prevent the 1st column from being re-ordered private int columnValue = -1; private int columnNewValue = -1; tblResults.getColumnModel().addColumnModelListener(new TableColumnModelListener() { public void columnAdded(TableColumnModelEvent e) {} public void columnMarginChanged(ChangeEvent e) {} public void columnMoved(TableColumnModelEvent e) { if (columnValue == -1) columnValue = e.getFromIndex(); columnNewValue = e.getToIndex(); } public … Read more

tech