Hi folks...I have a Class called Model which I am using to iterate through a string, tokenize it, and then access a method in my GUI class which is supposed to change the color of the borderLayout panel. I am getting this error. I have tried to make me Model class extend my GUI class, this didn't work. I made the setNorthBorder Color method a static method, but this caused a problem with accessing the layoutBorderLayoutSection method. The program runs from a simple driver Class...
any ideas?

thanks.

Driver class....

/**
*Starts my GUI
*/
public class Driver{
public static void main(String[] args){
GUI g = new GUI("Mark's GUI", new Model());
}
}

GUI Class......

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color.*;

public class GUI extends JFrame implements ActionListener

{
JButton parseButton;
JButton clearButton;
JTextField tx;
static JLabel lNorth;
static JLabel lSouth;
static JLabel lEast;
static JLabel lWest;
static JLabel lCenter;
Model m;
Color c;
private static JPanel lowerPanel;


public GUI(String sTitleBar, Model m)
{
super(sTitleBar);
this.m = m;
//make program stop when user closes window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//get content pane
Container contentPane = getContentPane();
JPanel mainPanel = new JPanel();
//set up main contentPane
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
//add the panel to the contentPane
contentPane.add(mainPanel);
//create upper panel
JPanel upperPanel = new JPanel();
//create a layout for the upper section and associate it with the upper section
upperPanel.setLayout(new FlowLayout());
mainPanel.add(upperPanel);
//assign input text to variable j1
JLabel j1 = new JLabel("Your message here:    ");
//add j1 to upperPanel
upperPanel.add(j1);
//add a text input window to upper panel
tx = new JTextField(20);
//add text box to upperPanel
upperPanel.add(tx);
//assign parseButton
parseButton = new JButton("Parse");
//add parseButton to upperPanel
upperPanel.add(parseButton);
//assign clearButton
clearButton = new JButton("Clear");
//add clearButton to upperPanel
upperPanel.add(clearButton);
//create lower panel
lowerPanel = new JPanel(new BorderLayout());
mainPanel.add(lowerPanel);
//create north panel
lNorth = layoutBorderLayoutSection(lowerPanel, "North", BorderLayout.NORTH);
lEast =layoutBorderLayoutSection(lowerPanel, "East", BorderLayout.EAST);
lWest = layoutBorderLayoutSection(lowerPanel, "West", BorderLayout.WEST);
lCenter=layoutBorderLayoutSection(lowerPanel, "Center", BorderLayout.CENTER);
lSouth =layoutBorderLayoutSection(lowerPanel, "South", BorderLayout.SOUTH);


clearButton.addActionListener(this);
parseButton.addActionListener(this);
this.pack();
setVisible(true);
}


//Add panels, layouts, and labels for BorderLayout section
private JLabel layoutBorderLayoutSection(JPanel ap, String s, String compasspt)
{
JPanel p = new JPanel(new FlowLayout());
ap.add(p, compasspt);
JLabel l = new JLabel(s, SwingConstants.CENTER);
p.add(l);
l.setBackground(Color.white);
l.setOpaque(true);
return l;
}



public void actionPerformed(ActionEvent event)
{
if (event.getSource() == clearButton)
{
lNorth.setForeground(Color.black);
lSouth.setForeground(Color.black);
lEast.setForeground(Color.black);
lWest.setForeground(Color.black);
lCenter.setForeground(Color.black);
}


else if(event.getSource() == parseButton)
m.parse(tx.getText());
}



public void setNorthBorderColor(Color c)
{
JPanel newNorth = new JPanel(new FlowLayout());
lowerPanel.add(newNorth);
JLabel j = layoutBorderLayoutSection(lowerPanel, "North", BorderLayout.NORTH);
newNorth.add(j);
j.setBackground(c);
j.setOpaque(true);



}



public static void setEastBorderColor(Color c)
{
lEast.setBackground(c);
}


public static void setWestBorderColor(Color c)
{
lWest.setBackground(c);
}



public static void setSouthBorderColor(Color c)
{
lSouth.setBackground(c);
}


public static void setCenterBorderColor(Color c)
{
lCenter.setBackground(c);
}



}

Model class.....

import java.awt.Color;


public class Model
{
// you will need to take a string for lab8
Color c;



public void parse(String s) {
// split string from text window into individual words
String[] sa = s.split(" ");
//for loop for sa text
for(int i = sa.length-1; i>=0; i--)
// {
//                      //iterate through sa text
//                      for(int j =0; j <sa.length; j++)
{
//convert each each character to lower case
String str = sa.toLowerCase();
//if an individual word starts wth n, e, s, w, or c
if(sa.startsWith("n") || sa.startsWith("e") || sa.startsWith("s") ||
sa.startsWith("w") || sa.startsWith("c"))
//send that word to seBackgroundColor method
createBackgroundColor(sa);


}
}
//}



/**
* method to return appropriate background color for panels
**/


public void createBackgroundColor(String word)
{
char f = word.charAt(0);
char s = word.charAt(word.length()-1);


switch(s){


//if j string ends with a b
case 'b':
//return background color of blue
c = Color.blue;
break;
//if j string ends with a c
case'c':
//return background color of cyan
c = Color.cyan;
break;
//if j string ends with a d
case'd':
//return background color darkGray
c = Color.darkGray;
break;
//if j string ends with a g
case 'g':
//return background color green
c = Color.green;
break;
//if j string ends with a l
case 'l':
//return background color lighGray
c = Color.lightGray;
break;
//if j string ends with a m
case 'm':
//return background color magenta
c = Color.magenta;
break;
//if j string ends with a o
case 'o':
//return background color orange
c = Color.orange;
break;
//if j string ends with a p
case 'p':
//return background color pink
c = Color.pink;
break;
//if j string ends with a r
case 'r':
//return background color red
c = Color.red;
break;
//if j string ends with a y
case 'y':
//return background color yellow
c = Color.yellow;
break;
default:      ;
createBorderColor(c, f);


}


}


public void createBorderColor(Color c, char f)
{
switch(f){


case 'n':
GUI.setNorthBorderColor(c);
break;
case 'e':
GUI.setEastBorderColor(c);
break;
case 's':
GUI.setSouthBorderColor(c);
break;
case 'w':
GUI.setWestBorderColor(c);
break;
case 'c':
GUI.setCenterBorderColor(c);
break;
default: ;
}


}
}

Edited 3 Years Ago by happygeek: fixed formatting

Your trying to reach non-static contents from a static method. You can't do that. Either make your method public, or you'll need to create an instance of what your trying to get. I would just declare it public.

Your trying to reach non-static contents from a static method. You can't do that. Either make your method public, or you'll need to create an instance of what your trying to get. I would just declare it public.

All of my methods are public aren't they?

What about these?

public static void setEastBorderColor(Color c)
{
lEast.setBackground(c);
}

public static void setWestBorderColor(Color c)
{
lWest.setBackground(c);
}


public static void setSouthBorderColor(Color c)
{
lSouth.setBackground(c);
}

public static void setCenterBorderColor(Color c)
{
lCenter.setBackground(c);
}
This article has been dead for over six months. Start a new discussion instead.