Button das JScrollPane aktualisiert

AngelDE98

Mitglied
Erstmal Hallo 🙂
Erstmal vorweg : Ich habe keine Umlaute auf der Tastatur , aber trotzdem etwas Kenntnis .
Ich bin nicht ganz neu und arbeite an einem kleinen Projekt . Das Problem :
Ich habe etwas ueber der Mitte eine Newspage . Unten ist eine Leiste mit Logo , Buttons und Textfeldern . Ein Button ist dafuer da um die Newspage zur Optionspage zu wechseln und umgekehrt . Der ActionListener fuer den Button ist :
Java:
this.optionsButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
    	  if (options == "news"){
    		  options = "options";
    		  newsactive = false;
  		      jpopne = OptionsPanel();
    		  optionsButton.setText("News");
    	  }else if (options == "options"){
    		  options = "news";
    		  newsactive = false;
  		      jpopne = getUpdateNews();
    		  optionsButton.setText("Options");
    	  }
      } } );
Nun etwas Erklaehrung :
- Options ist am Anfang "options" .
- Newsactive ist fur die Newspage wichtig da sie nach Button-Doppelklick verschwindet ( ??? ) .
- jpopne ist fur mich kurz wie JPanelOPtionsNEws . Also wird der JScrollPane , der beim Return von OptionsPanel() bzw. getUpdateNews() darin " gespeichert " ( anfangs ist es getUpdateNews() ) und spaeter dadurch angezeigt .

Nun liegt bestimmt das Problem darin , dass jpopne in ...
Java:
    JPanel p = new TransparentPanel(new BorderLayout());
    p.add(jpopne, "Center");
erstmal neu gezeichnet werden soll . Das Problem : Solange ich den Code aus der Funktion rausnehme und fuer ALLE Funktionen sichtbar mache ... es funktioniert nicht ???:L Stat dessen wird " NICHTS " angezeigt bzw. ( Ja , unter manchen Bedingungen ) sogar das ganze Programm " abkackt " .

Also : Wie mache ich es so , dass es nach dem Klick auf dem Button von News zu Options / von Options zu News wechselt ? Vielen Dank fuer Hilfe !

MfG Angel
 
Ok , Ich fug hinzu : Ich habe es geschafft den Code auserhalb zu haben ABER leider wird ...
Java:
  private JPanel ******************() {
    p.add(jpopne, "Center");

    JPanel southPanel = new TexturedPanel();
    southPanel.setLayout(new BorderLayout());
    southPanel.add(new LogoPanel(), "West");
    southPanel.add(new TransparentPanel(), "Center");
    southPanel.add(center(buildLoginPanel()), "East");
    southPanel.setPreferredSize(new Dimension(100, 100));

    p.add(southPanel, "South");
    return p;
  }
selbst von anderen Funktionen benutzt , die dann DAS zeichnen ...
Also , Ich brauch ehrlich Hilfe ...

MfG Angel
 
Eine Frage da ich Einsteiger im Forum bin : Soll ich Threads durch Neuigkeiten BUMPEN oder lieber den letzten , unbeantworteten Post editieren ?

Back-To-Topic :
Also , durch das
Code:
FRAMEZUMREFRESHEN.validate()
konnte ich den Frame jeweils nach dem Wechsel Validaten ... dachte ich . Es kam zum Options-Menu aber nicht zuruck zu den News . Es hat die News geladen aber Ich glaub ich muss dann nur noch den JEditorPane refreshen ... wie ?
 
Ich gebe zu, dass es schwer ist, Dein Problem zu verstehen. Man kann anhand der Code Fragemente nicht sehen, ob das Problem wirklich am Umschalten liegt oder schon in den verschiedenen Methoden wie OptionsPanel() oder getUpdateNews().

Also erstmal die Frage: Werden denn Deine Seiten VOR dem Versuch, umzuschalten, angezeigt? Wenn nein, dann gibt es wohl schon vorher ein Problem, da müsstest dann doch mehr Code zur Verfügung stellen.

Wenn Du Components nach dem Anzeigen tauschst, dann musst Du noch mal pack() aufrufen. Sonst siehst Du keine Änderung. Diesen Aufruf habe ich in Deinem Code nicht gesehen. Beispiel:

Code:
private void switchPanel() {
        if (options.equals("options")) {
            options = "news";
            panel = new Panel2();
            btn.setText("News");
        } else {
            options = "options";
            panel = new Panel1();
            btn.setText("Options");
        }
        pack();
    }
 
