7 Replies - 2297 Views - Last Post: 19 April 2011 - 05:11 PM Rate Topic: -----

#1 sjbpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Jtable column sorting

Posted 18 April 2011 - 01:41 PM


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.table.*;

public class voting implements ActionListener {
	
	JFrame frame;
	JFrame frame2;
	JFrame frame3;
	JFrame frame4;
	JButton button;
	JButton button2;
	JButton button3;
	JButton button4;
	JTextField A;
	JTextField B;
	JTextField C;
	JTextField D;
	JPanel panel;
	JPanel panel2;
	JPanel panel3;
	JPanel buttons;
	String awins = "A wins ";
	String bwins = "B wins ";
	String cwins = "C wins";
	String dwins = "D wins";
	int avb=0;
	int avc=0;
	int avd=0;
	int bva=0;
	int bvc=0;
	int bvd=0;
	int cva=0;
	int cvb=0;
	int cvd=0;
	int dva=0;
	int dvb=0;
	int dvc=0;
	int ga;
	int gb;
	int gc;
	int gd;
	int avbres;
	int avcres;
	int avdres;
	int bvcres;
	int bvdres;
	int cvdres;
	int total;
	String geta;
	String getb;
	String getc;
	String getd;
	String test = "test";
	
	public voting() {
		
		JLabel a = new JLabel("                          Alan");
		JLabel b = new JLabel("                          Bob");
		JLabel c = new JLabel("                          Carter");
		JLabel d = new JLabel("                          Dilan");
		
		A = new JTextField(1);
		B = new JTextField(1);
		C = new JTextField(1);
		D = new JTextField(1);
		
		button = new JButton("Submit");
		button2 = new JButton("Tally Results"); 
		button3 = new JButton("Sort Results");
		button4 = new JButton("Res");
		
		button.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		A.addActionListener(this);
		B.addActionListener(this);
		C.addActionListener(this);
		D.addActionListener(this);
		
		frame = new JFrame("Ballot");
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		panel = new JPanel(new GridLayout(4,2));
		buttons = new JPanel(new FlowLayout());
		
		panel.add(a);
		panel.add(A);
		panel.add(B)/>;
		panel.add(B)/>;
		panel.add(c);
		panel.add(C);
		panel.add(d);
		panel.add(D);
		
		buttons.add(button);
		buttons.add(button2);
		buttons.add(button3);
		buttons.add(button4);
		
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(buttons, BorderLayout.SOUTH);
		
		frame.setSize(500, 300);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	}
	
	public static int getwinner(int aa, int bb) {
		if(aa > bb) {
			return aa;
		} else{
		return bb;
		}
	}
	
