Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

kenji mapes

Ranch Hand
+ Follow
since Jun 16, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by kenji mapes

Sorry the / was a typo.

I have two txt files to process:

One is bad and one is good.


Good one:

123456789|Smith|John|freshman|15|4.0
987654321|Doe|Joe|sophomore|35|3.9
846834676|Wang|Chin|senior|95|3.7
957654321|Russell|June|junior|75|3.5
957654320|Russell|Harold|senior|195|3.2
456789321|Ortiz|Juan|freshman|25|3.8
987123456|Wade|Warren|sophomore|56|2.5
578315853|Ellis|Anita|sophomore|45|3.8

Bad one:

123456789|Smith|John|freshman|15|4.0
987654321|Doe|Joe|sophomore|35|3.9
248615937|Doe|Jane|senior|35|3.9
846834676|Wang|Chin|senior|95|3.7
9jtd85765|Duval|Janice|senior|100|3.7
957654321|Russell|June|junior|75|3.5
957654320|Russell|Harold|senior|195|3.2
456789321|Ortiz|Juan|freshman|25|3.8
519372486|Fields|Alice
987123456|Wade|Warren|sophomore|56|2.5
578315853|Ellis|Anita|sophomore|45|3.8


So I was not given too much criteria on what to check for, only to check the "format." So, from the providd files, it appears an invallid format, would be one without all the proper field regardless of the characters, etc.

So how would I check? Maybe number of delimeters? i was also thinking that I could put it into the array, and then just delete it?

thanks for your help guys.
15 years ago
By "invalid" I mean not folowing the above format.
15 years ago
I am reading from a file, and storing it into an array. Each line must have the format of

SSN|first|last|grade\humCredits|gpa

If there a student with invalid info, I am supposed to reject it and store the rest into the array.

It must have a boolean return value. I have written a basic definition, but I can't see to figure how to reject invalid text fle line, and get to the next line.

Any ideas?
15 years ago
Thanks Michael, I will try that. I was thinking that the vector would be accessible in the same folder, same package, but I guess my idea of scope is not fully refined.
16 years ago
I think there is a setLocation method(). Put in the parameters of (x,y) in pixels for the start point; that point should be the upper left of the applet frame. setLocation is in JFrame, and should be in applet too. However I am not positive.

Try it, and let me know if that works.
16 years ago
Let me see if I can explain this without posting code.

I have a GUI class that extends JFrame and implements listeners, so the calss itself is the listener. The handlers are overriden in this calss.

In my driver class, I create a vector to hold instances of of type derived Vehicles (my abstract class), with the specific instances being "vanilla" cars, family cars, economy cars, and motorcycles.

I create the vector outside of the GUI calss so that I can reference it outside of a GUI instance. I pass the vector to the GUi constructor, and it adds the vector to a JList and a JComboBox. That is a bit reundant, but I wanted to play with both components so I could learn them.

Now for the event handling, I wanted to be able to add vehicles, remove vehicles, and sell vehicles from the handles on the buttons named "sell," "remove," and "add."

I'd like for the vector to be updated with these actions. I have code in the definition of actionPerformed.

For example, when a user wants to add a vehicle, I have input to select the vehicle type, and then prompt the user for info to store, and then pass to a new vehicle instantiation. I wanted this new object to be added to the vector, and then added to the combo box and list.

However, in my class definition it says that I cannot access the vector refeence variable that is declared in main, and then passed to the GUI contructor.

What gives? Thanks.

Kenji
16 years ago
Wow, Ken, nice through and extremely helpful replies. I really appreciate it. This is a great learning experience.

It is helping me to improve immensely, and your thoughts are noted. Yes, making my JFrame derived class itself the listener is a bit antithetical to OOP, but for this scenario it seemed appropriate.
16 years ago
Ok Ken, I have had some time to work and think on this. I cleaned up my code a little; it was just that I was making so many changes, that it became a little unwieldy to worry about aesthetics at the time.

Let me see if I can elaborate. My JCL class has a constructor for the GUI. Obviously I create the GUI. As far as a Vector to store vehicle instances, I wanted to reference outside of the GUI instance, so I though about putting it in main, which I did.

Now since I wanted to consolidate as much as I could into one class I implemented the ActionEvent and ItemEvent interfaces, so I could make the class itself the listener. I registered the JButtons, and JMenuItems with their respective listeners. Also, I overode, but did not fully redefine the
actionPerformed or itemStateChanged methods. They are ouside of the constructor, and I thought that the class could "see" them. However, when I comile I get erros stating that it cannot resolve the references in the listener method bodies; ie actionPerformed and itemStateChanged.

