Een nieuw broser openen met een knop in javaapplet

Pagina: 1
Acties:
  • 92 views sinds 30-01-2008

  • Bernaar
  • Registratie: Juli 2001
  • Laatst online: 06-01 16:03
Hoi,
ik weet dat het eigenlijk niet de bedoeling is maar ik kan het nergens vinden;

Ik heb een Java-applet en ik wil als je op de knop 'internet' klikt, dat hij dan een nieuw browser opent. Het is gewoon een normale applet, dus niet op internet ofzo.

Thnx!

Bernaar

  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024

Alarmnummer

-= Tja =-

Hiermee kun je onafhankelijk van het os een browser openen.
(Stond ergens in een artikel op http://www.javaworld.com)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
public class BrowserControl

{

    /**

     * Display a file in the system browser.  If you want to display a

     * file, you must include the absolute path name.

     *

     * @param url the file's url (the url must start with either "http://"

or

     * "file://").

     */

    public static void displayURL(String url)

    {

      boolean windows = isWindowsPlatform();

      String cmd = null;

      try

      {

        if (windows)

        {

            // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'

            cmd = WIN_PATH + " " + WIN_FLAG + " " + url;

            Process p = Runtime.getRuntime().exec(cmd);

        }

        else

        {

            // Under Unix, Netscape has to be running for the "-remote"

            // command to work.  So, we try sending the command and

            // check for an exit value.  If the exit command is 0,

            // it worked, otherwise we need to start the browser.

            // cmd = 'netscape -remote openURL(http://www.javaworld.com)'

            cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";

            Process p = Runtime.getRuntime().exec(cmd);

            try

            {

              // wait for exit code -- if it's 0, command worked,

              // otherwise we need to start the browser up.

              int exitCode = p.waitFor();

              if (exitCode != 0)

              {

                // Command failed, start up the browser

                // cmd = 'netscape http://www.javaworld.com'

                cmd = UNIX_PATH + " "  + url;

                p = Runtime.getRuntime().exec(cmd);

              }

            }

            catch(InterruptedException x)

            {

              System.err.println("Error bringing up browser, cmd='" +

                           cmd + "'");

              System.err.println("Caught: " + x);

            }

        }

      }

      catch(IOException x)

      {

        // couldn't exec browser

        System.err.println("Could not invoke browser, command=" + cmd);

        System.err.println("Caught: " + x);

      }

    }

    /**

     * Try to determine whether this application is running under Windows

     * or some other platform by examing the "os.name" property.

     *

     * @return true if this application is running under a Windows OS

     */

    public static boolean isWindowsPlatform()

    {

      String os = System.getProperty("os.name");

      if ( os != null && os.startsWith(WIN_ID))

        return true;

      else

        return false;



    }

    /**

     * Simple example.

     */

    public static void main(String[] args)

    {

      displayURL("http://www.javaworld.com");

    }

    // Used to identify the windows platform.

    private static final String WIN_ID = "Windows";

    // The default system browser under windows.

    private static final String WIN_PATH = "rundll32";

    // The flag to display a url.

    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";

    // The default browser under unix.

    private static final String UNIX_PATH = "netscape";

    // The flag to display a url.

    private static final String UNIX_FLAG = "-remote openURL";

}

Als je alleen maar op windows werkt dan zou je ook het volgende kunnen zeggen.
code:
1
Runtime.getRuntime().exec("iexplorer.exe http://www.tweakers.net");

Verwijderd

Mij denkt je een exceptie krijg. Dit is niet voor applet.
Kun je niet gewoon een url openen en windows start automatisch de browser?? (Applet.loadUrl() of i.d.)

  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024

Alarmnummer

-= Tja =-

Op maandag 12 november 2001 14:10 schreef robh het volgende:
Mij denkt je een exceptie krijg. Dit is niet voor applet.
Kun je niet gewoon een url openen en windows start automatisch de browser?? (Applet.loadUrl() of i.d.)
Als je een applet draaid op je eigen bak dan kun je die security dingen wel uitzetten geloof ik. En dat is geloof ik de bedoeling.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Voortaan gewoon ff in de API docs kijken ;) .

java.applet.AppletContext:

public void showDocument(URL url, String target)

Requests that the browser or applet viewer show the Web page indicated by the url argument. The target argument indicates in which HTML frame the document is to be displayed. The target argument is interpreted as follows:

"_self" Show in the window and frame that contain the applet.

"_parent" Show in the applet's parent frame. If the applet's frame has no parent frame, acts the same as "_self".

"_top" Show in the top-level frame of the applet's window. If the applet's frame is the top-level frame, acts the same as "_self".

"_blank" Show in a new, unnamed top-level window.

"name" Show in the frame or window named name. If a target named name does not already exist, a new top-level window with the specified name is created, and the document is shown there.

An applet viewer or browser is free to ignore showDocument.

Parameters:
url - an absolute URL giving the location of the document.
target - a String indicating where to display the page.

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Bernaar
  • Registratie: Juli 2001
  • Laatst online: 06-01 16:03
