Hi
Now I've tried for a few days to make an interface which takes the
text of a button and adds it to a JLabel. It is also partly succeed -
When I println the information to the prompt about the JLabel after
pressing a button it shows me the right updated JLabel text, but in
the GUI the JLabel have not been updated. It shows the text from the
begining.
The code is below! It is placed on a JLayered defined in a nother
class file. Sorry if the code is a bit strange, I haven't made that
much Java programming before. First I've made the text change in the
actionlistener but that didn't work so I tried to make a function to
update the JLabel as seen below. What could be wrong in both cases.
Changing the JLabel in the actionlistener and/or in the class file
should both be possible right?!
Thanks in advance.
/Flemming
public class RegisterEvent extends JPanel
{
public static InformationString informationStringPanel;
public static RegisterEventButtonPanel registerEventButtonPanel;
public static String markeventlabel;
public static RegisterEvent registerEvent;
public static JLabel InformationStringField;
public RegisterEvent()
{
markeventlabel = new String("Vælg Event");
InformationStringField = new JLabel("Ini");
setLayout( new BorderLayout() );
setBounds(0,0,800,500);
informationStringPanel = new InformationString(markeventlabel,
InformationStringField);
RegisterEventButtonPanel registerEventButtonPanel = new
RegisterEventButtonPanel();
add(informationStringPanel, BorderLayout.NORTH);
add(registerEventButtonPanel, BorderLayout.SOUTH);
}
public static void update(String nytext)
{
markeventlabel = nytext;
InformationStringField.setText(nytext);
System.out.println("\n\n\n\n" + nytext);
//informationStringPanel = new InformationString(markeventlabel,
InformationStringField);
}
class EventActionListener implements ActionListener
{
public String labeltext;
public String knaptext;
public String stringtxt;
public String nytext;
public void actionPerformed( ActionEvent e )
{
JButton pressedButton;
//labeltext = InformationStringField
pressedButton = (JButton)e.getSource();
knaptext = pressedButton.getLabel();
stringtxt = RegisterEvent.markeventlabel;
RegisterEvent.markeventlabel = (stringtxt + " - " + knaptext);
RegisterEvent.update(RegisterEvent.markeventlabel);
informationStringPanel = new
InformationString(RegisterEvent.markeventlabel,
InformationStringField);
add(informationStringPanel, BorderLayout.NORTH);
}
}
}
class RegisterEventButtonCreator extends JButton
{
public Color baggrundColor;
public Color trykketColor;
protected JButton RegisterEventButton;
String txtButton;
public RegisterEventButtonCreator(String textButton)
{
baggrundColor = new Color(192,255,255);
trykketColor = new Color(0,255,0);
txtButton = textButton;
setLabel( txtButton );
setBorderPainted( false );
setPreferredSize( new java.awt.Dimension(170,70) );
setBackground( baggrundColor );
setForeground( new Color(0,0,0) );
setSize( new Dimension(220,70) );
setFont(new Font("Times New Roman", Font.BOLD, 14));
addActionListener( new EventActionListener());
}
void ChangeButtontxt(String txtButton)
{
setLabel( txtButton );
}
void ChangeButtonColor() //indsæt argument...
{
//Change buttoncolor med argument.
}
}
class InformationString extends JPanel
{
static String EventString;
static JPanel informationStringPanel;
public InformationString(String markEventlabel, JLabel
InformationStringField)
{
InformationStringField = new JLabel(markEventlabel);
InformationStringField.setPreferredSize( new
java.awt.Dimension(750, 20));
InformationStringField.setBackground( new Color(255,255,0));
InformationStringField.setForeground( new Color(0,0,0));
InformationStringField.setBounds(0,0,750,20);
InformationStringField.setFont(new Font("Times New Roman",
Font.BOLD, 14));
setLayout(new BorderLayout());
setLocation( new java.awt.Point(0,100));
setSize( new java.awt.Dimension(800,20));
setPreferredSize(new java.awt.Dimension(800,20));
setBackground( new Color(255,255,0));
add(InformationStringField, BorderLayout.CENTER);
}
}
class RegisterEventButtonPanel extends JPanel
{
public RegisterEventButtonCreator proAirWayButton;
public RegisterEventButtonCreator proCircButton;
public RegisterEventButtonCreator proDivButton;
public RegisterEventButtonCreator medicinSedaButton;
public RegisterEventButtonCreator medicinCarButton;
public RegisterEventButtonCreator infusionButton;
public RegisterEventButtonCreator occurButton;
public RegisterEventButtonCreator divButton;
public RegisterEventButtonPanel()
{
setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
setLocation( new java.awt.Point(0,120));
setSize( new java.awt.Dimension(800,480));
setPreferredSize(new java.awt.Dimension(800,480));
setBackground( new Color(0,128,128) );
RegisterEventButtonCreator proAirWayButton = new
RegisterEventButtonCreator("Procedure Luftveje");
RegisterEventButtonCreator proCircButton = new
RegisterEventButtonCreator("Procedure Kredsløb");
RegisterEventButtonCreator proDivButton = new
RegisterEventButtonCreator("Procedure Andre");
RegisterEventButtonCreator medicinSedaButton = new
RegisterEventButtonCreator("Medicin - Narkose Sedation");
RegisterEventButtonCreator medicinCarButton = new
RegisterEventButtonCreator("Medicin - Cardiovaskulær");
RegisterEventButtonCreator infusionButton = new
RegisterEventButtonCreator("Infusion");
RegisterEventButtonCreator occurButton = new
RegisterEventButtonCreator("Hændelse");
RegisterEventButtonCreator divButton = new
RegisterEventButtonCreator("Diverse");
add( proAirWayButton);
add( proCircButton);
add( proDivButton);
add( medicinSedaButton);
add( medicinCarButton);
add( infusionButton);
add( occurButton);
add( divButton);
}
}
Andrew Thompson - 29 Apr 2004 08:38 GMT
> Now I've tried for a few days to make an interface which takes the
> text of a button and adds it to a JLabel.
I tried for a few minutes to get your
code to compile.
I fixed the imports
I fixed the broken lines
I fixed the deprecated methods..
Then it asked for an EventActionListener..
What the _hell_ is that?
No ..do not post it. You should be
able to recreate your problem in under
100 lines of SELF CONTAINED code.
Check here for tips..
<http://www.physci.org/codes/sscce.jsp>
BTW - it might be..
<http://www.physci.org/guifaq.jsp#2.1>

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Roedy Green - 29 Apr 2004 10:19 GMT
>I tried for a few minutes to get your
>code to compile.
>
>I fixed the imports
>I fixed the broken lines
>I fixed the deprecated methods..
see http://mindprod.com/jgloss/newsgroups.html
for how to avoid getting flamed like this.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Flemming - 29 Apr 2004 23:43 GMT
> >I tried for a few minutes to get your
> >code to compile.
[quoted text clipped - 5 lines]
> see http://mindprod.com/jgloss/newsgroups.html
> for how to avoid getting flamed like this.
Sorry for taking your time. I went though your links and found my
error.
I'm telling here if some else are interested.
When I wanted to get the label to change I need to get the string for
the JLabel and not the one I defined in the begining. So the call for
stringtext should be change to jlabal1.getText();
/Flemming