Also, what I meant about the Vector methods, was using them in the said methods to manipulate, process the Vector with the built-in methods when a user adds, removes, or sells a vehicle instance.

I could not understand your explanation for this.

I know that a Vector and Array are not the same, just like an ArrayList or LinkedList. I commented most of the stuff out, but your can't tell in HTML format.

I kept some of the commented out code to annotate my changes, and to keep "residual" code, just in case my changes did not work. I wasn't sure whether to create my vector in main, in the constructor, or somewhere in the JCL class.

The latter is what I thought would be best but I could not make it happen. Where would I declare the list? Before the constructor?

Here's my updated code, so many you can give me some guidance.

16 years ago
Got it. Appended the system variables and user variables in advanced/environment variables with C:\j2sdk1.4.2_04\bin (which is where my J Archive Tool is, of course).

At the cmd prompt, jar then worked! Awesome. Thanks.

Best,
Kenji
16 years ago
Maybe it's just me but the Java Tutorial documentation is not helpful. I know the commands but can't seem to create one.

Every time I go into a command line or a shell in Win, it says the command jar is unrecognized. I get the same when I go into the Gnome Terminal on my Linux box.

Thanks.
16 years ago
Thanks Craig, much appreciated. I'll take a look at this and see what I can make of it.

Thanks again.
16 years ago
have the Vector creation in main which is outside of my JFrame derived class. The user's input for the car lot's size is the argument for the size of the vector. Not sure how to handle invalid user input's ouside on negative numbers, but that is another problem.

So I send the vector and the vector size variable to my JFrame derived class, JoesCarLot, henceforth referred to a JCL. And I do pretty much everything needed in the constructor: create widgets, add them, register listeners, etc. I implemented the interface, so I presume that I can listen with the calss itself and use the keyword this as sending a listener object.

Anyway, I cannot seem to use vector methods anywhere. Also, in my itemStateChanged and actionPerformed overriden methods, I have the definitions, but the references to the buttons are not working. But the worst part, again, is the vector processing which is preventing me from referencing the vector. I tried a data structure creation in the vehilce class, thought about it in the vehicle constructor, in main, and in the JCL constructor, or somewhere in the JCL class. It even crossed my mind to deep copy or clone the vector or array, and reference it that way.

I have spent a lot of time changing around the code to no avail, and at this point I am lost. I would appreciate any suugestions, comments, anecdotes, witicisms, criticisms, etc that you have.

Here's my code

package project4;

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

/* Kenji Mapes <kenergyone@yahoo.com>
* CMP 218 J1
* Project 3: "Class EconomyCar"
* Due 11/30/05 11:59 pm
* Vehicle.java, Car.java, EconomyCar.java, FamilyCar.java, Motorcycle.java, Project3Driver.java, , proj3.txt
*/

