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 program
How to close the program in Java completely in Swing
A Java Swing application can be closed using the following code segment to retrieve the parent of the container, check if it is a JFrame and then dispose the JFrame: public class btnCancelListener implements ActionListener { public void actionPerformed(ActionEvent ev) { Container bframe = btnCancel.getParent(); bframe = bframe.getParent(); while (! (bframe instanceof JFrame)) { System.out.println("Done"); … Continue reading How to close the program in Java completely in Swing
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
Where to place a text file for use with a Java project in Eclipse
I placed a text file myapp.txt in the folder ~/workspace/PayrollApp/src/org/username/payrollapp. However, it was not opening in the Java program. To make it work correctly, I had to do use the following path in the Java program when opening the file: src/org/username/payrollapp/pay.txt Code as follows: try { FileReader fr = new FileReader("src/org/username/payrollapp/pay.txt"); BufferedReader br = new … Continue reading Where to place a text file for use with a Java project in Eclipse
Where to place SQLite 3 database for use in a Java Project in Eclipse
My Eclipse Java project path is as follows: ~/workspace/PayrollApp/src/org/username/payrollapp/payroll.db I had placed the SQLite database file payroll.db in the above path, and found it was not getting used when I ran the Java program. Upon inspecting the contents of ~/workspace/PayrollApp, I found that payroll.db was there with 0 bytes. That implied that the file needs … Continue reading Where to place SQLite 3 database for use in a Java Project in Eclipse
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");