Labels not displaying properly...help!?

B

B J H

I'm using the following code to try and change the contents of some labels
when certain buttons are clicked...but the display is looking mangled like
this...

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box.jpg



/*
* File: ReactorInterface.java
* Description: A class which provides a GUI for the Reactor class.
* <Uses: Reactor class>
*/
import java.awt.*;
import java.awt.event.*;
public class ReactorInterface extends Panel implements ActionListener
{
private Button increaseButton = new Button("Increase Temperature");
private Button decreaseButton = new Button("Decrease Temperature");
private Label tempLabel = new Label();
private Label alarmLabel = new Label();
private Reactor reactor1 = new Reactor();
private String currentTemp;
private String templabel;

public ReactorInterface()

{
add(increaseButton);
add(decreaseButton);
add(tempLabel);
add(alarmLabel);
increaseButton.addActionListener(this);
decreaseButton.addActionListener(this);
}

/*public void paint(Graphics g)
{
//currentTemp = reactor1.getTemperature();
//templabel = "The current temperature of the reactor is: "
//tempLabel.setText("templabel");


//more goes here
}
*/



public void actionPerformed(ActionEvent e)
{

if( e.getSource() == increaseButton )
{
if ( reactor1.increaseTemp() == true )
{
alarmLabel.setText("** MAX TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}

tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}

if (e.getSource() == decreaseButton)
{
if (reactor1.decreaseTemp() == true)
{
alarmLabel.setText("** MIN TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}
tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}
}











}


*end code snip*

Any ideas?
 
A

Andrew Thompson

B J H said:
I'm using the following code to try and change the contents of some labels
when certain buttons are clicked...but the display is looking mangled like
this...

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box.jpg

I get a 'not found' on that URL
*end code snip*

If I understand you correctly - do not provide
cope snippets, they are as useful as breasts
on a bull.

Cut your problem down to the minimum
self contained, compileable code that
demonstrates the problem, and post that.
 
B

B J H

the url works fine here. ive no other way to show it...

OK try again...

Have a class which sets up a GUI - a "launcher" class contains the "main"
method.

In this interface is one window. I have two buttons. Basically when the
buttons are pressed, the test of two labels is changed, and the screen
repaint()-ed.

Here's less code...

import java.awt.*;
import java.awt.event.*;
public class ReactorInterface extends Panel implements ActionListener
{
private Button increaseButton = new Button("Increase Temperature");
private Button decreaseButton = new Button("Decrease Temperature");
private Label tempLabel = new Label();
private Label alarmLabel = new Label();
etc. etc.

Here's the event handler to change the text of the two labels...

public void actionPerformed(ActionEvent e)
{

if( e.getSource() == increaseButton )
{
if ( reactor1.increaseTemp() == true )

(this calls a method from a diff class which returns an true if an alarm
must be displayed, and false if not)
....

{
alarmLabel.setText("** MAX TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}

tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}

if (e.getSource() == decreaseButton)
{
if (reactor1.decreaseTemp() == true)
{
alarmLabel.setText("** MIN TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}
tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}
}

This is all the basic code I'm working with.

Yet when the button is clicked, the text of the labels does not show
properly. It kind of shows the first few characters. as in

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box.jpg
 
A

Andrew Thompson

B J H said:
I'm using the following code to try and change the contents of some labels
when certain buttons are clicked...but the display is looking mangled like
this...

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box.jpg

OK - I'm getting it now.

I'd say you need to ensure the Labels are
big enough to handle the largest text you
want to put in them. This depends a lot
on the layout of the GUI.

By the look of your GUI, I guess it could be
recreated in less than 50 lines. So, how
about a short, self contained, compileable
example?

BTW - the _NEXT_ time you have a
question like this, it may be worth posting
it to c.l.j.gui, rather than c.l.j.programmer,
they specialize in thos stuff.

For the moment, I am gonna risk cross-posting
it to c.l.j.g
 
A

Andrew Thompson

B J H said:
the url works fine here. ive no other way to show it... ....
Here's less code...

Problem is, while I am prepared to look at
a self contained, compileable example, I am
not prepared to look at 'code snippets', even
if they are 'less code'.

If they actually include the problem you
are observing, it is by accident.
 
B

B J H

Thanks for the advice.

You were right - I needed to make sure the Frame could accomodate the text I
was putting into it.

Done that now, and it's working

Next problem.....(the subject in this post is still relevant so I feel I
shoudln't post again...)

I want one of the labels to appear on the buttom half of the window (well to
keep at the bottom centre really when the window is resized). At the moment,
I've just added the components in the order I want them, and Java has put
them automatically onto the window.

Is there a way to do this? the following is how I want it to look like
this..

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box2.jpg

Currently, it's looking like this...

http://personalpages.umist.ac.uk/student/B.Hamilton-2/box3.jpg

As you can see, the lower label (which is called alarmLabel), is too high.

Any suggestions?

TIA
 
A

Andrew Thompson

B J H said:
Thanks for the advice.

You were right - I needed to make sure the Frame could accomodate the text I
was putting into it.

It's a bit tricky knowing who you are talking
to when you do not provide a short 'quote'
to give it context.

But I will comment - you still have not posted
the code I was refering to. (..And we are not psychic)
 
B

B J H

/*
* File: ReactorInterface.java
* Description: A class which provides a GUI for the Reactor class.
* <Uses: Reactor class>
*/
import java.awt.*;
import java.awt.event.*;
public class ReactorInterface extends Panel implements ActionListener
{
private Button increaseButton = new Button("Increase Temperature");
private Button decreaseButton = new Button("Decrease Temperature");
private Label tempLabel = new Label();
private Label alarmLabel = new Label();
private Reactor reactor1 = new Reactor();
private String currentTemp;
private String templabel;

public ReactorInterface()

{
add(increaseButton);
add(decreaseButton);
add(tempLabel);
add(alarmLabel);
increaseButton.addActionListener(this);
decreaseButton.addActionListener(this);
}

/*public void paint(Graphics g)
{
//currentTemp = reactor1.getTemperature();
//templabel = "The current temperature of the reactor is: "
//tempLabel.setText("templabel");


//more goes here
}
*/



public void actionPerformed(ActionEvent e)
{

if( e.getSource() == increaseButton )
{
if ( reactor1.increaseTemp() == true )
{
alarmLabel.setText("** MAX TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}

tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}

if (e.getSource() == decreaseButton)
{
if (reactor1.decreaseTemp() == true)
{
alarmLabel.setText("** MIN TEMP ALARM **");
}
else
{
alarmLabel.setText("Reactor is in safe state");
}
tempLabel.setText( "The current reactor temperature is: " +
reactor1.getTemperature() );
repaint();
}
}











}
 
V

VisionSet

B J H said:
/*
* File: ReactorInterface.java
* Description: A class which provides a GUI for the Reactor class.
* <Uses: Reactor class>
*/
....

And Reactor.java is where exactly?
 
S

Sudsy

B said:
I'm using the following code to try and change the contents of some labels
when certain buttons are clicked...but the display is looking mangled like
this...
Any ideas?

You need to learn about layout managers. Try the following wholesale
replacement of the body of your constructor:

public ReactorInterface() {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
setLayout( layout );
constr.gridx = constr.gridy = 0;
layout.setConstraints( increaseButton, constr );
add(increaseButton);
constr.gridx++;
layout.setConstraints( decreaseButton, constr );
add(decreaseButton);
constr.gridx = 0;
constr.gridy++;
constr.gridwidth = 2;
constr.fill = GridBagConstraints.HORIZONTAL;
tempLabel.setAlignment( Label.CENTER );
layout.setConstraints( tempLabel, constr );
add(tempLabel);
constr.gridy++;
alarmLabel.setAlignment( Label.CENTER );
layout.setConstraints( alarmLabel, constr );
add(alarmLabel);
increaseButton.addActionListener(this);
decreaseButton.addActionListener(this);
}

I also addressed the centering of the labels. Try it out and
then follow the yellow brick road through the javadocs to
figure out how it does what it does.
 
B

B J H

Reactor.java is irrelevant if you looked at my quesion (which related to the
positioning of elements on the screen). That is why I did not post it (as I
feared being flamed for posting irrelevant code).
Here goes..


/*
* File: Reactor.java
* Description: A class which is used to control the temperature of a Reactor
* object by providing methods which increase, decrease and display the
* current temperature. THIS CLASS IS INCOMPLETE.
*/
public class Reactor
{
private static final int MAX = 10; // set maximum temperature
private static final int MIN = 0; // set minimum temperature
private int temperature;
private boolean alarm;
public Reactor()
{
temperature = 0; // set initial level
}
public int getTemperature()
{
return temperature;
}

public boolean increaseTemp()
{
boolean alarm;
if (temperature < MAX)
{
temperature++;
alarm = false;
}
else
{
temperature = 0;
alarm = true;
}
return alarm;
}

public boolean decreaseTemp()
{
if (temperature >= MIN)
{
temperature--;
alarm = false;
}
else
{
temperature = 0;
alarm = true;
}
return alarm;
}




} //end class Reactor
 
B

B J H

I will take the classes you posted and search for them on the IDE on the Sun
web site - there must be a way to do it I can find in there.

Thanks for the advice.
 
A

Andrew Thompson

B J H said:
Reactor.java is irrelevant if you looked at my quesion (which related to the
positioning of elements on the screen).

The other code called it, therefore it was relevant.
..That is why I did not post it (as I
feared being flamed for posting irrelevant code).
Here goes..

This is only small so I doubt it would have been
a problem.

The original point I was making was to encapsulate
the problem in separate source, if necessary. This
rarely takes more than 100 lines, all up, and usually
helps you find the problem yourself.

Having said that, here is how _I_ would implement
this (wrt layout)..
______________________________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ReactorLayoutFrame extends JFrame implements ActionListener
{
JButton bUp = new JButton("Increase Temerature"),
bDn = new JButton("Decrease Temerature");

JLabel lDocs = new JLabel("The current temperature of the reactor is: "),
lMessage = new JLabel("");
int initialTemp = 22;
JTextField tfTemperature = new JTextField(""+initialTemp, 3);

String sMax = new String("** MAX TEMP ALARM **"),
sMin = new String("** MIN TEMP ALARM **"),
sSafe = new String("Reactor is in safe state");

public final int MIN = 20, MAX = 25;

ReactorLayoutFrame()
{
super("Reactor");

tfTemperature.setEnabled(false);
bDn.addActionListener(this);
bUp.addActionListener(this);

Container c = this.getContentPane();
c.setLayout( new BorderLayout() );

JPanel pButtons = new JPanel(new GridLayout());
pButtons.add( bDn );
pButtons.add( bUp );

c.add( pButtons, BorderLayout.NORTH );

JPanel pTempReadOut = new JPanel(new FlowLayout());
setMessage(initialTemp);
pTempReadOut.add( lDocs );
pTempReadOut.add( tfTemperature );

c.add( pTempReadOut, BorderLayout.CENTER );

JPanel pMessage = new JPanel( new FlowLayout(FlowLayout.CENTER) );
pMessage.add( lMessage );

c.add( pMessage, BorderLayout.SOUTH );

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==bUp) changeTemp(1);
else changeTemp(-1);
}

private void changeTemp(int amount)
{
int currentTemp = Integer.parseInt( tfTemperature.getText() );
currentTemp += amount;
setMessage(currentTemp);
tfTemperature.setText( "" + currentTemp );
}

private void setMessage(int temperature)
{
if (temperature<=MIN) lMessage.setText(sMin);
else if (temperature>=MAX) lMessage.setText(sMax);
else lMessage.setText(sSafe);
}

public static void main(String[] args)
{
ReactorLayoutFrame f = new ReactorLayoutFrame();
}
}
______________________________________
 
S

Sudsy

Andrew,
We came to almost the same conclusion. BorderLayout with a nested
GridLayout for the buttons is good. I just added a new Panel with a
BorderLayout, put the tempLabel in the NORTH, and added at CENTER in
the containing panel.
It's interesting that, once you become familiar with the various
layout managers, you can come with different approaches which achieve
the same goal. Hmmm...kind of like *nix; more than one way to skin a
cat.
 
A

Andrew Thompson

Sudsy said:
Andrew, ....
....Hmmm...kind of like *nix; more than one way to skin a
cat.

You need all the ways you can get,
...cats have 9 lives. ;-)

A.
 
M

Mike

I haven't tried your code sample, but I often find I need to call
either
revalidate() or invalidate() and validate()
and have to keep tinkering with a combination to get Swing to
reconsider its layout.
Some might say, call doLayout on something, but in the end, you'll
probably find Swing is fairly messy when it comes to updating
components that have changed.

Try
invalidate()
validate()
repaint()
all 3
 
A

Andrew Thompson

Mike said:
"B J H" <[email protected]> wrote in message
I haven't tried your code sample,

Which you requoted in its entirety..
..but I often find I need to call
either
revalidate() or invalidate() and validate()
and have to keep tinkering with a combination to get Swing to
reconsider its layout.
Some might say, call doLayout on something, but in the end, you'll
probably find Swing is fairly messy when it comes to updating
components that have changed.

You'll probably find that code that fits your
description is poorly written.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top