首页 > 解决方案 > 在jTable中添加指向单个sql值的超链接

问题描述

我在 mysql 的帮助下在 eclipse 中实现了一个搜索界面。我的搜索界面产生了我想要的结果,但我希望能够点击“video_url”列并显示一个超链接。有没有办法为单列做到这一点?现在 Jtable 是“可编辑的”,因此用户可以单击它,但不会对其进行任何更改。该链接可以复制然后粘贴,但我真的想从界面打开链接。

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

    /**
     * Create the application.
     */
    public VIdeo_search_test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 17));
        frame.setBounds(400, 400, 1050, 1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblVideoSearch = new JLabel("Video Search");
        lblVideoSearch.setFont(new Font("Tahoma", Font.PLAIN, 19));
        lblVideoSearch.setBounds(241, 11, 230, 27);
        frame.getContentPane().add(lblVideoSearch);

        JButton btnSearchCity = new JButton("Search City");
        btnSearchCity.setBounds(216, 80, 165, 25);
        btnSearchCity.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                DefaultTableModel model = new DefaultTableModel(new String[]{"video_url", "video name", "video description", "video_city", "video_subject", "video_tags", "reviewed_by", "star"}, 0);


try {

                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");

                    Statement stmt= con.createStatement();
                    String sql = "Select * from video where video_city = '" +txtCity.getText()+"'";
                    ResultSet rs=stmt.executeQuery(sql);
                    while(rs.next())
                    {
                         String a = rs.getString("video_url");
                         String d = rs.getString("video_name");
                         String e = rs.getString("video_description");
                         String f = rs.getString("video_city");
                         String g = rs.getString("video_subject");
                         String h = rs.getString("video_tags");
                         String k = rs.getString("reviewed_by");
                         String i = rs.getString("star");
                         model.addRow(new Object[]{a, d, e, f, g, h, k, i});

                            table.setModel(model);

                              }
                                {

                               con.close();}
                    } catch(Exception e) {System.out.print (e);}

            }
        });
        frame.getContentPane().add(btnSearchCity);

        JButton btnSearchTag = new JButton("Search Tag");
        btnSearchTag.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                DefaultTableModel model = new DefaultTableModel(new String[]{"video_url", "video name", "video description", "video_city", "video_subject", "video_tags", "reviewed_by", "star"}, 0);


                try {

                                    Class.forName("com.mysql.cj.jdbc.Driver");
                                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");

                                    Statement stmt= con.createStatement();
                                    String sql = "Select * from video where video_tags LIKE '"+txtTag.getText()+"%'";
                                    ResultSet rs=stmt.executeQuery(sql);
                                    while(rs.next())
                                    {
                                                String a = rs.getString("video_url");
                                                String d = rs.getString("video_name");
                                                String e = rs.getString("video_description");
                                                String f = rs.getString("video_city");
                                                String g = rs.getString("video_subject");
                                                String h = rs.getString("video_tags");
                                                String k = rs.getString("reviewed_by");
                                                String i = rs.getString("star");
                                                model.addRow(new Object[]{a, d, e, f, g, h, k, i});

                                            table_1.setModel(model);

                                              }
                                    {                                       


                                               con.close();}
                                    } catch(Exception e) {System.out.print (e);}

                            }



        });
        btnSearchTag.setBounds(216, 303, 165, 25);
        frame.getContentPane().add(btnSearchTag);

        txtCity = new JTextField();
        txtCity.setBounds(216, 49, 165, 20);
        frame.getContentPane().add(txtCity);
        txtCity.setColumns(10);

        table = new JTable();
        table.setBounds(10, 116, 867, 135);
        frame.getContentPane().add(table);

        txtTag = new JTextField();
        txtTag.setColumns(10);
        txtTag.setBounds(216, 272, 165, 20);
        frame.getContentPane().add(txtTag);

        table_1 = new JTable();
        table_1.setBounds(10, 341, 601, 135);
        frame.getContentPane().add(table_1);

        JButton btnViewVideo = new JButton("View Video");
        btnViewVideo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                registered_video_interface info = new registered_video_interface();
                registered_video_interface.main(null); }


        });
        btnViewVideo.setBounds(251, 509, 89, 23);
        frame.getContentPane().add(btnViewVideo);
    }
}

标签: javasqlswingjdbc

解决方案


推荐阅读