aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes need help with JPanel, liseners, and referencing vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "need help with JPanel, liseners, and referencing vectors" Watch "need help with JPanel, liseners, and referencing vectors" New topic
Author

need help with JPanel, liseners, and referencing vectors

kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
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
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I'm not sure what your question is. Could you perhaps elaborate on the exact point where you're having troubles?

Anyway, I cannot seem to use vector methods anywhere.


What exactly does that mean? You use addElement() so I don't see how you're not able 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.


That's because they are declared out of scope. You can't declare them in the constructor, then use the reference in actionPerformed() because this is outside the scope of actionPerformed(). You must move the declaration (not to be mistaken with it's instantiation) to the class for actionPerformed() to "see" it.

But the worst part, again, is the vector processing which is preventing me from referencing the vector.


What exactly does that mean?

It even crossed my mind to deep copy or clone the vector or array, and reference it that way.


You realize that a Vector is not an array and that they can not be used interchangeably right? A brief skim over your code makes it look like you're trying to use them somewhat interchangeably. Furthermore, when it comes to using Collections you should prefer an ArrayList over a Vector. The only reason Vector isn't deprecated is backwards compatibility.

What you probably want to do is declare a List as a member of the "JCL" class. It would accept a List as a parameter in it's constructor and alias it's member to that parameter. That List will then be available throughout your JCL class.

Last, but certainly not least, please format your code to something resembling Java conventions and use code tags so it will keep the formatting. i.e. <code> *insert formatted code here* </code> with the <> replaced with [].
[ December 19, 2005: Message edited by: Ken Blair ]
kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
Thanks Ken.
kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
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.

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.


I would suggest looking into an IDE like Eclipse that can automatically format code with a single keystroke. If you already have an IDE I would be surprised if it doesn't have something similar. That will make things easier on you and make it easier to share with others.

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.


Have you considered creating a class that's responsibile for maintaining a list of vehicles? For example, a simple VehicleManager:



Now since I wanted to consolidate as much as I could into one class


For the purposes of this discussion I'll accept that premise. I want to point out, however, that this is not very object oriented and sounds like the God object anti-pattern.

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.


Be careful about creating cyclical references. Your JComponents now have a listener registered that likewise has references to those JComponents. I believe that unless you specifically unregister those listeners or null out those components they will never get garbage collected.

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.


First, you implemented the methods. Overriding happens when a subclass overrides methods that exist in a superclass. You are implementing interfaces rather than extending a class.

Second, they are methods of the class. The constructor can "see" them. The methods, however, can not see the constructor's local variables.



i is a member of class X. Every method and inner class can "see" it. z is a local variable declared in a constructor, it can't be "seen" outside of the constructor. y is a local variable declared in method m, it can't be "seen" outside of the method m.

You declare a local variable in your constructor. That variable is can only be used within that constructor. Once the constructor finishes executing it will be gone forever.

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 still don't understand what you mean by you "can't" use them. What error does it give you and where?

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?


In this particular case it doesn't look like it matters where you create it. What's important is that you get a reference to it where you need it. One way of doing that is passing a reference to your JCL in the constructor. That's fine, but you then need to alias that reference to a member in order for other methods in JCL to make use of it.

kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with JPanel, liseners, and referencing vectors