	public String getwin(int win) {
		if(avb > bva) {
			return awins;
		}else 
			return bwins;
		}
		
		
		
	

	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==button) {
			
			total++;
			
			geta = A.getText();
			getb = B.getText();
			getc = C.getText();
			getd = D.getText();
			
			ga = Integer.parseInt(geta);
			gb = Integer.parseInt(getb);
			gc = Integer.parseInt(getc);
			gd = Integer.parseInt(getd);
			
			if(ga < gb) {
				
				avb++;
				
			}
			
			if(ga < gc) {
				
				avc++;
				
			}
			
			if(ga < gd) {
				
				avd++;
				
			}
			
			if(gb < ga) {
				
				bva++;
				
			}
			
			if(gb < gc) {
				
				bvc++;
				
			}
			
			if(gb < gd) {
				
				bvd++;
				
			}
			
			if(gc < ga) {
				
				cva++;
				
			}
			
			if(gc < gb) {
				
				cvb++;
				
			}
			
			if(gc < gd) {
				
				cvd++;
				
			}
			
			if(gd < ga) {
				
				dva++;
				
			}
			
			if(gd < gb) {
				
				dvb++;
				
			}
			
			if(gd < gc) {
				
				dvc++;
			}
			
			frame2 = new JFrame("Table");
			panel2 = new JPanel();
			
			String data[][] = {{"", "        A", "        B", "        C", "        D"},
					{"A", "-", "" + avb, "" + avc, "" + avd},
					{"B", "" + bva, "-", "" + bvc, "" + bvd},
					{"C", "" + cva, "" + cvb, "-", "" + cvd},
					{"D", "" + dva, "" + dvb, "" + dvc, "-"}};

			String col[] = {"A", "B", "C", "D", "E"};
			
			DefaultTableModel model = new DefaultTableModel(data,col);
			
			JTable table = new JTable(model);
			table.setShowGrid(true);
			table.setShowVerticalLines(true);
			table.setGridColor(Color.gray);
			panel2.add(table,BorderLayout.CENTER);
			
			frame2.add(panel2);
			frame2.setVisible(false);
			frame2.setSize(400, 200);
			
			
		}
		
		if(e.getSource()==button2) {
			
			frame2.setVisible(true);
			System.out.println(total);
			
		}
		
		if(e.getSource()==button3) {
			
			avbres = getwinner(avb, bva);
			avcres = getwinner(avc, cva);
			avdres = getwinner(avd, dva);
			bvcres = getwinner(bvc, cvb);
			bvdres = getwinner(bvd, dvb);
			cvdres = getwinner(cvd, dvc);
			
			frame3 = new JFrame("Sort Results");
			panel3 = new JPanel();
			
			String data2[][] = {{"     Pair", "     Margin"},
							  {"A v B", "" + avbres},
							  {"A V C", "" + avcres },
							  {"A V D", "" + avdres},
							  {"B V C", "" + bvcres},
							  {"B V D", "" + bvdres},
							  {"C V D", "" + cvdres}};
			
			String col2[] = {" Pair ", " Winner "};
			
			DefaultTableModel model2 = new DefaultTableModel(data2,col2);
			
			JTable table2 = new JTable(model2);
			TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model2);
			table2.setRowSorter(sorter);
			table2.setShowGrid(true);
			table2.setShowVerticalLines(true);
			table2.setGridColor(Color.gray);
			panel3.add(table2,BorderLayout.CENTER);
			
			frame3.add(panel3);
			frame3.setSize(400,300);
			frame3.setVisible(true);
			
		}
		
		if(e.getSource()==button4) {
			
			frame4 = new JFrame("Res");
			JPanel panel4 = new JPanel();
			
			
			String data3[][] = {{"     Pair", "     Winner"},
					  		 {"A v B", "" },
					  		 {"A V C", "" },
					  		 {"A V D", "" },
					  		 {"B V C", "" },
					  		 {"B V D", "" },
					  		 {"C V D", "" }};
			
			String col3[] = { "A", "B"};
			
			DefaultTableModel model3 = new DefaultTableModel(data3,col3);
			
			JTable table3 = new JTable(model3);
			table3.setShowGrid(true);
			table3.setShowVerticalLines(true);
			table3.setGridColor(Color.gray);
			panel4.add(table3, BorderLayout.CENTER);
			
			frame4.add(panel4);
			frame4.setVisible(true);
			frame4.setSize(400, 300);
			
		}
		
	}
	
	public static void main(String[] args) {
		
		new voting();
	
	}
	
}





Hi,

I'm writing a program that ranks the winner of an election using ranked pairs. I was wondering if it was possible to sort columns. the margin column in table2 needs to be sorted from largest number to smallest number. I've tried TableRowSorter but as expected that has not done anything. Is there anything else I can use?

Is This A Good Question/Topic? 0
  • +

Replies To: Jtable column sorting

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Jtable column sorting

Posted 18 April 2011 - 05:41 PM

Never a good idea to sort arrays

Look at macosxnerd101 tutorial on parallel arrays
http://www.dreaminco...arallel-arrays/

You should write a class CandidateAndVote that implements Comparable

