Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Applet che mi apre 2 volte una pagina web invece di una o_O

    Salve. Utilizzando il metodo showDocument di AppletContext specificando come secondo parametro la stringa "_blank", mi si dovrebbe aprire la pagina web specificata su una nuova finestra, ma il problema è che me la apre ben 2 volte! o_O

    codice:
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.awt.BorderLayout;
    import java.applet.AppletContext;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class SiteSelector extends JApplet
    {
    	private HashMap < Object, URL > sites;
    	private ArrayList < String > siteNames;
    	private JList siteChooser;
    	
    	public void init()
    	{
    		sites = new HashMap < Object, URL >();
    		siteNames = new ArrayList < String >();
    		
    		getSitesFromHTMLParameters();
    		
    		add (new JLabel ("Choose a site to browse"), BorderLayout.NORTH);
    		
    		siteChooser = new JList (siteNames.toArray());
    		siteChooser.addListSelectionListener
    		(
    			new ListSelectionListener()
    			{
    				public void valueChanged (ListSelectionEvent event)
    				{
    					Object object = siteChooser.getSelectedValue();
    					
    					URL newDocument = sites.get (object);
    					
    					AppletContext browser = getAppletContext();
    					
    					browser.showDocument (newDocument, "_blank");
    				}
    			}
    		);
    		
    		add (new JScrollPane (siteChooser), BorderLayout.CENTER);
    	}
    	
    	private void getSitesFromHTMLParameters()
    	{
    		String title;
    		String location;
    		URL url;
    		int counter = 0;
    		
    		title = getParameter ("title" + counter);
    		
    		while (title != null)
    		{
    			location = getParameter ("location" + counter);
    			
    			try
    			{
    				url = new URL (location);
    				sites.put (title, url);
    				siteNames.add (title);
    			}
    			catch (MalformedURLException exception)
    			{
    				exception.printStackTrace();
    			}
    			
    			counter++;
    			title = getParameter ("title" + counter);
    		}
    	}
    }
    Qualche idea?

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Applet che mi apre 2 volte una pagina web invece di una o_O

    Originariamente inviato da Dreamer89
    Qualche idea?
    Il problema non è certo nel showDocument() ma nel come viene gestito il ListSelectionListener. Per ogni azione di selezione viene prima inviato un ListSelectionEvent con getValueIsAdjusting() a true e poi un altro ListSelectionEvent con getValueIsAdjusting() a false.

    Più in generale, se premi su un elemento ma non rilasci il pulsante del mouse e "trascini" la selezione, ricevi tanti eventi (per ogni elemento su cui passi) con getValueIsAdjusting() a true. Quando rilasci il pulsante del mouse ricevi un evento con getValueIsAdjusting() a false.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3

    Re: Re: Applet che mi apre 2 volte una pagina web invece di una o_O

    Originariamente inviato da andbin
    Il problema non è certo nel showDocument() ma nel come viene gestito il ListSelectionListener. Per ogni azione di selezione viene prima inviato un ListSelectionEvent con getValueIsAdjusting() a true e poi un altro ListSelectionEvent con getValueIsAdjusting() a false.

    Più in generale, se premi su un elemento ma non rilasci il pulsante del mouse e "trascini" la selezione, ricevi tanti eventi (per ogni elemento su cui passi) con getValueIsAdjusting() a true. Quando rilasci il pulsante del mouse ricevi un evento con getValueIsAdjusting() a false.
    Grazie mille, ho risolto eseguendo showDocument solo se si verifica la condizione !event.getValueIsAdjusting()

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.