How to code multiple line tooltips with bold text

Many Java programs use tool tips in Java Swing to display more information when the mouse cursor hovers over a button or text field. There is a method of making text within that tool tip bold, italics, colored and more. Below is an illustration of the command that uses HTML tags to display that formatting … Continue reading How to code multiple line tooltips with bold text

GridLayout struggle when it has excess rows and how to overcome

In my Java code, I am using GridLayout (line shown in red) in code below: frame = new JFrame("Contacts Details"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainPanel = new JPanel(); buttonPanel = new JPanel(); mainPanel.setLayout(new GridLayout(9, 3)); buttonPanel.setLayout(new GridLayout(1, 3)); The result was this: Name [                        ] (Text Box for name) City [                        ] (Text Box for City) Cell [                        … Continue reading GridLayout struggle when it has excess rows and how to overcome

How to filter selection of files by extension in Java Swing’s JFileChooser

Display of files of a particular extension like .txt or .csv in JFileChooser can be done using the FileNameExtensionFilter in Java as follows: fileChooser = new JFileChooser(); fileChooser.setSelectedFile(null); FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("CSV Files", "csv"); fileChooser.setFileFilter(fileFilter); fileChooser.showOpenDialog(frame);

How to make a form refer to the parent frame using a JDialog

Below code segment shows how to use a JDialog to refer to a parent frame. MyApp.java public class MyApp { JDialog dialog; JFrame parentFrame; public static void main(String[] args) { MyApp app = new MyApp(); app.gui(); } public void gui() { //The parentFrame parentFrame = new JFrame(); parentFrame = setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //The dialog dialog = new … Continue reading How to make a form refer to the parent frame using a JDialog

Read data from a JTable created from AbstractTableModel

If you have a JTable in Java created with an AbstractTableModel, you can read the data from it, using following: int rowCount = testTable.getRowCount(); for (int i = 0; i < rowCount; i++) { String mColumn1 = (String) testTable.getModel().getValueAt(i, 0); String mColumn2 = (String) testTable.getModel().getValueAt(i, 1); } Alternatively, you could also try where the JTable … Continue reading Read data from a JTable created from AbstractTableModel

Delete a selected row in JTable with a custom model

DefaultTableModel model = (DefaultTableModel) myTable.getModel(); //Get reference to model model.removeRow(x); //Remove row x You can also add a method deleteRow(int row) to your custom model class code as in: public class MyTableModel extends AbstractModel { private Vector dataVector = new Vector(); String[] columnNames = {"First Name", "Last Name", "Age"}; public int getRowCount() { if (dataVector.size() … Continue reading Delete a selected row in JTable with a custom model

Delete all rows from a Java JTable with a custom TableModel

I want to clear all rows in a custom TableModel. Unfortunately, a custom TableModel in Java does not allow use of most efficient of row clearing methods, which only applies to a DefaultTableModel like that shown below: myTableModel.getDataVector().removeAllElements(); myTableModel.fireTableDataChanged(); To do an all clear like the above, for a custom TableModel, you need to reset … Continue reading Delete all rows from a Java JTable with a custom TableModel