public class JoesCarLot extends JFrame implements ActionListener, ItemListener //SelectionListener,
{
//data fields

public int length;
public int size;

/* Default constructor JoesCarLot
* No parameters
* setups up the look and feel of the GUI object
*/

public JoesCarLot(Vector vehicles, int carLotSize)
{

//since this is a derived class, a call to the parent class, JFrame,
//with the keyowrd super() ensures proper initialization

super();

//store array/vector size coming in in a local variable

size = carLotSize;

/*

//test to see if incoming paramter is an array of type vehicle

if(o instanceof Vehicle)
{
Vehicle vehicles = o;
}
else
{
System.exit(0);
}

//prompt user for car lot size which dictates how many vehicles
// car be added to list, created, size of array, etc.


int carLotSize;
String carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);

Object vehicles[ ] = new Vehicle[ carLotSize ];
/*

if(object.length < vehicles.getArraySize)
{
carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);
}

//while loop to handle valid input from user

while(carLotSize < 1)
{
carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);
}

*/

// FRAME/contentPane setup

//set title and font to JCL object

setTitle("Joe's Car Lot");
setFont(new Font("Arial", Font.BOLD, 20));

//creates reference for an ImageIcon object

Icon carIcon = new ImageIcon("s_coopermini.gif");
Icon carIcon2 = new ImageIcon("red_coopermini.jpeg");

//get the content pane to be able to reference it

Container container = getContentPane();

//set Layout with appropriate layout manager
//in this case multiple managers will be used

container.setLayout(new BorderLayout());

//create color objects to reference for special colors

Color colorTan = new Color(245,245,220);
Color colorDkGrey = new Color(49, 79, 79);
Color colorChoco = new Color(205, 105, 30);
Color colorAlmond = new Color(255,235,205);
Color colorKhaki = new Color(189, 183, 107);
Color colorOlive = new Color(107, 142, 35);
Color colorSiena = new Color(160, 82, 45);
Color colorSBrown = new Color(139, 69, 19);
Color colorWheat = new Color(245, 222, 179);

//set frame background color

container.setBackground(Color.GRAY);


//FRAME MENU BAR SETUP

//create Menu bar object
//and set it in frame

JMenuBar jmbMenu = new JMenuBar();
setJMenuBar(jmbMenu);

//create menu bar object "file"
//and add it to the menu

JMenu jmFile = new JMenu("File");
jmbMenu.add(jmFile);


//create menu item Open for File

JMenuItem jmiOpen = new JMenu("Open File");

/*register listener
*since this class implements the Listener interface for this object
*the class itslef is the listener and can be referenced with this
*/

jmiOpen.addItemListener(this);

//add "File" button to JMenu

jmFile.add(jmiOpen);

//create menu item Save File for File

JMenuItem jmiSave = new JMenu("Save File");

/*register listener
*since this class implements the Listener interface for this object
*the class itslef is the listener and can be referenced with this
*/

jmiSave.addItemListener(this);

//add "Save" button to JMenu

jmFile.add(jmiSave);

//create menu item Open for File

JMenuItem jmiExit = new JMenu("Exit GUI");

/*register listener
*since this class implements the Listener interface for this object
*the class itslef is the listener and can be referenced with this
*/

jmiExit.addItemListener(this);

//add "Exit" button to JMenu

jmFile.add(jmiExit);

//create menu bar object "help"
//and add it to the menu

JMenu jmHelp = new JMenu("Help");

/*register listener
*since this class implements the Listener interface for this object
*the class itslef is the listener and can be referenced with this
*/

jmHelp.addItemListener(this);

//add "Help" button to JMenu

jmbMenu.add(jmHelp);

//PANELS and NESTED PANELS

//create JPanel objects and
//add them to the frame

/*create first JPanel
*set layout for panel
*set background color
*/

//JPANEL 1

JPanel jpPanel1 = new JPanel();
jpPanel1.setLayout(new FlowLayout());
jpPanel1.setBackground(colorKhaki);

//create JButton instance "OK" for future use possibly

JButton jbtok = new JButton("OK");

//create JLabel to hold image for title panel
//prior to JTextArea

JLabel jlRedCoopMin = new JLabel(carIcon);

//add JLabel

jpPanel1.add(jlRedCoopMin);

//create uneditable text area for greeting

JTextArea jtaGreet = new JTextArea("Welcome to Joe's Car Lot's User Interface");
jtaGreet.setFont(new Font("Times", Font.ITALIC, 20));

//prevent JTextArea from being edited by user

jtaGreet.setEditable(false);

//add greeting to panel 1

jpPanel1.add(jtaGreet);

//create second JLabel to hold image for title panel
//after to JTextArea greeting

JLabel jlRedCoopMin2 = new JLabel(carIcon);
jpPanel1.add(jlRedCoopMin2);

//add panel to north area of Frame (content pane)

getContentPane().add(jpPanel1,BorderLayout.NORTH);

//create second JPanel object
//set layout

//JPANEL 2

JPanel jpPanel2 = new JPanel();
jpPanel2.setLayout(new FlowLayout());

//make background color contrasting

jpPanel2.setBackground(colorTan);

//create border for panel 2

Border panel2Border = new TitledBorder("Vehicle List");
jpPanel2.setBorder(panel2Border);

/*create labels for top of list
*purpose: defines what properties are in list contingent upon
*vehicle type
*/

JLabel carListHeader = new JLabel();
carListHeader.setText(" Car listed with: Veh ID# Year Color Model #Doors Price *mileage for family car");
jpPanel2.add(carListHeader);

JLabel motoListHeader = new JLabel();
motoListHeader.setText(" Motorcycles listed with: Veh ID# Year Model Price Engine Size");
jpPanel2.add(motoListHeader);

//create array of vehicles and add vehicles to it
/*
Vehicle vehicles[ ] = new Vehicle[ carLotSize ];

vehicles[ 0 ] = new FamilyCar( );
vehicles[ 1 ] = new EconomyCar( 1999, 5, "Grey", 13385,
"Honda Civic" );
vehicles[ 2 ] = new Motorcycle( );
vehicles[ 3 ] = new Motorcycle( 1987, "Harley Davidson Sportster",
4995, 1200 );
vehicles[ 0 ] = new FamilyCar( );
vehicles[ 4 ] = new EconomyCar( 2005, 5, "Green", 13385,
"Cooper Mini" );
vehicles[ 5 ] = new Motorcycle( );
vehicles[ 6 ] = new Motorcycle( 2003, "Honda CBR 600RR",
9995, 600 );

vehicles[ 7 ] = new FamilyCar( );
vehicles[ 8 ] = new EconomyCar( 1970, 2, "Yellow", 13385,
"Porsche Boxster" );
vehicles[ 9 ] = new Motorcycle( );
*/

//create JList for panel 2

JList jlVeh = new JList(vehicles);

//jlveh.add(o);

//edit JList properties

//jlVeh.selectionMode(SINGLE_SELECTION);
//jlVeh.setVisibleAmount(4);

//create JScrollBar for JList

JScrollBar jpP2Scroll = new JScrollBar(JScrollBar.VERTICAL);
getContentPane().add(jpP2Scroll, BorderLayout.CENTER);

//create JLabel for combo box here to inform user

JLabel comboHeader = new JLabel();
comboHeader.setText("Select vehicle here, and then choose appropriate action");

//set JLabel color for combo box

comboHeader.setForeground(Color.RED);

//create JComboBox for panel 2

JComboBox jcbVeh = new JComboBox(vehicles);

//add JList to panel 2

jpPanel2.add(jlVeh);

//add heading above combo box

jpPanel2.add(comboHeader);

//add JComboBox to panel 2

jpPanel2.add(jcbVeh);

//add second JPanel to the frame

getContentPane().add(jpPanel2, BorderLayout.CENTER);

//JPANEL 3

//create third JPanel

JPanel jpPanel3 = new JPanel();
jpPanel3.setLayout(new BorderLayout());
jpPanel3.setBackground(colorKhaki);

//JPANEL 3 ALPHA

//create first nested sub-JPanel to add to third MAIN panel

JPanel jpPanel3a = new JPanel();
jpPanel3a.setLayout(new FlowLayout());
jpPanel3a.setBackground(Color.DARK_GRAY);

//create border object to create border on panel 3a
//set it into panel 3a
Border panel3aBorder = new BevelBorder(BevelBorder.RAISED);
jpPanel3a.setBorder(panel3aBorder);

//create JLabel for panel 3a and set text
//color and literal

JLabel jlcarLotSz = new JLabel();
jlcarLotSz.setForeground(Color.RED);

//maximum vehicles allow will be user input defined
//carLotSize

jlcarLotSz.setText("Maximum # vehicles allowed: " + carLotSize);

//add JLabel to panel 3a

jpPanel3a.add(jlcarLotSz);

//add the panel to the NORTH sector of JPanel 3

jpPanel3.add(jpPanel3a, BorderLayout.NORTH);

//JPANEL 3 BRAVO

//create second nested sub-JPanel to add into third MAIN panel
//set layout

JPanel jpPanel3b = new JPanel();
jpPanel3b.setLayout(new FlowLayout(10));
jpPanel3b.setBackground(colorKhaki);

//create titled border around this panel
//and set

Border panel3bBorder = new TitledBorder("Vehicle List Actions");
jpPanel3b.setBorder(panel3bBorder);

//create buttons for JPanel 3a
//add, remove, sell

JButton jbtAdd = new JButton("Add");
JButton jbtRemove = new JButton("Remove");
JButton jbtSell = new JButton("Sell");

//set colors for panel 3b JButtons

jbtAdd.setBackground(colorTan);
jbtRemove.setBackground(colorTan);
jbtSell.setBackground(Color.PINK);

//create tool tip texts to inform user
//further of button actions

jbtAdd.setToolTipText("Adds a vehicle to the list");
jbtRemove.setToolTipText("Removes a vehicle from the list");
jbtSell.setToolTipText("Sells selected vehicle");

/*register listener for three JButtons: Add, Remove, Sell
*since this class implements the listener interface for this object
*the class itslef is the listener and can be referenced with this
*/

jbtAdd.addActionListener(this);
jbtRemove.addActionListener(this);
jbtSell.addActionListener(this);

//add the Add, Remove, Sell, buttons to the Panel

jpPanel3b.add(jbtAdd);
jpPanel3b.add(jbtRemove);
jpPanel3b.add(jbtSell);

//add second nested sub-JPanel to third MAIN panel

jpPanel3.add(jpPanel3b, BorderLayout.CENTER);

//add the third MAIN JPanel to the Frame with
//the nested panels already added

getContentPane().add(jpPanel3,BorderLayout.EAST);

//create fourth main JPanel that will go into the frame
//set layout

JPanel jpPanel4 = new JPanel();
jpPanel4.setLayout(new FlowLayout());

//set border and background color

Border border4 = new LineBorder(Color.RED, 2);
jpPanel4.setBorder(border4);
jpPanel4.setBackground(colorKhaki);

//create EXIT JButton for panel 4

JButton jbtExit = new JButton("Exit system!");

//set background red to alert user

jbtExit.setBackground(Color.RED);

//set tool tip text System Exit button

jbtExit.setToolTipText("Exits system - INFORMATION MAY BE LOST!");

//add listener to jbtExit button
jbtExit.addActionListener(this);

//add button to panel

jpPanel4.add(jbtExit);

//add panel 4 to the south portion of the frame

getContentPane().add(jpPanel4,BorderLayout.SOUTH);

}

//actionPerformed method overriden from ActionListener interface

/* void itemStateChanged
* Parameters: event object
* No return statement - statements executed as conditions permit
*/

public void actionPerformed(ActionEvent e)
{
/*

if(e.getSource() == jbtAdd)
{

}

if(e.getSource() == jbtRemove)
{

}

if(e.getSource() == jbtSell)
{

}

if(e.getSource() == jbtExit)
{
JOptionPane.showMessageDialog(null, "System is Exiting", "For Your Information:", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

*/

}//end of ActionPerformed()

//itemStateChanged method overriden from ItemListener interface

/* void itemStateChanged
* Parameters: event object
* No return statement - statements executed as conditions permit
*/

public void itemStateChanged(ItemEvent e)
{ /*
if(e.getSource() == jmiOpen)
{

}
if(e.getSource() == jmiSave)
{

}
if(e.getSource() == jmhelp)
{

}
*/
}//end of ItemStateChanged()

}//end of class

