G-Code
G-Code

Reputation: 11

Display Options for Text Descriptions - Java Swing

Okay, I have this program I am writing and would love some oversight please.


This program of sorts, uses a JList and JListSelectionListener to output a number of images onto, 1/2 vertically-split JPanes. Of these JPanes, as mentioned beforehand; one displays the images and in the bottom JPanel, a JEditorPane reads text. For the JEditorPane, I have an HTML doc. styled and being read from. This in theory, is my description of each image. Except, I cannot redirect the JEditorPane as too, read from aforementioned HTML files whose URLs or paths are accessed via a String Array[].

Main Points/Questions (tldr;)


  1. How do I have this JEditorPane read from a different HTML file each time a new image is selected from the JList?
  2. Should I be using a JTextPane or something else instead? Only, too my knowledge, styling might be in-or-out of the question. So, what am I doing wrong or differently and should be?

Code


private String[] fileName = { "htmlDoc1", "htmlDoc2", "htmlDoc3", "htmlDoc4" };

protected JScrollPane createEditorList() {
    JEditorPane editorPane = createEditorPane(fileName[list.getSelectedIndex()]);
    JScrollPane editorScrollPane = new JScrollPane(editorPane);
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    editorScrollPane.setPreferredSize(new Dimension(250, 145));
    editorScrollPane.setMinimumSize(new Dimension(10, 10));
    return editorScrollPane;
}

private JEditorPane createEditorPane(String file) {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);
    java.net.URL helpURL = Bobbleheads.class.getResource("/images/bobbleheads/" + file + ".html");
    if (helpURL != null) {
        try {
            editorPane.setPage(helpURL);
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + helpURL);
        }
    } else {
        System.err.println("Couldn't find file: " + fileName);
    }

    return editorPane;
}

public void valueChanged(ListSelectionEvent e) {
    JList<?> list = (JList<?>) e.getSource();
    updateLabel(imageNames[list.getSelectedIndex()]);
    createEditorPane(fileName[list.getSelectedIndex()]);
}

Thank you too everyone, contributing any way possible!

Upvotes: 0

Views: 445

Answers (1)

G-Code
G-Code

Reputation: 11

Crunching in a few methods solved this Java singleton. With the outstanding help from AJNeufeld, Andrew Thompson & Stack Overflow of course!

Heres how:


Original methods for change:

protected JScrollPane createEditorList() {
private JEditorPane createEditorPane(String file) {

and the public void valueChanged(ListSelectionEvent e) { method were replaced with and replicated into these newer code blocks.

Code:


New, rescripted methods:

protected JScrollPane makeAEditorPane() {
    JScrollPane editorScrollPane = new JScrollPane(makeAEditorList());
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    editorScrollPane.setPreferredSize(new Dimension(250, 145));
    editorScrollPane.setMinimumSize(new Dimension(10, 10));

    return editorScrollPane;
}

protected JEditorPane makeAEditorList() {
    editorPane = new JEditorPane();
    editorPane.setEditable(false);

    return editorPane;
}

private void feedEditor(String name) {
    URL helpURL = Bobbleheads.class.getResource("/images/bobbleheads/" + name + ".html");

    if (helpURL != null) {
        try {
            editorPane.setPage(helpURL);
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + helpURL);
        }
    } else {
        System.err.println("Couldn't find file: TextSampleDemoHelp.html");
    }
}

public void valueChanged(ListSelectionEvent e) {
    JList<?> list = (JList<?>) e.getSource();
    updateLabel(imageNames[list.getSelectedIndex()]);
    feedEditor(htmlDocs[list.getSelectedIndex()]);

}

Understanding what has changed:


  1. Of the two original methods, three new methods were deduced. createEditorList() & createEditorPane(String file) became makeAEditorPane(), makeAEditorList() & feedEditor(String name). Simply by splitting up the components editorScrollPane & editorPane during formation thanks to, @AJNeufeld.
  2. Very next, the new method feedEditor(String name), did exactly what its name implies. Feeds the initiation of our URL here, allocating a HTML file for the editorPane to derive and load thanks to, @Andrew Thompson.
  3. Accessing the method: feedEditor(htmlDocs[list.getSelectedIndex()]);, inside of the valueChanged(ListSelectionEvent e) method, paved the way for an event to occur. An event between a JList known as list.

Wrapping things up...


Now, the event brought a change in the URL discussed on earlier. Here, it is relatively simply put; allowing for switching between JList selections, activates HTML files located as project resources.

That's it! Thank you everybody and stay safe.

Upvotes: 0

Related Questions