JComboBox used to update a JLabel


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JComboBox used to update a JLabel

  1. #1
    Join Date
    Jan 2006
    Posts
    1

    JComboBox used to update a JLabel

    Hi Folks,

    I have a Combo box, with a list of contact names... When I select a name from the combo box it currently prints the selected name into my JAva Console. So I know the actionPerfomed and SetActionCommand etc... are all wired up correctly.

    What I would like it to do is rather than print the name to the console. I'd like it to update a JLabel. (that is currently, intentionally blank, for this purpose)

    Heres some code snippets that I hope help you understand what I have done so far.


    **THIS IS FROM MY CONTROLLER CLASS**

    public synchronized void actionPerformed (ActionEvent event) {

    String command = event.getActionCommand();

    JLabel nameFillLabel = new JLabel(); // not sure why put this here

    if(command.equals("recipientName")){
    JComboBox cb = (JComboBox)event.getSource();
    String combName = (String)cb.getSelectedItem();
    System.out.println(combName); // prints selected item to console
    nameFillLabel.setText(combName); // doesnt actually do anything
    }

    }



    ***THIS IS FROM MessagerUI class***

    private JComboBox recipientCombo = null;
    private JLabel nameFillLabel = null;
    ...
    recipientCombo = new JComboBox(names);
    nameFillLabel = new JLabel();
    ...
    recipientCombo = new JComboBox(names);
    recipientCombo.setActionCommand("recipientName");
    ...

    public void setActionListener(ActionListener sendActionEventsHere){
    recipientCombo.addActionListener(sendActionEventsHere);

    }


    I'm new to this and I think to problem may have something to do with me trying to update a JLabel thats contained within another class, if so how do I resolve this Issue, Any advice would be greatly appreciated.

    Thanks in Advance

    Regards

    Terry

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Don't create the label inside the eventhandler.

    The label should be created prior to the event. If the label and the eventhandler are contained in different classes you can get this done in many ways. I prefer this one:

    The class containing the eventhandler gets the pointer to the class containing the label as a contructor parameter (or a setter method) and the class with the label has a public method for setting the label value.

    The class with the label
    Code:
    public class AClass {
      private JLabel aLabel=new JLabel();
    .
    (place the label somewhere...)
    .
    public void setComboLabelValue(String s) {
      aLabel.setText(s);
    }
      
    }
    The eventhandler class' constructor would then be like:
    Code:
    public class ControllerClass {
    private AClass ac=null;
    public ControllerClass (AClass ac) {
      this.ac=ac;
    }
    .
    .
    public synchronized void actionPerformed (ActionEvent event) {
      String command = event.getActionCommand();
      if(command.equals("recipientName")){
        JComboBox cb = (JComboBox)event.getSource();
        String combName = (String)cb.getSelectedItem();
        System.out.println(combName); // prints selected item to console
        ac.setComboLabelValue(combName); // voila !
        
    .
    .
    .
    Last edited by sjalle; 01-08-2006 at 11:54 AM.
    eschew obfuscation

Similar Threads

  1. Replies: 0
    Last Post: 06-04-2002, 11:13 AM
  2. update query
    By an in forum Database
    Replies: 0
    Last Post: 05-08-2002, 02:12 PM
  3. Replies: 0
    Last Post: 05-07-2002, 09:34 PM
  4. Update Fails
    By Perry in forum VB Classic
    Replies: 1
    Last Post: 09-11-2000, 06:45 PM
  5. Urgent!!! ADO Update conflict
    By Chillu in forum VB Classic
    Replies: 0
    Last Post: 07-14-2000, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center