Have an array of CandidateAndVote object and sort it using a standard sort method from the API

The class should look like this

class CandidateAndVote implements Comparable<CandidateAndVote> {

   private String candidate;
   private int vote;

   // constructor
   CandidateAndVote(String candidate, int vote) {
      this.candidate = candidate;
      this.vote = vote;
   }

   public int compareTo(CandidateAndVote other) {
     return vote - other.vote;
   }


Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: Jtable column sorting

Posted 18 April 2011 - 05:51 PM

I agree with pbl. This is also more reason to separate your program GUI and data/state from each other. Design a StateManager or DataManager class to store your data and handle the sorting there. Then just pull your data and re-populate the JTable.

Also, probably a good idea to create classes for your individual JFrames. Cramming a lot of GUI code into one class is a recipe for confusion and clutter.
Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Jtable column sorting

Posted 18 April 2011 - 06:00 PM

View Postmacosxnerd101, on 18 April 2011 - 07:51 PM, said:

Also, probably a good idea to create classes for your individual JFrames. Cramming a lot of GUI code into one class is a recipe for confusion and clutter.

Yes you can create a class DisplaySort extends JFrame that receive the array of CandidateAndVote as parameter to the constructor

class DisplaySort extends JFrame {

   String[] header = {"Candidate", "Vote");
   DisplaySort(CandidateAndVote[] cav) {
      super("Sort results");
      Arrays.sort(cav);
      

Was This Post Helpful? 1
  • +
  • -

#5 sjbpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Re: Jtable column sorting

Posted 19 April 2011 - 03:29 AM

View Postmacosxnerd101, on 18 April 2011 - 05:51 PM, said:

I agree with pbl. This is also more reason to separate your program GUI and data/state from each other. Design a StateManager or DataManager class to store your data and handle the sorting there. Then just pull your data and re-populate the JTable.

Also, probably a good idea to create classes for your individual JFrames. Cramming a lot of GUI code into one class is a recipe for confusion and clutter.



I understand creating different classes will help to reduce the confusion and improve it, however, to do this will I have to extend every class from DataManager to make this work. For example if i create a class called Tally, to separate the JFrame used to tabulate the individual results will it have to be: public class Tally extends DataManager?
Was This Post Helpful? 0
  • +
  • -

#6 sjbpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Re: Jtable column sorting

Posted 19 April 2011 - 04:18 AM


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class voting implements ActionListener {
	
	JFrame frame;
	JButton button;
	JButton button2;
	JButton button3;
	JButton button4;
	JTextField A;
	JTextField B;
	JTextField C;
	JTextField D;
	JPanel panel;
	JPanel buttons;
	String awins = "A wins ";
	String bwins = "B wins ";
	String cwins = "C wins";
	String dwins = "D wins";
	int avb=0;
	int avc=0;
	int avd=0;
	int bva=0;
	int bvc=0;
	int bvd=0;
	int cva=0;
	int cvb=0;
	int cvd=0;
	int dva=0;
	int dvb=0;
	int dvc=0;
	int ga;
	int gb;
	int gc;
	int gd;
	int avbres;
	int avcres;
	int avdres;
	int bvcres;
	int bvdres;
	int cvdres;
	int total;
	String geta;
	String getb;
	String getc;
	String getd;
	String test = "test";
	
	public voting() {
		
		JLabel a = new JLabel("                          Alan");
		JLabel b = new JLabel("                          Bob");
		JLabel c = new JLabel("                          Carter");
		JLabel d = new JLabel("                          Dilan");
		
		A = new JTextField(1);
		B = new JTextField(1);
		C = new JTextField(1);
		D = new JTextField(1);
		
		button = new JButton("Submit");
		button2 = new JButton("Tally Results"); 
		button3 = new JButton("Sort Results");
		button4 = new JButton("Res");
		
		button.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		A.addActionListener(this);
		B.addActionListener(this);
		C.addActionListener(this);
		D.addActionListener(this);
		
		frame = new JFrame("Ballot");
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		panel = new JPanel(new GridLayout(4,2));
		buttons = new JPanel(new FlowLayout());
		
		panel.add(a);
		panel.add(A);
		panel.add(B)/>;
		panel.add(B)/>;
		panel.add(c);
		panel.add(C);
		panel.add(d);
		panel.add(D);
		
		buttons.add(button);
		buttons.add(button2);
		buttons.add(button3);
		buttons.add(button4);
		
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(buttons, BorderLayout.SOUTH);
		
		frame.setSize(500, 300);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	}
	
	public static int getwinner(int aa, int bb) {
		if(aa > bb) {
			return aa;
		} else{
		return bb;
		}
	}
	
	public String getwin(int win) {
		if(avb > bva) {
			return awins;
		}else 
			return bwins;
		}
		
		
		
	

	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==button) {
			
			total++;
			
			geta = A.getText();
			getb = B.getText();
			getc = C.getText();
			getd = D.getText();
			
			ga = Integer.parseInt(geta);
			gb = Integer.parseInt(getb);
			gc = Integer.parseInt(getc);
			gd = Integer.parseInt(getd);
			
			if(ga < gb) {
				
				avb++;
				
			}
			
			if(ga < gc) {
				
				avc++;
				
			}
			
			if(ga < gd) {
				
				avd++;
				
			}
			
			if(gb < ga) {
				
				bva++;
				
			}
			
			if(gb < gc) {
				
				bvc++;
				
			}
			
			if(gb < gd) {
				
				bvd++;
				
			}
			
			if(gc < ga) {
				
				cva++;
				
			}
			
			if(gc < gb) {
				
				cvb++;
				
			}
			
			if(gc < gd) {
				
				cvd++;
				
			}
			
			if(gd < ga) {
				
				dva++;
				
			}
			
			if(gd < gb) {
				
				dvb++;
				
			}
			
			if(gd < gc) {
				
				dvc++;
			}
			
			
		}
		
		if(e.getSource()==button2) {
			
			new Tally();
			
		}
		
		if(e.getSource()==button3) {
			
			new sort();
			
		}
		
		if(e.getSource()==button4) {
			
			new Sorted();
			
		}
		
	}
	
	public static void main(String[] args) {
		
		new voting();
	
	}
	
}


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class Tally extends voting {
	
	public Tally() {
		
		JFrame Tally = new JFrame("Tally");
		JPanel panel = new JPanel();
		
		String data[][] = {{"", "        A", "        B", "        C", "        D"},
				{"A", "-", "" + avb, "" + avc, "" + avd},
				{"B", "" + bva, "-", "" + bvc, "" + bvd},
				{"C", "" + cva, "" + cvb, "-", "" + cvd},
				{"D", "" + dva, "" + dvb, "" + dvc, "-"}};

		String col[] = {"A", "B", "C", "D", "E"};
		
		DefaultTableModel model = new DefaultTableModel(data,col);
		
		JTable table = new JTable(model);
		table.setShowGrid(true);
		table.setShowVerticalLines(true);
		table.setGridColor(Color.gray);
		panel.add(table,BorderLayout.CENTER);
		
		Tally.add(panel);
		Tally.setVisible(false);
		Tally.setSize(400, 200);
		
	}
	
	public static void main(String[] args) {
		
		new Tally();
	}

}


import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Color;

public class sort extends voting {
	
	JFrame sort = new JFrame("Sort");
	JPanel sortPanel = new JPanel();

	public sort() {
		
		avbres = getwinner(avb, bva);
		avcres = getwinner(avc, cva);
		avdres = getwinner(avd, dva);
		bvcres = getwinner(bvc, cvb);
		bvdres = getwinner(bvd, dvb);
		cvdres = getwinner(cvd, dvc);
		
		sort = new JFrame("Sort Results");
		sortPanel = new JPanel();
		
		String data2[][] = {{"     Pair", "     Margin"},
						  {"A v B", "" + avbres},
						  {"A V C", "" + avcres },
						  {"A V D", "" + avdres},
						  {"B V C", "" + bvcres},
						  {"B V D", "" + bvdres},
						  {"C V D", "" + cvdres}};
		
		String col2[] = {" Pair ", " Winner "};
		
		DefaultTableModel model2 = new DefaultTableModel(data2,col2);
		
		JTable table2 = new JTable(model2);
		table2.setShowGrid(true);
		table2.setShowVerticalLines(true);
		table2.setGridColor(Color.gray);
		sortPanel.add(table2,BorderLayout.CENTER);
		
		sort.add(sortPanel);
		sort.setSize(400,300);
		sort.setVisible(true);
	}
	
}

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Color;

public class Sorted {
	
