is this feasible? (newbie question)

N

Newbie

I'm passing the param "name" from a seperate class into my new class
where the majority of my code lies

I would like to have a JLabel within the titlebar of my new program
that can be changed dynamically using the setText() through an
actionListener() using a combobox as the firing component.

however, when i try this the title bar fills with the JLabel constraints
as apposed to the text that it holds

Can some kind soul point out the obvious to me as my head is starting
to bleed from hitting against the brick wall

Code is below:
------------------------------------------
public void newName(String name)
{
title = new JLabel(name);
setTitle(""+getContentPane().add(title));
return;
}
------------------------------------------
 
C

Christophe Vanfleteren

Newbie said:
I'm passing the param "name" from a seperate class into my new class
where the majority of my code lies

I would like to have a JLabel within the titlebar of my new program
that can be changed dynamically using the setText() through an
actionListener() using a combobox as the firing component.

however, when i try this the title bar fills with the JLabel constraints
as apposed to the text that it holds

Can some kind soul point out the obvious to me as my head is starting
to bleed from hitting against the brick wall

Code is below:
------------------------------------------
------------------------------------------

Yes you can alter the text in the titlebar using the text of the JLabel, but
you'll have to use the getText() method of the JLabel.

If you don't specify getText() in the setTitle method, the toString() method
of the object (JLabel in your case) will get called (because you try to
concatenate it with a String, ""), and the toString implementation of JLabel
shows it's properties instead of the title.

Just beware that the title will not get updated if the text of the label
changes.

The simplest way to code what you want is

public void newName(String name)
{
setTitle(name);
//explicit return was not necessary
}

creating a JLabel was totally unnecessary.
 
N

Newbie

Christophe Vanfleteren wrote in message
Yes you can alter the text in the titlebar using the text of the JLabel, but
you'll have to use the getText() method of the JLabel.

If you don't specify getText() in the setTitle method, the toString() method
of the object (JLabel in your case) will get called (because you try to
concatenate it with a String, ""), and the toString implementation of JLabel
shows it's properties instead of the title.

Just beware that the title will not get updated if the text of the label
changes.

The simplest way to code what you want is

public void newName(String name)
{
setTitle(name);
//explicit return was not necessary
}

creating a JLabel was totally unnecessary.

The whole purpose of this was to be able to change the title bar text at
will
through a combobox.

I'm more than aware that in a "real life" situation, this wouldn't be
required
however, for my college assessment, I passed the var as asked, but wanted
to try having the titlebar be updated at will from within the main program.

thanks for your assistance.
 
J

John C. Bollinger

Newbie said:
Christophe Vanfleteren wrote in message
[...]
The simplest way to code what you want is

public void newName(String name)
{
setTitle(name);
//explicit return was not necessary
}
[...]

The whole purpose of this was to be able to change the title bar text at
will
through a combobox.

I'm more than aware that in a "real life" situation, this wouldn't be
required
however, for my college assessment, I passed the var as asked, but wanted
to try having the titlebar be updated at will from within the main program.

This is certainly possible, but it gets rather deeper into Java GUI
programming. What you need to do is set an ActionListener on the
JComboBox, which, on receiving an ActionEvent, gets the currently
selected item from the JComboBox and uses it to set the title. The
actual code is left as an excercise ( :) ), but I do recommend that you
read the GUI trail in the Java Tutorial for help
(http://java.sun.com/docs/books/tutorial/uiswing/index.html). If you
still can't figure it out then come back.


John Bollinger
(e-mail address removed)
 
N

Newbie

John said:
This is certainly possible, but it gets rather deeper into Java GUI
programming. What you need to do is set an ActionListener on the
JComboBox, which, on receiving an ActionEvent, gets the currently
selected item from the JComboBox and uses it to set the title. The
actual code is left as an excercise ( :) ), but I do recommend that you
read the GUI trail in the Java Tutorial for help
(http://java.sun.com/docs/books/tutorial/uiswing/index.html). If you
still can't figure it out then come back.


John Bollinger

John,

I tried to do what I wanted with the title bar, however I couldnt get it to
update at
run-time when using a String.
I then thought, hmmmm, maybe I could use a JLabel and utilizing the
getText() &
setText() methods, I 'could' do it from within the actionlistener.
All I got was the constraints for the JLabel showing up apposed to what the
JLabel
held inside (the actual string param).
(2nd param change was the background color of the program)
I sussed out (and felt i saved alot of time) the JColorChoser dialog using
the actionListener
with the getSelectedItem() method to retrieve the option choosen.
Re-adding the option to "change title" isnt difficult.
My only trouble was having it repaint the frame titlebar dynamically with a
new value at run-time

Hope this helps. I dont come to this NG hoping for others to do my own
homework.
The criteria has already been met regards what my professor asked for.
(he didn't stipulate that he wanted the option to change the params 'again'
at run-time,
only that 2 params were passed from one class to another class for
processing through
prompting the user for input, all of which is already in place :) )

With java being a large part of the real world these days, I wanted to
achieve more than
the basic requirements for my own knowledge. :)

I have included a snippet from the code showing the actionlistener I have
setup awaiting
command from the JComboBox.

Thankyou very much for your assistance.
----------------------------------------------------------------

public void actionPerformed (ActionEvent evt)
{
//change color
if (options.getSelectedItem() == "change color")
{
clr = JColorChooser.showDialog(null,
"Select background color for the conversion
program",Color.yellow);
inputPanel.setBackground(clr);
outputPanel.setBackground(clr);
optionsPanel.setBackground(clr);
}
}//end actionPerformed
--------------------------------------------------------------
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top