how to implement.(About JTable and the JEditorPane)

Status
Nicht offen für weitere Antworten.

iskohl

Mitglied
Ok.I have a few HTML files in my local HD.And I bulid a JTable and a JEditorPane within a JSplittPane.The JTable can display the index of the HTML file such as "File 1,File 2,.....File n".To implement is that I choose a index in JTable,the corresponding HTML can display in the JTEditorPane.How should I write codes?

Thanks a lot.
 
Code:
/*
 * HTML_Display.java
 */
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.table.*;
public class HTML_Display extends JFrame {
    public HTML_Display() {
        super("HTML_Display");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        
        //JComponents erzeugen:
        label = new JLabel("        Klick auf einen Index Eintrag um die HTML Datei anzuzeigen.");
        splitPane = new JSplitPane();
        scrollPane1 = new JScrollPane();
        table = new JTable();
        scrollPane2 = new JScrollPane();
        editorpane = new JEditorPane();
        
        //Table model:
        tableModel = new DefaultTableModel(
                new Object [][] {
                    {"http://www.tactika.com/realhome/contents.html"},
                    {"file:c:\\htmltext.html"},
                    {null},
                    {null}
        },
                new String [] {"Index"}
        ) {
            Class[] types = new Class [] {String.class};
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        };
        
        //Layout:
        getContentPane().add(label, BorderLayout.NORTH);
        splitPane.setDividerLocation(200);
        table.setModel(tableModel);
        scrollPane1.setViewportView(table);
        splitPane.setLeftComponent(scrollPane1);
        scrollPane2.setViewportView(editorpane);
        splitPane.setRightComponent(scrollPane2);
        getContentPane().add(splitPane, BorderLayout.CENTER);
        
        //Listener:
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
    }
    private void tableMouseClicked(MouseEvent evt) {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        String urlValue = (String) table.getValueAt(table.getSelectedRow(), 
                table.getSelectedColumn());
        try {
            URL url = new URL(urlValue);
            editorpane.setPage(url);
        } catch (Exception e) {}
        setCursor(Cursor.getDefaultCursor());
    }
    public static void main(String args[]) {new HTML_Display().setVisible(true);}
    private JEditorPane editorpane;
    private JLabel label;
    private JScrollPane scrollPane1, scrollPane2;
    private JSplitPane splitPane;
    private JTable table;
    private DefaultTableModel tableModel;
}
 
Campino hat gesagt.:
hi iskohl,
you are in a german forum here so you will get no answer by asking in english.

Just in case another English guy is reading this... of course we do also give answers in English, but it may need some more time... but it will most probably be shorter as if you asked in Sun's forums 😉

(I don't want to say anything against Sun's forums, but I experienced that they can need several days or weeks until they answer)
 
Illuvatar hat gesagt.:
Code:
/*
 * HTML_Display.java
 */
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.table.*;
public class HTML_Display extends JFrame {
    public HTML_Display() {
        super("HTML_Display");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        
....


Vielen Dank,die funktioniert.

Andre_Uhres hat gesagt.:
but it will most probably be shorter as if you asked in Sun's forums

schon gefragt,bis jetzt keine Antworten.Zu leicht oder?

Andre_Uhres hat gesagt.:
dann frag auch in deutsch wenn du deutsch kannst
das ist ja auch ein deutsches forum

bin faul,habe direkt von Sun BBS kopiert.Naechstes Mal frage ich auf deutsch.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben