Using JLists with the ListModel interface

February 13, 2018 Leave a comment

Purists may prefer only to allow the View to communicate with the Model via the Controller but in the types of small applications that we do in schools I think it’s perfectly reasonable for the View to have a direct reference to the Model.

// In the Model
private Set people = new HashSet();
public List getPeople() {
     return new ArrayList(people);
}

// In the View
listModel.clear();
List people = model.getPeople();
for (Person person : people) {
     listModel.addElement(person);
}

 

Advertisements

Basic Java Swing Application using MVC

February 13, 2018 Leave a comment

This is worth working through. It’s a simple pattern that can be used with almost all IAs and in using you can claim complexity and good programming practice. Note especially how easy it would be to create a different interface (View) for the Model. Almost nothing would need to be re-coded as long as the View raised the right events with the Controller.

https://github.com/retrosnob/MVCUsingListeners

Basic Java Programming Course

February 9, 2018 Leave a comment

Using Git with Netbeans

February 8, 2018 Leave a comment

Pygame resource

January 9, 2018 Leave a comment

Program arcade games, apparently. This site has been recommended to me for those interested in Python game development.

http://programarcadegames.com/

Coding Games

December 5, 2017 Leave a comment

Inserting Multiple Rows into a SQLite Database from Java

November 26, 2017 Leave a comment
package sqlitetest;
import java.sql.*;

public class SqliteTest {

    public static void main(String args[]) {
        Connection connection = null;

        try {
            /* The next line will give a ClassNotFound error unless the
               sqlite jar is in the classpath. To add it in Netbeans, 
               right-click on the project, choose Properties, Libraries 
               and add the path to the jar file. On Linux I saved it in 
               /usr/local/sqlite/sqlite-jdbc-3.20.0.jar, but this will be
               different if you are using Mac or Windows.
            */
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:test.db");

            // The database must already have a country table with these fields.
            // In my table there is also a CountryId primary key field, but the
            // database assigns values to that field automatically.
            String sql = "insert into Country (CountryName, Population) values (?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);


            // In larger applications you would probably loop through a list
            // of country objects. This is just a simple demo.

            ps.setString(1, "United States of America");
            ps.setString(2, "320000000");
            ps.addBatch();

            ps.setString(1, "China");
            ps.setString(2, "1400000000");
            ps.addBatch();

            ps.executeBatch();
            connection.close();

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Insert successful");
    }
}