Ok, thnx, ga ik morgen op school proberen :)
Ben een eigen (educatief) spel aan het maken... Voor groep 7/8...
Als je dan in het java-applet op de knop drukt krijg je info over landen in de vorm van een internet (op je harde schijf)

  • Bernaar
  • Registratie: Juli 2001
  • Laatst online: 06-01 16:03
Hoi,

ik heb dus 'public void showDocument(URL url, String target)' geprobeerd, maar hij zegt dan:

class URL not found in bla bla... Terwijl ik wel heb 'import java.applet.*;'

Dus weet jij hoe dat komt...???

Thnx

Bernaar

  • wasigh
  • Registratie: Januari 2001
  • Niet online

wasigh

wasigh.blogspot.com

opmat URL niet in java.applet.* staat maar in java.net.*
wat dus ook te vinden is in de api-docs.

je moet dus een import java.net.*; in je code zetten..

  • Bernaar
  • Registratie: Juli 2001
  • Laatst online: 06-01 16:03
Sorry, maar ik ben dus een newbie op het geboed van java :) (1e jaars student)
Ik heb dit nu:

package untitled5;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

public class Applet1 extends Applet
{

URL av;
String target;
Button knopje;

public void init()
{
knopje = new Button("Klik");
add(knopje);

av = new URL("http://www.av.com");
target = "_blank";
}

public void paint( Graphics g)
{

}

public boolean action( Event e, Object o)
{
if( e.target == knopje)
{


return true;
}
return false;
}

public void showDocument(URL av, String target)
{

}

}


Maar hij geeft een fout in de regel: 'av = new URL("http://www.av.com");' (must be caught or declared at bla bla)

En ik weet niet hoe je die 'imports' bekijkt ;)

Maar kunnen jullie me vertellen wat hier fout aan is?

Thnx

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
bvwillig: Maar hij geeft een fout in de regel: 'av = new URL("http://www.av.com");' (must be caught or declared at bla bla)
Hele duidelijke melding: de constructor van de klasse URL kan een exception gooien: MalformedURLException oid. Deze moet je opvangen. Zie voor info over exceptions:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/exception.html

Gevonden via de index:

http://java.sun.com/docs/books/tutorial/java/TOC.html#nutsandbolts

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • Bernaar
  • Registratie: Juli 2001
  • Laatst online: 06-01 16:03
Hij geeft geen foutmeldingen meer, maaaarr, hij doet niks als je op de knop klikt...

Weet iemand hoe dit komt?

package untitled1;

import java.awt.*;
import java.applet.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class Applet1 extends Applet
{

Button knopje;

public void init()
{
knopje = new Button("Klik");
add(knopje);

}

public void paint( Graphics g)
{

}

public boolean action( Event e, Object o)
{
if( e.target == knopje)
{
try
{
getAppletContext().showDocument(new URL("http://walk.to/spg"),"_top");
}
catch (Exception ex) {}

return true;
}
return false;
}

}


Ik hoop dat iemand iets weet, of een werkend voorbeeld heeft!!!

Thnx

  • Silver7
  • Registratie: Januari 2002
  • Laatst online: 14-05 21:13
/*
Hij geeft geen foutmeldingen meer, maaaarr, hij doet niks als je op de knop klikt...
Weet iemand hoe dit komt?
*/


Ik zocht ook naar dat manier van openen van browservenster via java.

Jammer dat hij niet de goede complete code heeft gepost.

Ik geloof dat met de knop die niks doet. ook de knopje.addActionlistener( new knopjeHandler()); moet hebben toch?

Verwijderd

EDIT
/me Woei, beetje oud topic niet? Niet beter om dan een nieuwe te openen?
/EDIT

De klasse van JavaWorld is een beetje beperkt en oud.
Beter is om webstart te gebruiken voor een java applicatie. Dan kun je gewoon de browser op vragen.
Doe je dat niet dan is die javaworld een mooi stukje code, maar er is een betere te vinden: http://geosoft.no/software/browser/Browser.java.html
Het is alleen een GNU Licentie, dus ik heb er persoonlijk nooit gebruik van gemaakt.

(was trouwens snel te vinden door in google even "Browser.java.html" in te tikken ;) [/zoek-tip])

[ Voor 91% gewijzigd door Verwijderd op 15-12-2004 13:53 ]


  • Silver7
  • Registratie: Januari 2002
  • Laatst online: 14-05 21:13
De klasse van JavaWorld is een beetje beperkt en oud.
Beter is om webstart te gebruiken voor een java applicatie. Dan kun je gewoon de browser op vragen.
Ik zal gaan zoeken naar de "webstart". Ik wil alleen maar de website openen. (buiten de java om)

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 18-05 23:23

.oisyn

Moderator Devschuur®

Demotivational Speaker

Hoi
Een topic van ruim 3 jaar oud omhoog schoppen is niet zo heel nuttig, zeker als die topic ook nog eens in het verkeerde forum staat (maar daar kun jij niets aan doen). Het is handiger als je gewoon een nieuwe opent, dat voorkomt verwarring :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.

Pagina: 1

Dit topic is gesloten.