Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2007, 12:32 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
java assignment, need help bad.
I have a programming assignement that i have been working on for quite some time now. I need your guys' help. My assignement is to great a table with data in it. and it will have a combobox and 2 buttons to how it will be sorted( run the program and you'll see how its setup).The table will have a comboBox that includes the 4 headings. We have to use insertion and selection sorting. I have done some of the program, just need help on getting up and running good. There will also be two buttons (“0-9/A-Z” ) and (“9-0/Z-A”). The user will choose by which heading the table should be sorted and then press the button as to how it will be sorted. When that button is pressed the table will be sorted and appear correctly with the correct information for each person. As 2 of the headings are Strings and 2 are integers, you will need to make the sorting of the Last name and age a selection sort and the sorting of the First name and test score insertion sorts. Check out the code, thanks for the help guys.

Code:
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JTable; import javax.swing.JComboBox.*; public class Main implements ActionListener { JPanel p1; Object[] test; JFrame f1; JButton b1,b2; JTable table; JComboBox combo; Container c1; GP g1; JScrollPane scrollpane; public Main() { String[] columnNames = {"First Name","Last Name","Age","Test Scores"}; String[] combodata = {"First Name","Last Name","Age","Test Scores"}; // Smith Dave 12 75 // Johnson Bill 14 72 // Anderson Sara 11 92 // Hoyez Kara 16 83 // Cruz Kyle 13 84 String[] test = {"","","","",""}; b1=new JButton("0-9/A-Z"); b2=new JButton("9-0/Z-A"); b1.addActionListener(this); b2.addActionListener(this); combo = new JComboBox(combodata); combo.setSelectedIndex(3); combo.setEditable(false); combo.addActionListener(this); Object[][] rowData = { {"Dave", "Smith", new Integer(12), new Integer(75)}, {"Bill", "Johnson", new Integer(14), new Integer(72)}, {"Sara", "Anderson", new Integer(11), new Integer(92)}, {"Kara", "Hoyez", new Integer(16), new Integer(83)}, {"Kyle", "Cruz", new Integer(13), new Integer(84)}}; f1 = new JFrame(); table = new JTable(rowData,columnNames); f1.setSize(400,170); c1 = f1.getContentPane(); g1 = new GP(); p1 = new JPanel(); p1.setSize(400,170); //int[] intArray = new int[] {4, 1, 3, -23}; // Arrays.sort(intArray); //System.out.println(rowData[2][3]); p1.add(table); p1.add(combo); p1.add(b1); p1.add(b2); c1.add(p1); f1.show(); } public static void main(String[] args) { Main pw = new Main(); } public class GP extends JPanel { public GP() { } } public void actionPerformed(ActionEvent event) { if (event.getSource() == b1){ Object comboselect = combo.getSelectedItem(); // if (comboselect.equals("First Name")){ // for(int i = 0; i< 4; i++){ // String get2d = rowData.get[0][i]; // get2d = test[i]; //} ///insertion sort // int[] test = {54,23,7,53,4}; //for (int index = 0; index < test.length; index++){ // int key = test[index]; // int position = index; // while (position > 0 && test[position -1] > key){ // test[position] = test[position-1]; // position--; // } // test[position] = key; // } //selection sort //for (int index = 0; index < numbers.length-1; index++){ // min=index; // for(int scan = index+1; scan < numbers.length; scan++){ // if (numbers[scan] < numbers [min]){ // min=scan; //swap values // temp = numbers[min]; // numbeers[min] = numbers[index]; // numbers[index] = temp; // } // } //} //for (int index = 1; index < test.length; index++){ //System.out.println(test[index]); // } //} if (comboselect.equals("Last Name")){} if (comboselect.equals("Age")){} if (comboselect.equals("Test Scores")){} } if (event.getSource() == b2){ Object comboselect = combo.getSelectedItem(); if (comboselect.equals("First Name")){} if (comboselect.equals("Last Name")){} if (comboselect.equals("Age")){} if (comboselect.equals("Test Scores")){} } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-06-2007, 06:53 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class MainSortTest implements ActionListener { JButton b1,b2; JTable table; JComboBox combo; public MainSortTest() { String[] columnNames = {"First Name","Last Name","Age","Test Scores"}; String[] combodata = {"First Name","Last Name","Age","Test Scores"}; // Smith Dave 12 75 // Johnson Bill 14 72 // Anderson Sara 11 92 // Hoyez Kara 16 83 // Cruz Kyle 13 84 // String[] test = {"","","","",""}; b1=new JButton("0-9/A-Z"); b2=new JButton("9-0/Z-A"); b1.addActionListener(this); b2.addActionListener(this); combo = new JComboBox(combodata); combo.setSelectedIndex(3); combo.setEditable(false); // The combo selectedIndex == table headerColumn index // so there is no need for an ActionListener. // combo.addActionListener(this); Object[][] rowData = { {"Dave", "Smith", new Integer(12), new Integer(75)}, {"Bill", "Johnson", new Integer(14), new Integer(72)}, {"Sara", "Anderson", new Integer(11), new Integer(92)}, {"Kara", "Hoyez", new Integer(16), new Integer(83)}, {"Kyle", "Cruz", new Integer(13), new Integer(84)} }; table = new JTable(new DefaultTableModel(rowData,columnNames)); JFrame f1 = new JFrame(); f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c1 = f1.getContentPane(); c1.add(new JScrollPane(table)); JPanel p1 = new JPanel(); // p1.setSize(400,170); p1.add(combo); p1.add(b1); p1.add(b2); c1.add(p1, "Last"); f1.setSize(400,170); f1.setLocation(200,200); f1.setVisible(true); } public static void main(String[] args) { new MainSortTest(); } public void actionPerformed(ActionEvent event) { int index = combo.getSelectedIndex(); JButton button = (JButton)event.getSource(); boolean ascending = button == b1; if(index < 2) sortStrings(index, ascending); else sortIntegers(index, ascending); } private void sortStrings(int col, boolean ascending) { // Collect the values in the column. TableModel model = table.getModel(); String[] colValues = new String[model.getRowCount()]; int[] rowIndices = new int[model.getRowCount()]; for(int j = 0; j < colValues.length; j++) { colValues[j] = (String)model.getValueAt(j, col); rowIndices[j] = j; } // Sort both arrays. for(int j = 0; j < colValues.length; j++) { String datum = colValues[j]; int replaceIndex = j; for(int k = j+1; k < colValues.length; k++) { int n = colValues[k].compareTo(datum); if( (ascending && n < 0) || (!ascending && n > 0) ) { datum = colValues[k]; replaceIndex = k; } } if(replaceIndex != j) { String temp = colValues[j]; colValues[j] = colValues[replaceIndex]; colValues[replaceIndex] = temp; int tempIndex = rowIndices[j]; rowIndices[j] = rowIndices[replaceIndex]; rowIndices[replaceIndex] = tempIndex; } } sortRows(rowIndices); } private void sortIntegers(int col, boolean ascending) { TableModel model = table.getModel(); Integer[] colValues = new Integer[model.getRowCount()]; int[] rowIndices = new int[model.getRowCount()]; for(int j = 0; j < colValues.length; j++) { colValues[j] = (Integer)model.getValueAt(j, col); rowIndices[j] = j; } // Sort arrays. for(int j = 0; j < colValues.length; j++) { Integer datum = colValues[j]; int replaceIndex = j; for(int k = j+1; k < colValues.length; k++) { int n = colValues[k].compareTo(datum); if( (ascending && n < 0) || (!ascending && n > 0) ) { datum = colValues[k]; replaceIndex = k; } } if(replaceIndex != j) { Integer temp = colValues[j]; colValues[j] = colValues[replaceIndex]; colValues[replaceIndex] = temp; int tempIndex = rowIndices[j]; rowIndices[j] = rowIndices[replaceIndex]; rowIndices[replaceIndex] = tempIndex; } } sortRows(rowIndices); } private void sortRows(int[] indices) { Object[][] data = getSortedData(indices); Object[] colIds = getColumnIdentifiers(); DefaultTableModel model = (DefaultTableModel)table.getModel(); model.setDataVector(data, colIds); model.fireTableStructureChanged(); } private Object[][] getSortedData(int[] indices) { DefaultTableModel model = (DefaultTableModel)table.getModel(); int rows = model.getRowCount(); int cols = model.getColumnCount(); Object[][] data = new Object[rows][cols]; for(int j = 0; j < rows; j++) { for(int k = 0; k < cols; k++) data[j][k] = model.getValueAt(indices[j], k); } return data; } private Object[] getColumnIdentifiers() { TableColumnModel model = table.getColumnModel(); int cols = model.getColumnCount(); Object[] colIds = new Object[cols]; for(int j = 0; j < cols; j++) colIds[j] = model.getColumn(j).getIdentifier(); return colIds; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java assignment - couple methods don't know how to figure out Snowboardmylife New To Java 1 04-16-2008 12:52 PM
Java assignment xtianah77 New To Java 1 02-18-2008 01:54 AM
Please help... assignment for school confused2000 New To Java 3 11-12-2007 10:12 AM
for Assignment plz help assamhammad New To Java 1 11-06-2007 10:35 PM
Help with my assignment java toby New To Java 1 08-07-2007 07:59 AM


All times are GMT +3. The time now is 11:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org