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.
Tag: object oriented programming
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
Java program bug due to multiple declaration of variable in code and the fix
I created a small Java class to accept a date. However, when I tried to use it, I got a NullPointerException error. This post is just to illustrate how it was fixed. 1 public class DateForm { 2 JFormattedTextField txtMyDate; //This is a variable at class level 3 4 dateForm () { 5 JTextFormattedField … Continue reading Java program bug due to multiple declaration of variable in code and the fix
Java compilation Error: no suitable constructor found for Box(int)
I spent a lot of time trying to figure out why my Java program was giving me a compilation error on the Box constructor that said Error: no suitable constructor found for Box(int). My attempt was to simply use the Box class in the Swing Library. I figured out why. It so happens that in … Continue reading Java compilation Error: no suitable constructor found for Box(int)
Create Java JAR file with execution entry point (main)
$ jar -cfe ContactListTestDrive.jar ContactListTestDrive ContactListTestDrive.class ContactList.class Contact.class <enter> where: JAR file = ContactListTestDrive.jar Entry Point class = ContactListTestDrive Entry Point class = ContactListTestDrive.class (This is the class with the main() function) ContactList.class and Contact.class are the other classes to add to the JAR file. Normally this is automatically done when you use an IDE … Continue reading Create Java JAR file with execution entry point (main)
UML Diagrammer / Class Generator Tool on Linux
Astah Community Edition Today's object oriented languages require extensive use of objects, their inter-relationships and methods. There are several UML tools out there like UMlet, Umbrello on Linux, but this one from astah stands out. You can try the community edition before deciding to purchase a full version. It enables the creation of objects, building … Continue reading UML Diagrammer / Class Generator Tool on Linux
Simplest explanation to Objects in Object Oriented Programming
Teaching myself to program in Java by following the book Head First Java by Kathy Sierra and Bert Bates has been one of the best things to have happened this year, so far. After trying several years to understand object-oriented programming concepts, finally found this book that provides one of the best introductions. Finally, I … Continue reading Simplest explanation to Objects in Object Oriented Programming