package project4;

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


/* Kenji Mapes <kenergyone@yahoo.com>
* CMP 218 J1
* Project 3: "Class EconomyCar"
* Due 11/30/05 11:59 pm
* Vehicle.java, Car.java, EconomyCar.java, FamilyCar.java, Motorcycle.java, Project3Driver.java, , proj3.txt
*/
public class Project4 extends JFrame implements ActionListener, ItemListener //SelectionListener,
{

public static void main(String[] x)
{

//prompt user for car lot size which dictates how many vehicles
// car be added to list, created, size of array, etc.


int carLotSize;
String carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);

while(carLotSize < 1)
{
carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);
}



Vector vehicles = new Vector(carLotSize);

vehicles.addElement(new FamilyCar());

vehicles.addElement(new EconomyCar( 1999, 5, "Grey", 13385,
"Honda Civic" ));
vehicles.addElement(new Motorcycle( ));
vehicles.addElement(new Motorcycle( 1987, "Harley Davidson Sportster",
4995, 1200 ));



vehicles.addElement(new EconomyCar( 2005, 5, "Green", 13385,
"Cooper Mini" ));
vehicles.addElement(new Motorcycle( ));
vehicles.addElement(new Motorcycle( 2003, "Honda CBR 600RR",
9995, 600 ));

