Java - Need help

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Say I have this line of code and I want to use an int value that has been passed to the constructor for x, how can I do it?


When I try that it doesn't work because it tries to pass that value to jPanel, but all I want it to do is set it to a certain colour.
 
Last edited:
Make sure that you setOpaque(true) on the component in question if you want it's background to actually show.

I'm not sure about this class.jPanel(this.a) you are on about though . . . Why not just hold a reference (as an instance variable) to the JPanel that needs it's colour updated every so often on your View/GUI class so you can just call on its setBackground method?

I'm not sure your explanation is really clear as to what you are trying to do, are you trying to set the colour of a JPanel once, just after you construct it, or at another time, i.e. after a user clicks something?
 
Aaaahhh, ok.

You prolly want to have your GUI class set up like this. And you most certainly dont want your JPanel segments to be public so that another class can use them (encapsulation rule "one class may not play with another class's private parts" ;P).

Basically if you make a lot of JPanels, and you somehow want to iterate them based on index, you need to hold them in an array, vector, list, collection, whatever.

Code:
public class GUI {

	private JPanel[] segments = new JPanel[7];
	
	public GUI() {
		// Construct
		for ( int i = 0; i < segments.length; i++ ) {
			segments[i] = new JPanel();
			segments[i].setOpaque(true);
			segments[i].setBackground(new Colour(0, 0, 0));
			
			this.add(segments[i]); // Or wherever you wanna put the panel
		}
	}

	public void setSegmentOn(int segmentNum, boolean b) {
		if ( b )
			segments[segmentNum].setBackground(new Color(255, 51, 51);
		else
			segments[segmentNum].setBackground(new Color(0, 0, 0);
	}
	
}

For your setup code in your switch statement to turn on the lights based on the actual integer they need to display you'd then do something like:

Code:
case 0:  	 
	GUI.setSegmentOn(0, true);            
	GUI.setSegmentOn(1, true);
	GUI.setSegmentOn(2, true);
	GUI.setSegmentOn(3, false);
	GUI.setSegmentOn(4, true);
	GUI.setSegmentOn(5, true);
	GUI.setSegmentOn(6, true);
	break;

Is that clear? The main thing you want to learn here is that if you are finding your self needing many of one type of object, you shouldn't reference them all as JPanel1, JPanel2, JPanel3 as instance variables or whatever, you should put them in an array and then reference by array index. And the second thing you want to learn is that even your GUI classes shouldn't expose their private elements, instead make methods to perform specific operations on them, such as turning segments on or off.
 
Last edited:
For dynamic method invocation (determining which method to call at runtime) you need to make use of the Java Reflection API. However the problem you are trying to solve has other solutions that do not involve the complexities of reflection. Pulseammo has the right idea, working with arrays that hold references to the real objects rather than locating them at runtime usually works much better and makes the code much easier to understand.
 
Cheers. Btw how do you stop the gui builder creating a space between jPanels? Everytime I try to move them close together they snap apart.
 
Cheers. Btw how do you stop the gui builder creating a space between jPanels? Everytime I try to move them close together they snap apart.

Right-Click the Component and select Space Around Component... then change the border releating to the edge with the gap to 0. So for example if I had JPanel1 and then placed JPanel2 to the right of it I could alter the left border of JPanel2 to zero to get rid of the gap.
 
Thanks, is there also a way to totally remove all the automatic spacing and resizing crap on everything. It's doing my head in automatically moving things and resizing them all over the place!
 
Last edited:
I know what you mean, I'm often fighting the GUI builder, normally when I have my design perfect but I need to add an extra field and it completely screws up the layout and everything keeps snapping back to different locations.

There is a basic tutorial here which shows some horizontal resize feature which I haven't used before, might stop the snapping. http://form.netbeans.org/nonav/demos/sample_GUI1.html
 
Back
Top Bottom