Also Danke aber der Fehler liegt glaub ich im getUpdateNews() . Die News werden leider VOR dem Versuch gezeichnet und ueberhaupt geladen . Hier der zensierte Code ( Also alles was nach @ kommt und der link durch example.com ersetzt ) :
Java:
  private JScrollPane getUpdateNews()
  {

	  if (options == "options") {
		  this.scrollPane = OptionsPanel();
		  return this.scrollPane;
	  }
	  if (options == "news") {
		  this.scrollPane = null;
	  }
	  if ((this.scrollPane != null) || (this.newsactive)) { 
		  this.scrollPane = new JScrollPane(editorPane);
		  //return this.scrollPane;
	  }
	  
      long l = 1L;

      editorPane.setText("Loading update news @ CENSORED");
      editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent he) {
          if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            try {
              Util.openLink(he.getURL().toURI());
            } catch (Exception e) {
              e.printStackTrace();
            }
        }
      });
      try {
        editorPane.setPage(new URL("http://example.com"));
      } catch (IOException e) {
        e.printStackTrace();
        editorPane.setText("Failed to update News @ CENSORED");
      }
      editorPane.setBackground(new Color(0x151515));
      editorPane.setForeground(Color.WHITE);
      editorPane.setEditable(false);
      this.scrollPane = new JScrollPane(editorPane);
      this.scrollPane.setBorder(null);
      editorPane.setMargin(null);

      this.scrollPane.setBorder(new MatteBorder(0, 0, 2, 0, new Color(0x151515)));

    newsactive = true;
    return this.scrollPane;
  }

Ich probiers nochmal mit pack() denn validate() funktionierte zurzeit perfekt .

Edit : Pack funktionierte nicht so . Ich musste
Code:
launcherFrame.
davor setzen , funktionierte aber gleich gut . Trotzdem Danke .
 
Zuletzt bearbeitet:
Ich bin erst morgen wieder da aber so wie es aussieht " skipt " getUpdateNews() den
Java:
try {
        editorPane.setPage(new URL("http://minecraftsl.tumblr.com"));
      } catch (IOException e) {
        e.printStackTrace();
        editorPane.setText("Failed to update News @ http://minecraftsl.tumblr.com");
      }
Codeteil . Wie ich das wieder hinkriege weiss ich nicht ... Also bin morgen wieder da .

MfG Angel
 
So ganz habe ich nicht verstanden, warum Du Deinen Code so aufbaust. Ob es so eine gute Idee ist, Swing Componts einfach auf null zu setzen, weiß ich nicht (Oben in Deinem Code). Dann fügst Du bei jedem Aufruf Deinem editorPane einen neuen HyperlinkListener hinzu, hmm...

Es hat die News geladen aber Ich glaub ich muss dann nur noch den JEditorPane refreshen ... wie ?

Folgender Hinweis aus einem anderen Forum

If the desired URL is the one currently being displayed, the document will not be reloaded. To force a document reload it is necessary to clear the stream description property of the document. The following code shows how this can be done:
Document doc = jEditorPane.getDocument(); doc.putProperty(Document.StreamDescriptionProperty, null);

Ich habe Deinen Fall einmal so umgesetzt: Zwei Panel, ein Panel für die Options und das andere enthält den JEditorPane für eine Webseite. Beim klicken tausche ich die beiden Panels einfach aus:

Java:
private void switchPanel() {
        if (options.equals("news")) {
            options = "options";
            this.getContentPane().remove(panel1);
            this.getContentPane().add(panel2, BorderLayout.CENTER);
            btn.setText("Options");
        } else {
            options = "news";
            try {
                panel1.reset();
                panel1.getEditorPane().setPage("http://ubuntuforums.org/forumdisplay.php?f=11");
            } catch (IOException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.getContentPane().remove(panel2);
            this.getContentPane().add(panel1, BorderLayout.CENTER);
            btn.setText("News");
        }
        this.validate();
        this.repaint();
    }

Um eine Neuladen der Seite bei jedem Aufruf zu erzwingen, habe ich eine die reset() Methode dazu gepackt:

Java:
public void reset(){
        Document doc = jEditorPane1.getDocument(); 
        doc.putProperty(Document.StreamDescriptionProperty, null);
    }

panel1 und pane2 sind fertige Objekte, da brauch ich nicht groß neu zu erstellen. Klappt so, wie es soll.
 
Vielen Dank , ich probier es gleich aus ( gerad von der Schule gekommen ) . Ich muss manche Sachen umschreiben da this kein JFrame / JPanel ist ( launcherFrame ist der Main-Frame ) . Also : Ich probiere dene RESET - Methode .
 
Ok , vielen Dank ! Ich wusste das mit dem
Code:
editorPane.getDocument().putProperty(docjep.StreamDescriptionProperty, null);
nicht . Das gleiche mit dem repaint . Also meine Codeparts :

-- BUTTON -- ACTIONLISTENER --
Java:
this.optionsButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
    	  if (options == "news"){
    		  options = "options";
    		  newsactive = false;
    		  p.remove(jpopne);
  		      jpopne = OptionsPanel();
  		      p.add(jpopne, "Center");
  		      launcherFrame.validate();
    		  optionsButton.setText("News");
    	  }else if (options == "options"){
    		  options = "news";
    		  newsactive = false;
    		  p.remove(jpopne);
  		      jpopne = getUpdateNews();
  		      p.add(jpopne, "Center");
  		      docjep = editorPane.getDocument(); 
  		      docjep.putProperty(Document.StreamDescriptionProperty, null);
  		      launcherFrame.validate();
    		  optionsButton.setText("Options");
    	  }
      } } );

