HyperLinkListener取消并保存文件

时间:2014-04-07 17:16:41

标签: java html swing file-io jtextpane

的问题:

  • 当我将textpane的contentType保持为text/html时,输入的文本向右移动,例如:如果输入“asd”,则左侧出现'Message:','asd'出现在textpane的右侧

  • 添加文件时,如何为此编写HyperLinkListener。我在代码中注释了“保存”和“取消”。如何获取我点击的名称(取消或保存),以便我可以为这两个链接编写适当的代码?

代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;

public class SampleTextPane extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextPane textPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SampleTextPane frame = new SampleTextPane();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SampleTextPane() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout(0, 0));

        JPanel panel_1 = new JPanel();
        panel.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(new BorderLayout(0, 0));

        textPane = new JTextPane();
        textPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent arg0) {

                // Cancel
                // To cancel the progress

                // Save
                // To save the file
            }
        });
        textPane.setEditable(false);
        textPane.setContentType("text/html");
        panel_1.add(textPane);

        JPanel panel_2 = new JPanel();
        panel.add(panel_2, BorderLayout.SOUTH);
        panel_2.setLayout(new BorderLayout(0, 0));

        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent key) {
                if(key.getKeyCode()==KeyEvent.VK_ENTER){
                    if(!textField.getText().trim().isEmpty()){
                        sendMessage(textField.getText());
                        textField.setText("");
                    }
                }
            }
        });
        panel_2.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("+");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fileChoose = new JFileChooser();
                int returnVal = fileChoose.showOpenDialog(getParent());
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fileChoose.getSelectedFile();
                    String desc = file.getName().trim();
                    sendFileToUser(file, desc);
                }
            }
        });
        panel_2.add(btnNewButton, BorderLayout.EAST);
    }

    protected void sendFileToUser(File file, String desc) {
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, Color.BLACK);
        StyleConstants.setBold(set, true);
        StyleConstants.setAlignment(set, StyleConstants.ALIGN_LEFT);
        try {
            doc.insertString(doc.getLength(), " File : "+desc, set);
            HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
            HTMLDocument doc1 = (HTMLDocument)textPane.getDocument();
            try {
                editorKit.insertHTML(doc1, doc.getLength(), "<a href=\"\">Save</a>   <a href=\"\">Cancel</a>\n", 0, 0, null);
            } catch (BadLocationException | IOException e) {
                e.printStackTrace();
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

    }

    protected void sendMessage(String text) {
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, Color.BLACK);
        StyleConstants.setBold(set, true);
        StyleConstants.setAlignment(set, StyleConstants.ALIGN_LEFT);
        try {
            doc.insertString(doc.getLength(), " Message : ", set);
            doc.insertString(doc.getLength(), text+"\n", textPane.getStyle(StyleContext.DEFAULT_STYLE));
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

  

如何获取我点击的名称(取消或保存),

您是否指定了超链接的URL?

JEditorPane API显示了如何从HyperlinkEvent访问网址的示例。