• 0

Yet another simple java question...


Question

I need to get the text of JLabel in "labelHelp" method to show up before the button which is created in "helpDialog" method.

I do not know why, but the text does not appear before the button... it doesn't appear anywhere.

helpDialog is called in the main method at the bottom of the program.

If any more code is required let me know XD

public void labelHelp ()

{

JLabel help;

help = new JLabel ("Help Me!!");

help.setFont (new Font ("serif", Font.PLAIN,16));

add (help);

}

public void helpDialog ()

{

d = new JDialog (this, "Help Me");

d.setSize (200, 200);

d.setResizable (false);

labelHelp ();

d.getContentPane ().setLayout (new FlowLayout ());

JButton closeButton = new JButton ("Close");

closeButton.addActionListener (new ActionListener ()

{

public void actionPerformed (ActionEvent e)

{

d.dispose ();

}

});

d.getContentPane ().add (closeButton);

d.setLocationRelativeTo (this);

d.show ();

}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Right as for this one, this may or may not be the right answer (so try it and see) as GUIs aren't my strong point and someone will probably come along with a much better answer.

Anyway from just looking at the code, you're adding the label by calling add(help); ...but I don't think that method is being called on any object really...?

So I'm guessing you'd have to do something like d.getContentPane().add(help); (again I'm not sure, so have a play around, this was entirely a guess!) and so I guess you could pass in the dialog to the method in order to call getContentPane() on it?

public void labelHelp (JDialog d)
{
JLabel help;
help = new JLabel ("Help Me!!");
help.setFont (new Font ("serif", Font.PLAIN,16));
d.getContentPane().add(help);
}

and then call -

labelHelp (d);

Try that see if any of that works :p *think I need to read up on GUIs*

Link to comment
Share on other sites

  • 0

Right as for this one, this may or may not be the right answer (so try it and see) as GUIs aren't my strong point and someone will probably come along with a much better answer.

Anyway from just looking at the code, you're adding the label by calling add(help); ...but I don't think that method is being called on any object really...?

So I'm guessing you'd have to do something like d.getContentPane().add(help); (again I'm not sure, so have a play around, this was entirely a guess!) and so I guess you could pass in the dialog to the method in order to call getContentPane() on it?

public void labelHelp (JDialog d)
{
JLabel help;
help = new JLabel ("Help Me!!");
help.setFont (new Font ("serif", Font.PLAIN,16));
d.getContentPane().add(help);
}

and then call -

labelHelp (d);

Try that see if any of that works :p *think I need to read up on GUIs*

I had a jaw dropping moment...

Cheers to you pal! :D

Not only did it NOT give me any errors, but it fkn worked!!!

Could you explain something to me though?

What did the parameter "JDialog d" do in the labelHelp method?

Link to comment
Share on other sites

  • 0

I had a jaw dropping moment...

Cheers to you pal! :D

Not only did it NOT give me any errors, but it fkn worked!!!

Could you explain something to me though?

What did the parameter "JDialog d" do in the labelHelp method?

Ah that's good! I'm not entirely sure it is the best way but it works at least!

If you see how you added the button to the dialog, you first got your contentPane FROM the dialog (d) and then you added the button. I think it was no different to adding a JLabel.

However, when you called your method to create your label and then add it (to somewhere), you simply called add(JLabel)... but it would have no idea WHERE to add it. (Did you get any compiler errors on this line? When I tried it, I did I think).

By passing in the JDialog, within that method we can then use that JDialog d to get the contentPane, and then add it (just as we added the button, but in another method). That way, your method knows exactly which dialog to add the JLabel to.

Hope that isn't too confusing for you to understand... but yeah basically we're passing in a dialog so we can tell the method... here is the dialog where I want the label placed, so get the contentPane of this dialog and add the label there.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.