	JFrame Sorted;
	JPanel sortedPanel;

	public Sorted() {
		
		
		Sorted = new JFrame("Res");
		sortedPanel = new JPanel();
		
		
		String data3[][] = {{"     Pair", "     Winner"},
				  		 {"A v B", "" },
				  		 {"A V C", "" },
				  		 {"A V D", "" },
				  		 {"B V C", "" },
				  		 {"B V D", "" },
				  		 {"C V D", "" }};
		
		String col3[] = { "A", "B"};
		
		DefaultTableModel model3 = new DefaultTableModel(data3,col3);
		
		JTable table3 = new JTable(model3);
		table3.setShowGrid(true);
		table3.setShowVerticalLines(true);
		table3.setGridColor(Color.gray);
		sortedPanel.add(table3, BorderLayout.CENTER);
		
		Sorted.add(sortedPanel);
		Sorted.setVisible(true);
		Sorted.setSize(400, 300);
		
	}
}




I've separated my Jframes into separate classes however, now anytime I input votes into my ballot and click a button to show the table of results all of the values are set to 0. This is happening because when i say:


if(e.getSource()==button2) {
			
			new Tally();	
		}




it is printing the table with initial values which are obviously all 0. Is there a way of updating the table before it is shown?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: Jtable column sorting

Posted 19 April 2011 - 03:10 PM

You could define your Tally constructor to accept an Integer[][], and you can use that for your TableModel.

Also, I see no reason that Tally should extend Voting. There should either be a tally() method within the Voting class, or Tally should be a separate, non-related class altogether.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Jtable column sorting

Posted 19 April 2011 - 05:11 PM

View Postsjbpa, on 19 April 2011 - 05:29 AM, said:

I understand creating different classes will help to reduce the confusion and improve it, however, to do this will I have to extend every class from DataManager to make this work. For example if i create a class called Tally, to separate the JFrame used to tabulate the individual results will it have to be: public class Tally extends DataManager?

Not really does not need to extends the class, and ever if it was the case what's the problem

You CandidateAndVote class can have a method that return and array of object[][] based of an array of CandidateAndVote object[].
The class that extends the JFrame to display the JTable does not have to know what these objects are

class CandidateAndVote implements Comparable<CandidateAndVote> {

   private String candidate;
   private int vote;

   // constructor
   CandidateAndVote(String candidate, int vote) {
      this.candidate = candidate;
      this.vote = vote;
   }

   public int compareTo(CandidateAndVote other) {
     return vote - other.vote;
   }

   public static Object[][] arrayForJTable(CandidateAndVote[] array) {
      Arrays.sort(array);
      Object[][] obj = new Object[array.length][2];
      for(int i = 0; i < array.length; ++i) {
        obj[i][0] = array[i].candidate;
        obj[i][1] = new Integer(array[i].vote);
      }
      return obj;
   }
}


class DisplaySort extends JFrame {

   String[] header = {"Candidate", "Vote");

   DisplaySort(CandidateAndVote[] cav) {
      super("Sorted results");
      JTable table = new JTable(CandidateAndVote.arrayForJTable(cav), header);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1