pgmtkl
asked on
arrays
I am writing a program to prompt a user to select how they would like to sort from any of 3 arrays and then display the choices in a text field. i am getting a compile error of ''non-static variable service cannot be referenced from a static context''. I looked on the java.sun website and have set up the sort as the instructions say to, at least from what i see. I have posted the code below. What is wrong with the program?
1st program- gui-
import javax.swing.*;
import java.awt.*;
public class HairSalon extends JFrame {
HairSalonSupport HairSalonSupport=new HairSalonSupport(this);
//sets up rows for gui
JPanel row10 = new JPanel();
JLabel amntLabel = new JLabel("We Cut Salon", JLabel.LEFT);
//public JTextField serviceField = new JTextField(10);
JPanel row20 =new JPanel();
JComboBox options = new JComboBox();
JLabel optionsLabel = new JLabel();
JLabel lbltype= new JLabel("Salon Services",JLabel.LEFT);
JTextField years = new JTextField(2);
JPanel row30 = new JPanel();
JLabel sortType= new JLabel("Sort by one of the following:",JLabel.LEFT);
public JButton serviceButton = new JButton("Service");
public JButton priceButton = new JButton("Price");
public JButton timeButton = new JButton("Time");
JPanel row40 = new JPanel();
JLabel paymtLabel = new JLabel("Service Price");
public JTextField paymtField = new JTextField();
JPanel row50 = new JPanel();
JLabel lblservice= new JLabel("Service"+ "\t",JLabel.LEFT);
JLabel lblPrice= new JLabel("Price"+ "\t",JLabel.LEFT);
JLabel lblTime= new JLabel("Time"+ "\t",JLabel.LEFT);
//JLabel lblBalance= new JLabel("Balance"+ "\t",JLabel.LEFT);
JPanel row60 = new JPanel();
JTextArea textField = new JTextArea(8, 40);
JPanel row70=new JPanel();
public JButton calButton = new JButton("Calculate");
public JButton resetButton = new JButton("Reset");
public JButton endButton = new JButton("End");
public HairSalon() {
super("We Cut Salon");
setSize(650,500);
setLocation(250,60);
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
Font f = new Font ("Arial", Font.PLAIN, 30);
Font b = new Font ("Arial", Font.BOLD, 16);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane();
pane.setLayout(layout);
//serviceField.addActionLi stener(Hai rSalonSupp ort);
options.addItemListener(Ha irSalonSup port);
calButton.addActionListene r(HairSalo nSupport);
resetButton.addActionListe ner(HairSa lonSupport );
endButton.addActionListene r(HairSalo nSupport);
FlowLayout flow10 = new FlowLayout();
row10.setLayout(flow10);
row10.add(amntLabel);
//row10.add(serviceField);
pane.add(row10);
GridLayout flow20 = new GridLayout(1,4,5,1);
row20.setLayout(flow20);
row20.add(optionsLabel);
row20.add(options);
row20.add(lbltype);
options.setEnabled(true);
options.addItem("Select Option");
options.addItem("Cut at $8.00");
options.addItem("Shampoo at $4.00");
options.addItem("Manicure at $18.00");
options.addItem("Style at $48.00");
options.addItem("Permanent at $18.00");
options.addItem("Trim at $6.00");
pane.add(row20);
GridLayout flow30 = new GridLayout (1,4,75,1);
row30.add(sortType);
row30.add(serviceButton);
row30.add(priceButton);
row30.add(timeButton);
pane.add(row30);
FlowLayout flow40 = new FlowLayout();
row40.setLayout(flow40);
row40.add(paymtLabel);
row40.add(paymtField);
pane.add(row40);
GridLayout flow50= new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(lblservice);
row50.add(lblPrice);
row50.add(lblTime);
//row50.add(lblBalance);
pane.add(row50);
FlowLayout flow60 = new FlowLayout();
textField.setLineWrap(true );
row60.setLayout(flow60);
JScrollPane textpane= new JScrollPane(textField,
JScrollPane.VERTICAL_SCROL LBAR_ALWAY S,
JScrollPane.HORIZONTAL_SCR OLLBAR_NEV ER);
row60.add(textField);
pane.add(row60);
GridLayout flow70 = new GridLayout (1,4,75,1);
row70.add(calButton);
row70.add(resetButton);
row70.add(endButton);
pane.add(row70);
}
public static void main(String[] args) {
HairSalon guiFrame= new HairSalon();
guiFrame.setVisible(true);
}
}
__________________________ __________ __________ __________ ______
__________________________ __________ __________ __________ ______
2nd program:
import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class HairSalonSupport implements ItemListener,ActionListene r
{
HairSalon gui;
double price = 0;
int minutes=0;
double term = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double payment = 0;
String service[] = {"cut","shampoo","manicure ","style", "permanent ","trim"};
//double price[] = { 8.00,4.00,18.00,48.00,18.0 0,6.00};
double price[] = {8,4,18,48,18,6};
double time[]={15,10,30,55,35,5};
int array;
int c = 1;
int index = 1;
//to format payment prices
DecimalFormat df = new DecimalFormat("$#,###.00") ;
public HairSalonSupport(HairSalon in)
{
gui=in;
}
public static void main(String [] args)
{
Arrays.sort(service);
//Print the array
for (int i=0; i<service.length; i++){
System.out.println(service [i]); }
Arrays.sort(price);
//Print the array
for (int i=0; i<price.length; i++){
System.out.println(price[i ]); }
Arrays.sort(time);
//Print the array
for (int i=0; i<price.length; i++){
System.out.println(time[i] ); }
}
public void itemStateChanged(ItemEvent arg0) {
Object button = gui.options.getSelectedIte m();
String buttonPress = button.toString();
Object source = arg0.getSource();
System.out.println(" == > item = " + buttonPress );
if (buttonPress == gui.serviceButton.getActio nCommand() ) {
array =1;
gui.textField.append("");
}
if (buttonPress == gui.priceButton.getActionC ommand()) {
array = 2;
gui.textField.append("");
}
if (buttonPress == gui.timeButton.getActionCo mmand()) {
array = 3;
gui.textField.append("");
}
}
public void actionPerformed(ActionEven t event)
{
String buttonClicked = event.getActionCommand();
System.out.println(" == > action = " + buttonClicked );
if (buttonClicked == "Reset") {
setNull();
}
if (buttonClicked == "End"){
clearResults();
System.exit(0);
}
if (buttonClicked =="Calculate"){
clearResults();
startCalculations();
}
}
void reset() {
gui.paymtField.setText(nul l);
gui.paymtField.setText(nul l);
gui.textField.setText(null );
}
void end() {
System.exit(0);
}
/*
* Sets GUI & Declarations to Null
*/
void setNull()
{
gui.textField.setText (null); //Sets value to null
gui.options.setSelectedIte m("Select Option"); //Sets value to "Select Option"
gui.paymtField.setText(nul l); //Sets value to null
index = 1; //Sets Loop-Counter to 0
}
/*
* Sets GUI & Declarations to Null
*/
void clearResults() //Sets GUI & Declarations to null
{
gui.textField.setText(null ); //Sets value to null
}
void setComboBoxNull()
{
gui.options.setEnabled(fal se); //Disables Drop Down Menu
}
}
1st program- gui-
import javax.swing.*;
import java.awt.*;
public class HairSalon extends JFrame {
HairSalonSupport HairSalonSupport=new HairSalonSupport(this);
//sets up rows for gui
JPanel row10 = new JPanel();
JLabel amntLabel = new JLabel("We Cut Salon", JLabel.LEFT);
//public JTextField serviceField = new JTextField(10);
JPanel row20 =new JPanel();
JComboBox options = new JComboBox();
JLabel optionsLabel = new JLabel();
JLabel lbltype= new JLabel("Salon Services",JLabel.LEFT);
JTextField years = new JTextField(2);
JPanel row30 = new JPanel();
JLabel sortType= new JLabel("Sort by one of the following:",JLabel.LEFT);
public JButton serviceButton = new JButton("Service");
public JButton priceButton = new JButton("Price");
public JButton timeButton = new JButton("Time");
JPanel row40 = new JPanel();
JLabel paymtLabel = new JLabel("Service Price");
public JTextField paymtField = new JTextField();
JPanel row50 = new JPanel();
JLabel lblservice= new JLabel("Service"+ "\t",JLabel.LEFT);
JLabel lblPrice= new JLabel("Price"+ "\t",JLabel.LEFT);
JLabel lblTime= new JLabel("Time"+ "\t",JLabel.LEFT);
//JLabel lblBalance= new JLabel("Balance"+ "\t",JLabel.LEFT);
JPanel row60 = new JPanel();
JTextArea textField = new JTextArea(8, 40);
JPanel row70=new JPanel();
public JButton calButton = new JButton("Calculate");
public JButton resetButton = new JButton("Reset");
public JButton endButton = new JButton("End");
public HairSalon() {
super("We Cut Salon");
setSize(650,500);
setLocation(250,60);
setDefaultCloseOperation(J
Font f = new Font ("Arial", Font.PLAIN, 30);
Font b = new Font ("Arial", Font.BOLD, 16);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane();
pane.setLayout(layout);
//serviceField.addActionLi
options.addItemListener(Ha
calButton.addActionListene
resetButton.addActionListe
endButton.addActionListene
FlowLayout flow10 = new FlowLayout();
row10.setLayout(flow10);
row10.add(amntLabel);
//row10.add(serviceField);
pane.add(row10);
GridLayout flow20 = new GridLayout(1,4,5,1);
row20.setLayout(flow20);
row20.add(optionsLabel);
row20.add(options);
row20.add(lbltype);
options.setEnabled(true);
options.addItem("Select Option");
options.addItem("Cut at $8.00");
options.addItem("Shampoo at $4.00");
options.addItem("Manicure at $18.00");
options.addItem("Style at $48.00");
options.addItem("Permanent
options.addItem("Trim at $6.00");
pane.add(row20);
GridLayout flow30 = new GridLayout (1,4,75,1);
row30.add(sortType);
row30.add(serviceButton);
row30.add(priceButton);
row30.add(timeButton);
pane.add(row30);
FlowLayout flow40 = new FlowLayout();
row40.setLayout(flow40);
row40.add(paymtLabel);
row40.add(paymtField);
pane.add(row40);
GridLayout flow50= new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(lblservice);
row50.add(lblPrice);
row50.add(lblTime);
//row50.add(lblBalance);
pane.add(row50);
FlowLayout flow60 = new FlowLayout();
textField.setLineWrap(true
row60.setLayout(flow60);
JScrollPane textpane= new JScrollPane(textField,
JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
row60.add(textField);
pane.add(row60);
GridLayout flow70 = new GridLayout (1,4,75,1);
row70.add(calButton);
row70.add(resetButton);
row70.add(endButton);
pane.add(row70);
}
public static void main(String[] args) {
HairSalon guiFrame= new HairSalon();
guiFrame.setVisible(true);
}
}
__________________________
__________________________
2nd program:
import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class HairSalonSupport implements ItemListener,ActionListene
{
HairSalon gui;
double price = 0;
int minutes=0;
double term = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double payment = 0;
String service[] = {"cut","shampoo","manicure
//double price[] = { 8.00,4.00,18.00,48.00,18.0
double price[] = {8,4,18,48,18,6};
double time[]={15,10,30,55,35,5};
int array;
int c = 1;
int index = 1;
//to format payment prices
DecimalFormat df = new DecimalFormat("$#,###.00")
public HairSalonSupport(HairSalon
{
gui=in;
}
public static void main(String [] args)
{
Arrays.sort(service);
//Print the array
for (int i=0; i<service.length; i++){
System.out.println(service
Arrays.sort(price);
//Print the array
for (int i=0; i<price.length; i++){
System.out.println(price[i
Arrays.sort(time);
//Print the array
for (int i=0; i<price.length; i++){
System.out.println(time[i]
}
public void itemStateChanged(ItemEvent
Object button = gui.options.getSelectedIte
String buttonPress = button.toString();
Object source = arg0.getSource();
System.out.println(" == > item = " + buttonPress );
if (buttonPress == gui.serviceButton.getActio
array =1;
gui.textField.append("");
}
if (buttonPress == gui.priceButton.getActionC
array = 2;
gui.textField.append("");
}
if (buttonPress == gui.timeButton.getActionCo
array = 3;
gui.textField.append("");
}
}
public void actionPerformed(ActionEven
{
String buttonClicked = event.getActionCommand();
System.out.println(" == > action = " + buttonClicked );
if (buttonClicked == "Reset") {
setNull();
}
if (buttonClicked == "End"){
clearResults();
System.exit(0);
}
if (buttonClicked =="Calculate"){
clearResults();
startCalculations();
}
}
void reset() {
gui.paymtField.setText(nul
gui.paymtField.setText(nul
gui.textField.setText(null
}
void end() {
System.exit(0);
}
/*
* Sets GUI & Declarations to Null
*/
void setNull()
{
gui.textField.setText (null); //Sets value to null
gui.options.setSelectedIte
gui.paymtField.setText(nul
index = 1; //Sets Loop-Counter to 0
}
/*
* Sets GUI & Declarations to Null
*/
void clearResults() //Sets GUI & Declarations to null
{
gui.textField.setText(null
}
void setComboBoxNull()
{
gui.options.setEnabled(fal
}
}
As CEHJ said, You can't have both classes dependant on each other. The situation You have now is something like that:
class Num1 {
Num2 n = new Num2();
}
class Num2 {
Num1 n = new Num1();
}
You can try, but it's obviuos that You won't be able to compile those two, because neither of them can be instantiated without the other.
So, You must choose which class You want to be independant, then initialize it and use it as an argument in the other one. Or simply, rethink Your design.
class Num1 {
Num2 n = new Num2();
}
class Num2 {
Num1 n = new Num1();
}
You can try, but it's obviuos that You won't be able to compile those two, because neither of them can be instantiated without the other.
So, You must choose which class You want to be independant, then initialize it and use it as an argument in the other one. Or simply, rethink Your design.
ASKER
So can i move that sort to the item state changed section?
>>So can i move that sort to the item state changed section?
Can't really answer that as we don't know what you want to achieve. If the starting order is not important, why don't you just declare them in a sorted way?
Can't really answer that as we don't know what you want to achieve. If the starting order is not important, why don't you just declare them in a sorted way?
ASKER
depending on what the user selects it is supposed to sort. if they pick the sort by price it will sort the array by price. i dont think you can do that by declaring them sorted. or can you?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
http://mindprod.com/jgloss/static.html