hi, i have a football league system that allows data to be placed into a jtable, im having trouble sortin the table tho, does any1 know how 2 sort the data first by points then by goal differece n then by goals scored? thanx
first class:
// Game.java
// Java core packages
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Java exntension packages
import javax.swing.*;
import javax.swing.table.*;
// Beginning of class
public class Menu{
private Object home;
private Object away;
private Object homeScore;
private Object awayScore;
LeagueTable league = new LeagueTable();
// Array lists
String mainmenus[] = {"Enter Result", "Display Results", "Display Table", "Exit"};
String options[] = {"Arsenal", "Everton", "Watford", "Chelsea"};
String goaloptions[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
JTextArea text = new JTextArea(6,100);
JFrame frame = new JFrame("Football League Results List");
// set up GUI
public Menu()
{
results_list();
boolean finish = false;
do{
int mainmenu = JOptionPane.showOptionDial
og(null, "Please Select An Option:", "Main Menu", 0, JOptionPane.PLAIN_MESSAGE,
null, mainmenus, mainmenus [0]);
if (mainmenu == 0)
{
//Entering a result
home = JOptionPane.showInputDialo
g(null, "Enter Home Team", "Home Team", 0, null, options, options[0]);
homeScore = JOptionPane.showInputDialo
g(null, "Enter Home Goals", "Home Team", 0, null, goaloptions, goaloptions[0]);
away = JOptionPane.showInputDialo
g(null, "Enter Away Team", "Away Team", 0, null, options, options[0]);
awayScore = JOptionPane.showInputDialo
g(null, "Enter Away Goals", "Away Team", 0, null, goaloptions, goaloptions[0]);
if (home != away){
league.insertleaguetable ( home, homeScore, away, awayScore);
update();
}
else {JOptionPane.showMessageDi
alog(null,
"Home team and away team can't be the same");}
}
if (mainmenu == 1)
{
display();
}
if (mainmenu == 2)
{
LeagueTable league = new LeagueTable();
}
if (mainmenu == 3)
{
System.exit(0);
}
}
while (finish == false);
}
public void results_list()
{
text.append("Home Team Goals Away Team Goals\n");
}
public void update()
{
text.append(home.toString(
));
text.append(" ");
text.append(homeScore.toSt
ring());
text.append(" ");
text.append(away.toString(
));
text.append(" ");
text.append(awayScore.toSt
ring());
text.append("\n");
}
public void display()
{
frame.getContentPane().add
(BorderLay
out.CENTER
, text);
// Exit application when frame is closed
frame.setDefaultCloseOpera
tion(JFram
e.EXIT_ON_
CLOSE);
frame.setSize(350,500);
frame.setVisible(true);
}
public static void main(String args[])
{
Menu menu = new Menu();
}
}
//End of class
second class:
// LeagueTable.java
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java exntension packages
import javax.swing.*;
import javax.swing.table.*;
public class LeagueTable extends JFrame{
//Constructs the JTable and displays automatically
private DefaultTableModel footballTeams;
private JTable table;
// LeagueTable constructor
public LeagueTable() {
super("Football League Table");
initTable();
// create a JTable for footballTeams DefaultTableModel
table = new JTable(footballTeams);
// layout GUI components
Container container = getContentPane();
container.add(new JScrollPane(table), BorderLayout.CENTER);
setSize(500,300);
setDefaultCloseOperation(E
XIT_ON_CLO
SE);
setVisible(true);
}
// end of LeagueTable constructor
private void initTable() {
//TableSorter sorter = new TableSorter(aTableModel);
//initTable.setModel(sorte
r);
//sorter.addMouseListenerT
oHeaderInT
able(initT
able);
//creates a DefaultTableModel to hold the footballTeams records
footballTeams = new DefaultTableModel();
// add columns to DefaultTableModel
footballTeams.addColumn(" Name ");
footballTeams.addColumn( "Played");
footballTeams.addColumn("W
on");
footballTeams.addColumn("D
rawn");
footballTeams.addColumn("L
ost");
footballTeams.addColumn("F
or");
footballTeams.addColumn("A
gainst");
footballTeams.addColumn("G
D");
footballTeams.addColumn("P
ts");
// add names and default data to DefaultTableModel
String[] arsenal = { "Arsenal", "0", "0", "0", "0", "0", "0", "0", "0"};
footballTeams.addRow(arsen
al);
String[] everton = { "Everton", "0", "0", "0", "0", "0", "0", "0", "0"};
footballTeams.addRow(evert
on);
String[] watford = { "Watford", "0", "0", "0", "0", "0", "0", "0", "0"};
footballTeams.addRow(watfo
rd);
String[] chelsea = { "Chelsea", "0", "0", "0", "0", "0", "0", "0", "0"};
footballTeams.addRow(chels
ea);
}
//Inserts data in to the table in the correct place
public void insertleaguetable(Object home, Object homeScore, Object away, Object awayScore)
{
String homeTeam = home.toString();
int homeGoals = Integer.parseInt(homeScore
.toString(
));
String awayTeam = away.toString();
int awayGoals = Integer.parseInt(awayScore
.toString(
));
for(int i=0; i<4; i++){
if (home == footballTeams.getValueAt(i
, 0)){
int played = Integer.parseInt(footballT
eams.getVa
lueAt(i, 1).toString());
footballTeams.setValueAt(n
ew Integer(played + 1), i, 1);
int won = Integer.parseInt(footballT
eams.getVa
lueAt(i, 2).toString());
int points = Integer.parseInt(footballT
eams.getVa
lueAt(i, 8).toString());
int goalsFor = Integer.parseInt(footballT
eams.getVa
lueAt(i, 5).toString());
footballTeams.setValueAt(n
ew Integer(goalsFor + homeGoals), i, 5);
int goalsAgainst = Integer.parseInt(footballT
eams.getVa
lueAt(i, 6).toString());
footballTeams.setValueAt(n
ew Integer(goalsAgainst + awayGoals), i, 6);
int gD = Integer.parseInt(footballT
eams.getVa
lueAt(i, 7).toString());
footballTeams.setValueAt(n
ew Integer(gD + homeGoals - awayGoals), i, 7);
if (homeGoals > awayGoals) {
footballTeams.setValueAt(n
ew Integer(won + 1), i, 2);
footballTeams.setValueAt(n
ew Integer(points + 3), i, 8);
}
int scoredraw = Integer.parseInt(footballT
eams.getVa
lueAt(i, 3).toString());
if (homeGoals == awayGoals && homeGoals !=0 && awayGoals !=0) {
footballTeams.setValueAt(n
ew Integer(scoredraw + 1), i, 3);
footballTeams.setValueAt(n
ew Integer(points + 2), i, 8);
}
int draw = Integer.parseInt(footballT
eams.getVa
lueAt(i, 3).toString());
if (homeGoals == awayGoals && homeGoals ==0 && awayGoals == 0) {
footballTeams.setValueAt(n
ew Integer(draw + 1), i, 3);
footballTeams.setValueAt(n
ew Integer(points + 1), i, 8);
}
int lost = Integer.parseInt(footballT
eams.getVa
lueAt(i, 4).toString());
if (homeGoals < awayGoals) {
footballTeams.setValueAt(n
ew Integer(lost + 1), i, 4);
}
}
if (away == footballTeams.getValueAt(i
, 0)){
int points = Integer.parseInt(footballT
eams.getVa
lueAt(i, 8).toString());
int played = Integer.parseInt(footballT
eams.getVa
lueAt(i, 1).toString());
footballTeams.setValueAt(n
ew Integer(played + 1), i, 1);
int goalsFor = Integer.parseInt(footballT
eams.getVa
lueAt(i, 5).toString());
footballTeams.setValueAt(n
ew Integer(goalsFor + awayGoals), i, 5);
int goalsAgainst = Integer.parseInt(footballT
eams.getVa
lueAt(i, 6).toString());
footballTeams.setValueAt(n
ew Integer(goalsAgainst + homeGoals), i, 6);
int gD = Integer.parseInt(footballT
eams.getVa
lueAt(i, 7).toString());
footballTeams.setValueAt(n
ew Integer(gD + awayGoals - homeGoals), i, 7);
int won = Integer.parseInt(footballT
eams.getVa
lueAt(i, 2).toString());
if (homeGoals < awayGoals) {
footballTeams.setValueAt(n
ew Integer(won + 1), i, 2);
footballTeams.setValueAt(n
ew Integer(points + 3), i, 8);
}
int scoredraw = Integer.parseInt(footballT
eams.getVa
lueAt(i, 3).toString());
if (homeGoals == awayGoals && homeGoals !=0 && awayGoals !=0) {
footballTeams.setValueAt(n
ew Integer(scoredraw + 1), i, 3);
footballTeams.setValueAt(n
ew Integer(points + 2), i, 8);
}
int draw = Integer.parseInt(footballT
eams.getVa
lueAt(i, 3).toString());
if (homeGoals == awayGoals && homeGoals ==0 && awayGoals == 0) {
footballTeams.setValueAt(n
ew Integer(draw + 1), i, 3);
footballTeams.setValueAt(n
ew Integer(points + 1), i, 8);
}
int lost = Integer.parseInt(footballT
eams.getVa
lueAt(i, 4).toString());
if (homeGoals > awayGoals) {
footballTeams.setValueAt(n
ew Integer(lost + 1), i, 4);
}
}
}
}
}