We help IT Professionals succeed at work.

Java system outputing string twice

oggiemc
oggiemc asked
on
Hello all,
Can someone tell me why the attached code is outputting strings to the console twice??

Thanks
************ clientGUI *************
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ClientGUI extends JFrame implements ActionListener {
	public static final int defaultWidth = 80, defaultHeight = 40;
	
	  
	   private String [] listChoices = {"Football", "Music"};
	   private JList streamList;
	   private List list;
	   
	   private Button  clearButton;
	   private Button  sendButton;
	   
	   private JTextArea consoleDisplay;
	   
	   private JScrollPane consoleScroll;
	   
	   public ClientGUI() {
	        super("ClientGUI");
		    Panel topPanel = new Panel();
		    Panel bottomPanel = new Panel();
	        
	        consoleDisplay = new JTextArea(200, 7);
	        consoleDisplay.setLineWrap(true);
	        consoleScroll = new JScrollPane(consoleDisplay);
	        
	        list = new List();
	        list.add("damian");
	        list.add("conor");
	        list.addActionListener(this);
	        topPanel.add(list);
	        
	        streamList = new JList(listChoices);
	        topPanel.add(streamList);
	        streamList.addListSelectionListener(
        			new ListSelectionListener(){
						public void valueChanged(ListSelectionEvent e) {							
							getStreamChoice();
						}   	
        });
	        
	        clearButton = new Button("Clear");
	        clearButton.setBackground(Color.white);
	        clearButton.addActionListener(this);
	        topPanel.add(clearButton);
	        
	        sendButton = new Button("Send");
	        sendButton.addActionListener(this);
	        topPanel.add(sendButton);
	        
	        bottomPanel.add(consoleDisplay);

	        this.add(topPanel, BorderLayout.NORTH);
	        this.add(bottomPanel, BorderLayout.CENTER);

	        this.setSize(400, 400);
	        this.setVisible(true);
	        
	        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	    }  
	 

 public static void main(String[] args) {
    new ClientGUI();
}

@Override
 public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub	
 }


 public void getStreamChoice(){
	
	if(streamList.getSelectedValue().equals("Football")){
		FootballStream footy = new FootballStream();
		footy.messageStream.add("hello, welcome to the football stream");
		System.out.println(footy.messageStream.firstElement());
	}
	else{
		System.out.println("Music");
	}	
 }	        	
}

************** FootballStream **************

import java.io.Serializable;
import java.util.Vector;

public class FootballStream implements Serializable{
	
	Vector<String> messageStream;
	
	public FootballStream(){
		this.messageStream = new Vector<String>();
		
	}

}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:

check here discussion and solution of similar issue with ListSelcetionEvent:
http://forums.devshed.com/java-help-9/listselectionlistener-registers-two-events-on-one-mouse-click-285809.html
GlobaLevelProgrammer

Commented:
looks good to me...because its a listener...is something tripping it twice during runtime?
Awarded 2011
Awarded 2011
Commented:
This prints only once:

import java.awt.event.ActionEvent;


import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ClientGUI extends JFrame implements ActionListener {
	public static final int defaultWidth = 80, defaultHeight = 40;


	   private String [] listChoices = {"Football", "Music"};
	   private JList streamList;
	   private List list;

	   private Button  clearButton;
	   private Button  sendButton;

	   private JTextArea consoleDisplay;

	   private JScrollPane consoleScroll;

	   public ClientGUI() {
	        super("ClientGUI");
		    Panel topPanel = new Panel();
		    Panel bottomPanel = new Panel();

	        consoleDisplay = new JTextArea(200, 7);
	        consoleDisplay.setLineWrap(true);
	        consoleScroll = new JScrollPane(consoleDisplay);

	        list = new List();
	        list.add("damian");
	        list.add("conor");
	        list.addActionListener(this);
	        topPanel.add(list);

	        streamList = new JList(listChoices);
	        topPanel.add(streamList);
	        streamList.addListSelectionListener(
        			new ListSelectionListener(){

						public void valueChanged(ListSelectionEvent e) {
                            if (! e.getValueIsAdjusting()){
                            System.out.println("here");
							getStreamChoice();
                            }
						}
        });

	        clearButton = new Button("Clear");
	        clearButton.setBackground(Color.white);
	        clearButton.addActionListener(this);
	        topPanel.add(clearButton);

	        sendButton = new Button("Send");
	        sendButton.addActionListener(this);
	        topPanel.add(sendButton);

	        bottomPanel.add(consoleDisplay);

	        this.add(topPanel, BorderLayout.NORTH);
	        this.add(bottomPanel, BorderLayout.CENTER);

	        this.setSize(400, 400);
	        this.setVisible(true);

	        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	    }


 public static void main(String[] args) {
    new ClientGUI();
}


 public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub
 }


 public void getStreamChoice(){

	if(streamList.getSelectedValue().equals("Football")){
		FootballStream footy = new FootballStream();
		footy.messageStream.add("hello, welcome to the football stream");
		System.out.println(footy.messageStream.firstElement());
	}
	else{
		System.out.println("Music");
	}
 }
}





 class FootballStream implements Serializable {

	Vector<String> messageStream;

	public FootballStream(){
		this.messageStream = new Vector<String>();

	}

}

Open in new window