vehicles.addElement(new FamilyCar( ));
vehicles.addElement(new EconomyCar( 1970, 2, "Yellow", 13385,
"Porsche Boxster" ));
vehicles.addElement(new Motorcycle( ));
vehicles.addElement(new FamilyCar( ));
vehicles.addElement(new EconomyCar( 1970, 2, "Yellow", 13385,
"Porsche Boxster" ));
vehicles.addElement(new Motorcycle( ));


//instantiate JoesCarLot object

//set title and font for JFrame type class
//set JFrame size, as visible, and close operation
//parameters pass an array of vehicles

Project4 p4 = new Project4(vehicles, carLotSize);
p4.setFont(new Font("Arial", Font.ITALIC, 4));
p4.setSize(900,800);
p4.setVisible(true);
p4.setDefaultCloseOperation(p4.EXIT_ON_CLOSE);

System.out.print(vehicles.size());




//display array which is in JList to screen

//System.out.println(vehicles.getArraySize());

}//end of main


/* Default constructor JoesCarLot
* No parameters
* setups up the look and feel of the GUI object
*/




public Project4(Vector vehicles, int carLotSize)
{


//since this is a derived class, a call to the parent class, JFrame,
//with the keyowrd super() ensures proper initialization

super();


/*
*
//test to see if incoming paramter is an array of type vehicle

if(o instanceof Vehicle)
{
Vehicle vehicles = o;
}
else
{
System.exit(0);
}

//prompt user for car lot size which dictates how many vehicles
// car be added to list, created, size of array, etc.


int carLotSize;
String carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);

Object vehicles[ ] = new Vehicle[ carLotSize ];
/*
if(object.length < vehicles.getArraySize)
{
carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);
}

//while loop to handle valid input from user

while(carLotSize < 1)
{
carLotInput = JOptionPane.showInputDialog("Please enter size of car lot(nonnegative integer):");
carLotSize = Integer.parseInt(carLotInput);
}

*/

// FRAME/contentPane setup

//set title and font to JCL object

setTitle("Joe's Car Lot");
setFont(new Font("Arial", Font.BOLD, 20));

//creates reference for an ImageIcon object

Icon carIcon = new ImageIcon("s_coopermini.gif");
Icon carIcon2 = new ImageIcon("red_coopermini.jpeg");

//get the content pane to be able to reference it

Container container = getContentPane();

//set Layout with appropriate layout manager
//in this case multiple managers will be used

container.setLayout(new BorderLayout());

//create color objects to reference for special colors

Color colorTan = new Color(245,245,220);
Color colorDkGrey = new Color(49, 79, 79);
Color colorChoco = new Color(205, 105, 30);
Color colorAlmond = new Color(255,235,205);
Color colorKhaki = new Color(189, 183, 107);
Color colorOlive = new Color(107, 142, 35);
Color colorSiena = new Color(160, 82, 45);
Color colorSBrown = new Color(139, 69, 19);
Color colorWheat = new Color(245, 222, 179);

//set frame background color

container.setBackground(Color.GRAY);


//FRAME MENU BAR SETUP

//create Menu bar object
//and set it in frame

JMenuBar jmbMenu = new JMenuBar();
setJMenuBar(jmbMenu);

//create menu bar object "file"
//and add it to the menu

JMenu jmFile = new JMenu("File");
jmbMenu.add(jmFile);

//create menu item Open for File

JMenuItem jmiOpen = new JMenu("Open File");
jmFile.add(jmiOpen);

//create menu item Save File for File

JMenuItem jmiSave = new JMenu("Save File");
jmFile.add(jmiSave);

//create menu item Open for File

JMenuItem jmiExit = new JMenu("Exit GUI");
jmFile.add(jmiExit);

//create menu bar object "help"
//and add it to the menu

JMenu jmHelp = new JMenu("Help");
jmbMenu.add(jmHelp);

//PANELS and NESTED PANELS

//create JPanel objects and
//add them to the frame

//create first JPanel
//set layout for panel
//set background color

//JPANEL 1

JPanel jpPanel1 = new JPanel();
jpPanel1.setLayout(new FlowLayout());
jpPanel1.setBackground(colorKhaki);

//create JButton instance "OK" for future use possibly

JButton jbtok = new JButton("OK");

//create JLabel to hold image for title panel
//prior to JTextArea

JLabel jlRedCoopMin = new JLabel(carIcon);

//add JLabel

jpPanel1.add(jlRedCoopMin);

//create uneditable text area for greeting

JTextArea jtaGreet = new JTextArea("Welcome to Joe's Car Lot's User Interface");
jtaGreet.setFont(new Font("Times", Font.ITALIC, 20));

//prevent JTextArea from being edited by user

jtaGreet.setEditable(false);

//add greeting to panel 1

jpPanel1.add(jtaGreet);

//create second JLabel to hold image for title panel
//after to JTextArea greeting

JLabel jlRedCoopMin2 = new JLabel(carIcon);
jpPanel1.add(jlRedCoopMin2);

//add panel to north area of Frame (content pane)

getContentPane().add(jpPanel1,BorderLayout.NORTH);

//create second JPanel object
//set layout

//JPANEL 2

JPanel jpPanel2 = new JPanel();
jpPanel2.setLayout(new FlowLayout());

//make background color contrasting

jpPanel2.setBackground(colorTan);

//create border for panel 2

Border panel2Border = new TitledBorder("Vehicle List");
jpPanel2.setBorder(panel2Border);

//create labels for top of list
//purpose: defines what properties are in list contingent upon
//vehicle type

JLabel carListHeader = new JLabel();
carListHeader.setText(" Car listed with: Veh ID# Year Color Model #Doors Price *mileage for family car");
jpPanel2.add(carListHeader);

JLabel motoListHeader = new JLabel();
motoListHeader.setText(" Motorcycles listed with: Veh ID# Year Model Price Engine Size");
jpPanel2.add(motoListHeader);

//create array of vehicles and add vehicles to it

/*Vehicle vehicles[ ] = new Vehicle[ carLotSize ];

vehicles[ 0 ] = new FamilyCar( );
vehicles[ 1 ] = new EconomyCar( 1999, 5, "Grey", 13385,
"Honda Civic" );
vehicles[ 2 ] = new Motorcycle( );
vehicles[ 3 ] = new Motorcycle( 1987, "Harley Davidson Sportster",
4995, 1200 );
vehicles[ 0 ] = new FamilyCar( );
/*vehicles[ 4 ] = new EconomyCar( 2005, 5, "Green", 13385,
"Cooper Mini" );
vehicles[ 5 ] = new Motorcycle( );
vehicles[ 6 ] = new Motorcycle( 2003, "Honda CBR 600RR",
9995, 600 );

vehicles[ 7 ] = new FamilyCar( );
vehicles[ 8 ] = new EconomyCar( 1970, 2, "Yellow", 13385,
"Porsche Boxster" );
vehicles[ 9 ] = new Motorcycle( );*/

//create JList for panel 2

JList jlVeh = new JList(vehicles);

//jlveh.add(o);

//edit JList properties

//jlVeh.selectionMode(SINGLE_SELECTION);
//jlVeh.setVisibleAmount(4);

//create JScrollBar for JList

JScrollBar jpP2Scroll = new JScrollBar(JScrollBar.VERTICAL);
getContentPane().add(jpP2Scroll, BorderLayout.CENTER);

//create JLabel for combo box here to inform user

JLabel comboHeader = new JLabel();
comboHeader.setText("Select vehicle here, and then choose appropriate action");

//set JLabel color for combo box

comboHeader.setForeground(Color.RED);


//create JComboBox for panel 2

JComboBox jcbVeh = new JComboBox(vehicles);

//add JList to panel 2

jpPanel2.add(jlVeh);


//add heading above combo box

jpPanel2.add(comboHeader);

//add JComboBox to panel 2

jpPanel2.add(jcbVeh);

//add second JPanel to the frame

getContentPane().add(jpPanel2, BorderLayout.CENTER);

//JPANEL 3

//create third JPanel

JPanel jpPanel3 = new JPanel();
jpPanel3.setLayout(new BorderLayout());
jpPanel3.setBackground(colorKhaki);

//JPANEL 3 ALPHA

//create first nested sub-JPanel to add to third MAIN panel

JPanel jpPanel3a = new JPanel();
jpPanel3a.setLayout(new FlowLayout());
jpPanel3a.setBackground(Color.DARK_GRAY);

//create border object to create border on panel 3a
//set it into panel 3a
Border panel3aBorder = new BevelBorder(BevelBorder.RAISED);
jpPanel3a.setBorder(panel3aBorder);

//create JLabel for panel 3a and set text
//color and literal

JLabel jlcarLotSz = new JLabel();
jlcarLotSz.setForeground(Color.RED);

//maximum vehicles allow will be user input defined
//carLotSize

jlcarLotSz.setText("Current # of vehicles in list: " + carLotSize);

//add JLabel to panel 3a

jpPanel3a.add(jlcarLotSz);

//add the panel to the NORTH sector of JPanel 3

jpPanel3.add(jpPanel3a, BorderLayout.NORTH);


//JPANEL 3 BRAVO

//create second nested sub-JPanel to add into third MAIN panel
//set layout

JPanel jpPanel3b = new JPanel();
jpPanel3b.setLayout(new FlowLayout(10));
jpPanel3b.setBackground(colorKhaki);

//create titled border around this panel
//and set

Border panel3bBorder = new TitledBorder("Vehicle List Actions");
jpPanel3b.setBorder(panel3bBorder);

//create buttons for JPanel 3a
//add, remove, sell

JButton jbtAdd = new JButton("Add");
JButton jbtRemove = new JButton("Remove");
JButton jbtSell = new JButton("Sell");


//set colors for panel 3b JButtons

jbtAdd.setBackground(colorTan);
jbtRemove.setBackground(colorTan);
jbtSell.setBackground(Color.PINK);

//create tool tip texts to inform user
//further of button actions

jbtAdd.setToolTipText("Adds a vehicle to the list");
jbtRemove.setToolTipText("Removes a vehicle from the list");
jbtSell.setToolTipText("Sells selected vehicle");

//add the buttons to the Panel

jpPanel3b.add(jbtAdd);
jpPanel3b.add(jbtRemove);
jpPanel3b.add(jbtSell);

//add second nested sub-JPanel to third MAIN panel

jpPanel3.add(jpPanel3b, BorderLayout.CENTER);

//add the third MAIN JPanel to the Frame with
//the nested panels already added

getContentPane().add(jpPanel3,BorderLayout.EAST);

//create fourth main JPanel that will go into the frame
//set layout

JPanel jpPanel4 = new JPanel();
jpPanel4.setLayout(new FlowLayout());

//set border and background color

Border border4 = new LineBorder(Color.RED, 2);
jpPanel4.setBorder(border4);
jpPanel4.setBackground(colorKhaki);

//create EXIT JButton for panel 4

JButton jbtExit = new JButton("Exit system!");

//set background red to alert user

jbtExit.setBackground(Color.RED);

//set tool tip text System Exit button

jbtExit.setToolTipText("Exits system - INFORMATION MAY BE LOST!");

//add button to panel

jpPanel4.add(jbtExit);

//add panel 4 to the south portion of the frame

getContentPane().add(jpPanel4,BorderLayout.SOUTH);









int vectorSize = vehicles.size();



















}