-- getUpdateNews() -- EDITABLE EDITION --
Java:
  private JScrollPane getUpdateNews()
  {

	  if (options == "options") {
		  this.scrollPane = OptionsPanel();
		  return this.scrollPane;
	  }
	  if ((options == "news") && (!this.newsactive)) {
		  this.scrollPane = null;
		  editorPane.getDocument().putProperty(docjep.StreamDescriptionProperty, null);
	  }
	  if ((this.scrollPane != null) || (this.newsactive)) { 
		  return this.scrollPane;
	  }
	  
      long l = 1L;

      editorPane.setText("Loading update news @ http://www.example.com");
      editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent he) {
          if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            try {
              Util.openLink(he.getURL().toURI());
            } catch (Exception e) {
              e.printStackTrace();
            }
        }
      });
      try {
        editorPane.setPage(new URL("http://www.example.com"));
      } catch (IOException e) {
        e.printStackTrace();
        editorPane.setText("Failed to update News @ http://www.example.com");
      }
      editorPane.repaint();
      editorPane.setBackground(new Color(0x151515));
      editorPane.setForeground(Color.WHITE);
      editorPane.setEditable(false);
      this.scrollPane = new JScrollPane(editorPane);
      this.scrollPane.setBorder(null);
      editorPane.setMargin(null);

      this.scrollPane.setBorder(new MatteBorder(0, 0, 2, 0, new Color(0x151515)));

    newsactive = true;
    return this.scrollPane;
  }

Und ChrisKu , Genau das war das Problem : 2 Panels und die wollte ich per Klick austauschen . Vieelen Dank . Soll ich dich irgendwie in die Credits einfugen ? Und ich lasse den Topic fur weitere Fragen anderer Benutzer offen .

MfG Angel

Edit : Wieso ich den scrollPane auf null gesetzt habe ? Es setzt sich auf null wenn es von OPTIONEN zu NEWS uebergeht damit es das alte " gecleant " hat was am Anfang noetig war .
 
Zuletzt bearbeitet:
Danke . In meinem Post oben habe ich als Edit nochma reingeschrieben wieso ich scrollPane auf null gestellt habe . Wenn ich den ganzen Sourcecode reinstellen wuerde wuerdet ihr Ausrasten und mir nicht helfen oder wenn ich sogar wenigstens den Projektnamen nenne werdet ihr mich dann bannen >.< >.< >.< >.< Egal ...

Falls du noch etwas ueber den Aufbau des Codes / Vorschlaege hast hau rein :toll:

MfG Angel
 
Ich bin zuruck mit einer NullPointerException -.-
Java:
editorPane.getDocument().putProperty(editorPane.getDocument().StreamDescriptionProperty, null);
gibt nonstop eine ab , ich habe endlich bemerkt das es DAS ist . Ich habe die null zu
Code:
new JScrollPane()
gewechselt aber immer das gleiche ... kann man statt die null nicht was anderes nehmen ( wenn ich was anderes probiere kackt bei mir mein Projekt ab -.- ) ? Es ist auch etwas unnotiges ... Also einen error zu printen meine ich ... es verlangsamt es nur etwas und viele beklagen sich schon wegen Geschwindigkeit und wechseln zu alternativen ...
Ich weis das die Seite erst geladen und dann gerendert werden muss aber kann man das trotzdem nicht machen ? Nur das ? Das ist der einzige Error -.-

Und ja , es hat noch was mit dem Thread zu tun und ich habe nichts bemerkt / nicht geantwortet da ich nicht genau darauf geachtet habe und ich noch in Polen war ( ohne iNet ) ...

Vielen Dank
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben