문제

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();
  }
 }
}
도움이 되었습니까?

해결책

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.

다른 팁

JComboBox.getSelectedItem() returns type Object, not String. You can call toString() on its result to return the string representation of your object. It looks as if you're trying to return a type of Topic, which means you'll need to override the toString() method on Topic to return the value you want.

Try the following code

topicCombobox.getSelectedItem() instanceof String ? (String)topicCombobox.getSelectedItem() : "Socks";

This is a temporary fix, because I don't know if the incoming getSelectedItem() is a String.
If you know it always will be just cast it

(String)topicCombobox.getSelectedItem()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top