/*protected void paint(Graphics g)
{
super.paintComponent(g);
g.drawString("Welcome to Joe's Car Lot");


}

public Vehicle displayJList(Vehicle vehicle[])
{
for(int i = 0; i < vehicles.length; i++)
System.out.println(vehicles[i]);
}


public int getArraySize()
{
return carLotSize;
}


public int getCarLotSize()
{
return carLotSize;

}

*/



public void actionPerformed(ActionEvent e)
{
/*if(e.getActionCommand().equals("OK"))
System.out.println("OK");
else
System.err.println("Error");
*/
}
public void itemStateChanged(ItemEvent e)
{
/*if(e.getActionCommand().equals("OK"))
System.out.println("OK");
else
System.err.println("Error");
*/
}
/*
public String toString()
{
System.out.println();
return vehicles;
}*/

}//end of class
16 years ago
I am developing a simple GUI

My derived class from from uses a BorderLayout. In the east, I create a panel to put in there. However, before hand, I create two panels to put into that Panel.

So the "east" panel is created, two more panels are created, and buttons are added to each of them, and they are added to said east panel. Then the east panel is added to the content pane.

However, my buttons get trunacted. Any chnages I do seem to change the CENTER panel as well which contains a JList.

Here's some of my code in question. (in testing phase and there is limited commenting and prog convention)

