|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Question related with EventListHello, I wonder if you could help.
I am making a Hockey Simulator game. Right now i am working on the fantasy draft. I am using a Mysql database, i found how to use the sortlist and pretty much everything. My issue is to update the view regarding to what i am selecting. I have the jtable and a jbutton whenever i press the Jbutton it picks the SelectedRow with the selectedColumn and it does an update in the mysql Table, my sql query is to see all the players where the team is at null, the update works correctly but then after i don't know how to tell the EventList that the source changed. And to redo the sql query. Right now it doesn't do anything. import ca.odell.glazedlists.BasicEventList; import ca.odell.glazedlists.EventList; import ca.odell.glazedlists.GlazedLists; import ca.odell.glazedlists.SortedList; import ca.odell.glazedlists.gui.TableFormat; import ca.odell.glazedlists.swing.EventTableModel; import ca.odell.glazedlists.swing.TableComparatorChooser; import java.sql.*; import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.*; public class JTableEtSorter extends JFrame implements ActionListener{ JButton j = new JButton("Clique ici"); JTable t; Connection conn; JFrame f = new JFrame() ; EventList Joueurs; SortedList sortedJoueur; String[] propertyNames = new String[] {"idJoueur", "nomJoueur", "prenomJoueur", "ageJoueur"}; String[] columnNames = new String[] {"idJoueur", "nomJoueur", "prenomJoueur", "ageJoueur"}; public static class JoueurID { private int idJoueur; private String nomJoueur; private String prenomJoueur; private int ageJoueur; public JoueurID(int idJoueur,String nomJoueur,String prenomJoueur,int ageJoueur) { this.idJoueur=idJoueur; this.nomJoueur=nomJoueur; this.prenomJoueur=prenomJoueur; this.ageJoueur=ageJoueur; } public JoueurID() { } public int getIdJoueur() { return idJoueur; } public int getAgeJoueur() { return ageJoueur; } public String getNomJoueur() { return nomJoueur; } public String getPrenomJoueur() { return prenomJoueur; } } public void actionPerformed(ActionEvent v) { Object source = v.getSource(); String s =((JButton)source).getActionCommand(); if(source == j) { try{ Statement s2 = conn.createStatement(); s2.execute("Update Joueur SET Equipe="+3+" WHERE IdJoueur="+t.getValueAt(t.getSelectedRow(),0)+" AND EQUIPE IS NULL"); s2.close(); ////////////////////////This is where i should have something like.... joueurs.notify or whatever.... to tell the list that it changed and to redo the query without what i just picked. } catch (SQLException ex) { System.out.println("error" + ex.getMessage()); } } } public void faireLeTraitement() { j.addActionListener(this) ; conn = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager .getConnection("jdbc:mysql://localhost:3306/hockey?user=root"); } catch (Exception ex) { System.out.println("error" + ex.getMessage()); } int compteur=0; // InsertionBD test= new InsertionBD(); // test.CreationOrdre(); // Vector data = new Vector(); //int[] tab= test.RetournerTableau(); Joueurs = new BasicEventList(); try{ Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT idJoueur,nomJoueur,PrenomJoueur,AgeJoueur FROM JOUEUR where EQUIPE IS NULL"); //ResultSet rs = s.executeQuery("SELECT idJoueur,nomJoueur,PrenomJoueur,AgeJoueur FROM JOUEUR where EQUIPE="+3); ResultSetMetaData md = rs.getMetaData(); //int columns = md.getColumnCount(); int idJoueur; String nomJoueur; String prenomJoueur; int ageJoueur; while (rs.next()) { idJoueur= Integer.parseInt(rs.getString(1)); nomJoueur=rs.getString(2); prenomJoueur=rs.getString(3); ageJoueur=Integer.parseInt(rs.getString(4)); Joueurs.add(new JoueurID(idJoueur,nomJoueur,prenomJoueur,ageJoueur) ); } rs.close(); s.close(); } catch (SQLException ex1) { System.out.println(ex1.getMessage()); } sortedJoueur = new SortedList(Joueurs, null); TableFormat tf = GlazedLists.tableFormat(JoueurID.class,propertyNames,columnNames); t = new JTable(new EventTableModel(sortedJoueur, tf)); new TableComparatorChooser(t, sortedJoueur, true); f.setLayout(new BorderLayout()); f.add(new JScrollPane(t), BorderLayout.NORTH); f.add(j,BorderLayout.CENTER); // show the frame f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } Sorry some stuff are in french. I have also my main... which is not a lot cause i am actually just testing that thing. public class CreerDraft { public static void main(String args[]) { // Draft draft=new Draft(); JTableEtSorter test=new JTableEtSorter(); test.faireLeTraitement(); } Thank you, if you can help :) if not i will find by myself. Thanks for taking some time to read it as well. |
|
|
Re: Question related with EventList
Hello Jonathan,
If I keep in mine the seperation of concerns principles I think you need an extra class that has the responsibility of being triggered when an EventList is basically an enhanced rowset and that the database has changed. That would be the easiest way: create a class that has an update method that basically reselects the data from the database and repopulates your event list. However there are neater solutions to your problem: When dealing with a standalone implementation: 1) you have your Jouers class which is a data object (typically called a DTO short for data transfer object most of the time it is a serializable class which might contain extra business logic), you might want to add the logic of registration of property change listeners and broadcast property change events whenever a property changes of course. 2) you have a class that is responsible for interacting with the database: e.g. it fetches, updates, inserts and deletes Joueurs which is typically called a data access object or DAO, this class might have a cache: of joueurs and when it needs to update the database it also updates the cache, to have this working nicely all db access must pass through this object. The cache can be your event list. Whenever you do an insert in the db you insert inthe event list and it will broadcast an "insert event", whenever you delete, you remove from the event list and it will broadcast an "delete event". Whenever you update: you do nothing :-) because for updates you might embed your event list in an observable event list which will automatically listen for changes in your dto objects if they have the ability of dealing properly with property change listeners. If you go client-server or even multiple tiers you might want to add some kind of JoueursService interface and implementations on top of the DTO and DAO that allow for broadcasting from the server to the clients when the cache of Jouers has changed on the server and update it locally with the changes, then the logic of the cache would be on the client side and on the server side. Gerrit Jonathan Chayer wrote: Hello, I wonder if you could help. -- -------------------- Marble Consulting ---------------------- Gerrit Cap http://www.marble.be/ OO Solutions Engineer Gerrit.Cap@... Marble Consulting gsm : +32 475 72.94.36 Vinkenbosstraat 13 tel : +32 16 89.50.55 B-3001 Heverlee fax : +32 16 89.50.58 |
| Free Forum Powered by Nabble | Forum Help |