[JAVA] "Hyperlink" navigatie in programma geeft problemen..

Pagina: 1
Acties:

  • Klaauw
  • Registratie: Februari 2002
  • Laatst online: 08-12-2025
Ik ben bezig met het schrijven van een programma waarin tekst in een JEditorPane wordt gezet.

In deze JEditorPane staan vragen met antwoorden (stap 1), deze antwoorden worden weergegeven als hyperlinks (stap 2). Als iemand op een antwoord klikt is het de bedoeling dat er in de JEditorPane een nieuwe tekst verschijnt (stap 3).

Nu is de laatste stap, van het verhaal, mijn probleem. Ik kan namelijk de hyperlinks creeëren en krijg in de "console" ook een goede output. Daarna probeer ik de inhoud van de JEditorPane met de .setText statement te veranderen maar gebeurd er niets zichtbaars, als ik namelijk in de console de inhoud van de pane vraag staat daar wel de nieuwe (en dus ook goede tekst). Weet iemand hoe ik dit kan oplossen. Ik hebrepaint(); en dergelijke opties al geprobeerd....

De code die erover gaat:
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
public void layout() 
{
    f = new Panel();
    invoervak = new JEditorPane(
        "text/html",
        "");
    invoervak.setEditable(false);
        StringBuffer sb = new StringBuffer();
    sb.append("<b>Vraag?</b><br>"); 
    sb.append("- <a href=\"v112\">v112</a><br>");
    sb.append("- <a href=\"v113\">v113</a><br>");
    invoervak.setText(sb.toString());
    invoervak.addHyperlinkListener(this);
    scrollPane = new JScrollPane(invoervak);
    f.add(scrollPane);
    f.setSize(500, 500);
    f.setVisible(true);
    setLayout(new BorderLayout());
    add("North", f);
    setSize(500, 500);
    setVisible(true);
}

public void hyperlinkUpdate(HyperlinkEvent e) 
{
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
            invoervak.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
        if (e.getEventType() == HyperlinkEvent.EventType.EXITED){
            invoervak.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            } 
         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          StringTokenizer st = new StringTokenizer(e.getDescription(), " ");
            if (st.hasMoreTokens()) {
                // String s = st.nextToken();
                       invoervak.setText("hoi");
                   String a = invoervak.getText();
                
            
                System.err.println(a);
            }
         }
}

  • martennis
  • Registratie: Juli 2005
  • Laatst online: 16-01 14:17
off-topic:
waarom roep je System.err aan in plaats van System.out?

kun je ipv repaint() ook eens updateUI doen? Sommige Swing componenten willen niet altijd updaten dmv repaint, maar wel met updateUI.

  • Stephan Oudmaijer
  • Registratie: Oktober 2000
  • Laatst online: 16-08-2023
In de documentatie staat wel wat hierover:

http://java.sun.com/j2se/...ax/swing/JEditorPane.html

In principe is de setText alleen te gebruiken voor initializatie.

There are several ways to load content into this component.

1. The setText method can be used to initialize the component from a string. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
2. The read method can be used to initialize the component from a Reader. Note that if the content type is HTML, relative references (e.g. for things like images) can't be resolved unless the <base> tag is used or the Base property on HTMLDocument is set. In this case the current EditorKit will be used, and the content type will be expected to be of this type.
3. The setPage method can be used to initialize the component from a URL. In this case, the content type will be determined from the URL, and the registered EditorKit for that content type will be set.

  • Klaauw
  • Registratie: Februari 2002
  • Laatst online: 08-12-2025
Thnx voor de informatie. Maar is er denk je een manier om het op een of andere manier toch te laten werken?

Zal verder vanmiddag de updateUI optie proberen.

offtopic:
Had idd system.out kunnen gebruiken om t iets netter aan te duiden. Maar t wilde maar niet werken dus dan ga je al gauw in errors denken ;)

  • martennis
  • Registratie: Juli 2005
  • Laatst online: 16-01 14:17
al gelukt?

  • Klaauw
  • Registratie: Februari 2002
  • Laatst online: 08-12-2025
Nee nog niet. Dus als iemand nog tips heeft.. Ze zijn welkom ;).
Pagina: 1