Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vectors capturing top and lowest

Status
Not open for further replies.

tourguy

Technical User
Nov 6, 2003
27
US
Hi, I got this code from java.sun.com, and i inserted into a class called classCars. I also created a class called classYr. What I want to do in the classYr is put a code that can capture the top 3 highest, lowest 3 price. I tried looking for example codes online, but coudln't find anything. I don't know how the code for this, so it's very tough for me. Thanks!
 
here is the code:


import javax.swing.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;





public class TestTableSelect extends JFrame
{

public TestTableSelect() {

// TODO Auto-generated constructor stub
}

public TestTableSelect(String input)
{
super("Table select");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getTable(input));
setSize (650, 250);
setVisible(true);
}

public JPanel getTable(String input)
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(100, 250));

Vector data = dataVector();
Vector column = headVector();


JTable table = new JTable(data, column);



table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
table.setSelectionBackground(Color.white);
table.setRowSelectionInterval(0,getSelectedIndex(data, input));
table.scrollRectToVisible(table.getCellRect(table.getSelectedRow(), 0, true));



javax.swing.JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(650, 150));

panel.add(scrollPane, BorderLayout.NORTH);

return panel;

}

public Vector dataVector()
{
Vector data = new Vector();

{
Vector data1 = new Vector();
data1.add("0123");
data1.add("$13324.23");
data1.add("5");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0456");
data1.add("$16922.54");
data1.add("4");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0784");
data1.add("$28314.39");
data1.add("3");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0546");
data1.add("$28324.65");
data1.add("2");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0458");
data1.add("$18324.23");
data1.add("6");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0468");
data1.add("$13904.24");
data1.add("5");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0987");
data1.add("$39024.12");
data1.add("4");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0518");
data1.add("$9324.23");
data1.add("6");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0978");
data1.add("$18324.23");
data1.add("7");
data.add(data1);
}
{
Vector data1 = new Vector();
data1.add("0314");
data1.add("$8324.23");
data1.add("3");
data.add(data1);
}

return data;

}
public Vector headVector()
{
Vector column = new Vector();
column.add("Identification Number");
column.add("Annual Income");
column.add("cars");


return column;
}

public int getSelectedIndex(Vector data, String key)

{
for(int i = 0; i < data.size(); i++)
{
Vector data1 = (Vector)data.get(i);
if(((String)data1.get(0)).trim().equalsIgnoreCase(key))
{
return i;
}
}
return 0;



}

}
 
In my opinion, this is not the way it works.
You may not just take a couple of code, found somewhere, post it to a forum and ask the people to solve your very tough problem.
Your description shows, that you are a very early beginner.

Since every class is a class, no intermediate programmer would name his class classCars.
He would stick to the codingStyle, but wouldn't call it ClassCars.
Perhaps: Cars.

A class Yr is very intuitive! What's that?

And we don't like to be told where to put our code.
If we think, we solve the problem best in putting the code into the 'Cars'-class, why not?

Programming isn't just to look for some sample-code and putting it together.
If you don't have the time to learn java, you will have to pay a developer.

Sorry if I overacted here.
 

You have to sort the cars by prices yourself. Then add sorted data to each element of vector and create a JTable from the vector.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top