//create third JPanel

JPanel jpPanel3 = new JPanel();
jpPanel3.setLayout(new BorderLayout());
jpPanel3.setBackground(colorBrown);


//create JPanel to nest in third panel

JPanel jpPanel3a = new JPanel();
//jpPanel3.setLayout(new GridLayout());

//create titled border around this panel

Border panel3aBorder = new TitledBorder("Vehicle Actions");

jpPanel3a.setBorder(panel3aBorder);

//create buttons for JPanel 3a

JButton jbtAdd = new JButton("Add");
JButton jbtRemove = new JButton("Remove");
JButton jbtSell = new JButton("Sell");

//add the buttons to the Panel

jpPanel3a.add(jbtAdd);
jpPanel3a.add(jbtRemove);
jpPanel3a.add(jbtSell);

//add the panel to the NORTH sector of JPanel 3

jpPanel3.add(jpPanel3a, BorderLayout.CENTER);

//create seconf JPanel to nest in third panel

JPanel jpPanel3b = new JPanel();
jpPanel3.setLayout(new FlowLayout());

Border panel3bBorder = new TitledBorder("Car Lot Size");


jpPanel3b.setBorder(panel3bBorder);

JLabel jlcarLotSz = new JLabel();
jlcarLotSz.setText("Maximum # vehicles allowed: " + carLotSize);
jpPanel3b.add(jlcarLotSz);

jpPanel3.add(jpPanel3b, BorderLayout.SOUTH);

//add the third JPanel to the Frame with
//the nested panels already added

getContentPane().add(jpPanel3,BorderLayout.EAST);

Any advice, input, and/or anecdotes would be greatly appreciated. Thanks!
16 years ago
amod gole brought up some really good points. When I started learning about inheritnace, dynamic binding, polymorphism, it took me a while to understand the ramifications of it all.

But with single inheritance, it is only driect inheritance, but you are inheriting a "single line" if you will from multiple classes. Just like Class Object. We might create a parent but the child inherits from diectly from the parent but also from Object, which is the parent to our parent class.

Interfaces allow direct inheritance from multiple classes. Once you start using the Component class and using such things as JFrame, JPanel, etc, and notice their immensity and "tree of inheritance," and get into event driven programming you'll see how awesome inheritance is and the imortance of interfaces.
16 years ago