Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4100618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:35:26+00:00 2026-05-20T20:35:26+00:00

Ive a Listner class called TopicS Im trying to call it from a gui

  • 0

Ive a Listner class called TopicS Im trying to call it from a gui called readMessages

When Im trying to run the class TopicS using the following method,

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.println("test test test"); 
    System.out.print("you pressed" +topicCombobox.getSelectedItem());
    TopicS a = new TopicS();
    a.addTopicToListner(topicCombobox.getSelectedItem());
}                 

It gives me error saying

addTopicListner(java.lang.String) in Topics Cannot be applied to (java.lang.Object)

When I change the String to Object I get other errors. The main method is included below, this works fine without GUI, but I need to add it to GUI. What I am trying to do is take value to combobox which is String array, and place that string into topic (where the (t) is now

 import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicS implements MessageListener
{

 private TopicConnection topicConnection;
 private TopicSession topicSession;
 public Topic topic;
 private TopicSubscriber topicSubscriber;


 public TopicS()
            {}
            public void addTopicToListner(String t){
  try
  {
   // create a JNDI context
   Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
   properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/");
   Context context = new InitialContext(properties);

   // retrieve topic connection factory
   TopicConnectionFactory topicConnectionFactory = 
       (TopicConnectionFactory)context.lookup("JmsTopicConnectionFactory");
   // create a topic connection
   topicConnection = topicConnectionFactory.createTopicConnection();

   // create a topic session
   // set transactions to false and set auto acknowledgement of receipt of messages
   topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

   // retrieve topic
   topic = (Topic) context.lookup(t);

   // create a topic subscriber and associate to the retrieved topic
   topicSubscriber = topicSession.createSubscriber(topic);

   // associate message listener
   topicSubscriber.setMessageListener(this);

   // start delivery of incoming messages
   topicConnection.start();
  }
  catch (NamingException e)
  {
   e.printStackTrace();
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 } 

/* public static void main(String[] args)
 //{

  try
  {
   TopicS listener = new TopicS();
   Thread.currentThread().sleep(2000);
  }

  catch (InterruptedException e)
  {
   e.printStackTrace();
  }
 }
 */
 // process incoming topic messages
 public void onMessage(Message message)
 {
  try
  {
   String messageText = null;
   if (message instanceof TextMessage)
    messageText = ((TextMessage)message).getText();
   System.out.println(messageText);
  }
  catch (JMSException e)
  {
   e.printStackTrace();
  }
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:35:26+00:00Added an answer on May 20, 2026 at 8:35 pm

    That’s because JComboBox.html.getSelectedItem() returns Object

    public Object getSelectedItem()
    

    And your method expects a string

    public void addTopicToListner(String t)
    

    If you’re 100% sure the contents of your combobox are string you just have to cast it:

    a.addTopicToListner( (String) topicCombobox.getSelectedItem());
    

    And that’s it.

    This code sample reproduces exactly your compilation error:

    class StringAndObject {
        public void workWithString( String s ) {} // We just care about 
        public void workWithObject( Object o ) {} // the signature. 
    
        public void run() {
    
            String s = ""; // s declared as String
            Object o = s;  // o declared as Object
    
            // works because a String is also an Object
            workWithObject( s );
            // naturally a s is and String
            workWithString( s );
    
    
            // works because o is an Object
            workWithObject( o );
            // compiler error.... 
            workWithString( o );
    
        }
    
    }
    

    Output:

    StringAndObject.java:19: workWithString(java.lang.String) in StringAndObject cannot be applied to (java.lang.Object)
            workWithString( o );
            ^
    1 error   
    

    As you see, the last call (workWithString(o) ) doesn’t compile even though it is a String object. It turns out the compiler only knows that o was declared as Object but it doesn’t have a way to know if that object is a string or is something else ( a Date for instance ).

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

long-time listener, first-time caller! I’m trying to connect to MySQL using the ADO.NET Entity
I'm trying to insert an opening <div class=container> after <body> and a closing </div>
So, I've started looking into using AspectJ to handle processing events when the state
I am trying to create an ExpandableListView with selectable elements(children) so that the selections
Morning, I would love help with my specific code but if you're busy any
I need some help! I have no doubt this is a dumb problem that
What's best practice for structuring an activity, which should begin by displaying an animated
Before i close my program (press X on the frame) i wish to have
I have an application that places several Windows within a com.extjs.gxt.desktop.client.Desktop. I need to
I have a problem with my Spring application where my @Service classes are being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.