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
Tag: java GUI
How to display message dialog in Java Swing using JOptionPane.showMessageDialog
Following code segments shows how to display a message dialog in Java Swing: import javax.swing.JOptionPane; JOptionPane.showMessageDialog(mainFrame, "Data processing has been completed"); where, mainFrame = the parent frame in which the dialog should be displayed. mainFrame can also be replaced with null.
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
How to remove all rows from a Java JTable created using Custom TableModel
To remove all rows from a Java JTable created using Custom TableModel, try the following: MyModel myTableModel = (MyModel) myTable.getModel(); for (int i = myTableModel.getRowCount()-1; i >= 0; i--) myTableModel.deleteRow(i); Define the deleteRow(int row) method in the MyModel class.
Display a popup message in Java Swing
At times, we need to show quick messages in a small window on screen to users in Java. Here is a quick way to do this. import javax.swing.JOptionPane JOptionPane.showMessageDialog(null, "This is my Message Dialog");
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