Vanavah Edwards
asked on
Resolving errors with comboboxes instance variables
Previously, I had double declared combobox object variables. I then declared them as private variables cb1,cb2 and removed the double declaration. This worked well with the sample program. However, when I do this to my class this created a few errors of the same vaiables that I cannot resolve including the private declaration.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
public class HelloWorld {
private JCombobox cb1, cb2;
public static void main(String[] args) {
// START ComboBox Image Setup
ImageIcon[] images;
String[] comboStrings = {"create new po", "browse and select"};
images = new ImageIcon[comboStrings.length]; //array size
Integer[] intArray = new Integer[comboStrings.length];
ComboBoxRenderer renderer= new ComboBoxRenderer();
for (int i = 0; i < comboStrings.length; i++) {
intArray[i] = new Integer(i);
images[i] = renderer.createImageIcon("images/" + comboStrings[i] + ".jpg"); // creates image; send path
Image img = images[i].getImage(); // scales image size
Image newimg = img.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
images[i] = new ImageIcon(newimg);
if (images[i] != null) { // sets image description
images[i].setDescription(comboStrings[i]);
}
}
// END ComboBox Image Setup
cb1 = new JComboBox(images);
cb1.setRenderer(renderer); // displays image description
cb1.setMaximumRowCount(2);
String[] xxx = {"1111111111", "222"};
cb2 = new JComboBox(xxx);
ClickListener cl = new ClickListener();
cb1.addActionListener(cl);
cb2.addActionListener(cl);
JFrame f = new JFrame("teerer");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(cb1);
p.add(cb2);
f.add(p);
f.setSize(300, 300);
f.setVisible(true);
}
private class ClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// System.out.println(e.getSource().equals(cb1));
if (e.getSource().equals(cb1)) {
System.out.println("You've seleccted - cb1");
}
if (e.getSource().equals(cb2)) {
System.out.println("You've seleccted - cb2");
}
if(cb1.getSelectedItem().equals("1111")) {
System.out.println("or right "+cb1.getSelectedItem());
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should make them static in your case - in general that is not good way to do thinsg
private static JComboBox cb1, cb2;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In general it is definitely bad practice to do all that stuff in method main()
Waht you should do - you should make a class which extends JFrame, then instatiate, bulild and place all your elements in the
constructor of this class.
Then usually the only styatement you will have in main() would be
new MyClassExtendingJFrame();
That would be much more reasonabnle way
and woill not cause any issues with static variables in main()
constructor of this class.
Then usually the only styatement you will have in main() would be
new MyClassExtendingJFrame();
That would be much more reasonabnle way
and woill not cause any issues with static variables in main()
Each static varaibale is unique for the whole class
Instance variable exists within every instance - and its value in one instance may be different
than in another isntance.
static method is static because it does not ddpend on any istance - therefore it is claaed with reference
only to class as a whole not to any instance - MyClass.myStaticMethod()
as ooppose to instance method which is invoked on specific instance:
MyClass instanceOfMyClass = new MyClass();
instanceOfMyClass.myInstan ceMethod() ;
therefore static method cannot depden on the instance variables - as it should give
teh saem result for given class and is not specific to any instance
therefore you cannot use instance varaibnles within the body of your static method.
method main() is static ----> youcannot use instance variables within the main() method
That was the reason for your compiler errors
Instance variable exists within every instance - and its value in one instance may be different
than in another isntance.
static method is static because it does not ddpend on any istance - therefore it is claaed with reference
only to class as a whole not to any instance - MyClass.myStaticMethod()
as ooppose to instance method which is invoked on specific instance:
MyClass instanceOfMyClass = new MyClass();
instanceOfMyClass.myInstan
therefore static method cannot depden on the instance variables - as it should give
teh saem result for given class and is not specific to any instance
therefore you cannot use instance varaibnles within the body of your static method.
method main() is static ----> youcannot use instance variables within the main() method
That was the reason for your compiler errors
ASKER
You suggest is correct. I tried them and all the errors disappear as you said but now it come up with a fatal error "java.lang.NoSuchMethodErr or: main
Exception in thread "main" "
I even removed the image part to make it easier but still the same problem
Exception in thread "main" "
I even removed the image part to make it easier but still the same problem
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
public class HelloWorld {
private static JComboBox cb1, cb2;
public void main(String[] args) {
String[] aaa = {"aaa", "bbb", "ccc"};
cb1 = new JComboBox(aaa);
// cb1.setRenderer(renderer); // displays image description
// cb1.setMaximumRowCount(2);
String[] xxx = {"1111111111", "222"};
cb2 = new JComboBox(xxx);
ClickListener cl = new ClickListener();
cb1.addActionListener(cl);
cb2.addActionListener(cl);
JFrame f = new JFrame("teerer");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(cb1);
p.add(cb2);
f.add(p);
f.setSize(300, 300);
f.setVisible(true);
}
private class ClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// System.out.println(e.getSource().equals(cb1));
if (e.getSource().equals(cb1)) {
System.out.println("You've seleccted - cb1");
}
if (e.getSource().equals(cb2)) {
System.out.println("You've seleccted - cb2");
}
if(cb1.getSelectedItem().equals("1111")) {
System.out.println("or right "+cb1.getSelectedItem());
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I accidentally changed that but chagned it back. However, after I did that the attached line of code comes up with this error "No enclosing instance of type HelloWorld is accessible. Must qualify the allocation with an enclosing instance of type HelloWorld (e.g. x.new A() where x is an instance of HelloWorld)."
ClickListener cl = new ClickListener();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
so make your inner class static
Once again, as I told you all this is incorrect approach.
You shoudl craete all your elements and listemers etc in constructor of the
class which extends JFrame
Method main() should usually conatins only few lines and careate instance of your class
new FrameClassname();
Then you would not have all these problems and that would be much more logiacal
Once again, as I told you all this is incorrect approach.
You shoudl craete all your elements and listemers etc in constructor of the
class which extends JFrame
Method main() should usually conatins only few lines and careate instance of your class
new FrameClassname();
Then you would not have all these problems and that would be much more logiacal
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is very basic template which should work for you for 90% simple applications:
Use it and you'll never have these kind of problems with static main, etc.
public class MyFrame extends JFrame implements ActionListener {
private JComboBox cb1;
private JTextField tf;
public MyFrame(...) {
super("My Frame");
tf = new JTextField(10);
cb1 = JComboBox(..);
cb1.addActionListener(this);
JLabel lbl = new JLabel("tratatat");
....
//set layouts, carete panels, populate panels, add papnels to top window, specify size of window, make it all visible
this.setVisible(true);
} // end of constructor
public void myMtehod(...) {
// some methods
}
public void actionPerformed (ActionEvent ae) {
if(ae.getSource().equals(cb1)){
// handle evbent
}
public static void main(String [] args){
//perhaps read some paerameters form args or from user input
new MyFrame(soe parameters here...);
}
}
ASKER
I was checking it. It works perfect. I have to check and see if I receive more messages from you. Thanks for your patients and talent. I will check my messages and award you and post more problems now.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
prompt, fast, understands problems well, can read java code very, very quickly, gives suggestions, gives code examples, direct users to the site with documents of solutions, patient, offers multiple solutions
Or post ComboBoxRenderer - otherwise I cannot compile it