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[].
HTML
file each time a new image is selected from the JList?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()]);
}
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:
createEditorList()
& createEditorPane(String file)
became makeAEditorPane()
, makeAEditorList()
& feedEditor(String name)
. Simply by splitting up the components editorScrollPane
& editorPane
during formation thanks to, @AJNeufeld.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.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.