简体   繁体   English

从数据库中获取值时,Jtable中的按钮未显示

[英]buttons not getting displayed in Jtable when values are fetched from database

So basically the requirement is to fetch results from database based on the search query. 因此,基本上的要求是基于搜索查询从数据库中获取结果。 Results are displayed in JTable. 结果显示在JTable中。 The results should have 5 columns. 结果应该有5列。 The 4 columns contain values fetched from database. 4列包含从数据库中获取的值。 The 5th column is supposed to have buttons . 第五栏应该有按钮。 When the button is clicked a popup should appear. 单击该按钮后,将出现一个弹出窗口。 Now the search functionality is working fine. 现在搜索功能运行良好。 I'm getting an error when I need to display a buttons in 5th column. 需要在第5列中显示按钮时出现错误。 I googled and found out that it can be done by TablCellRenderer. 我搜索了一下,发现可以通过TablCellRenderer完成。 I've also incorporated the code for the same in my code referring this link Now, I get the error as 我还通过引用此链接将相同的代码合并到了我的代码中。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at r_search_2$JTableButtonRenderer.getTableCellRendererComponent(r_search_2.java:193)

which lies in this block:- 在此块中:

             else
            {
                button.setForeground(table.getForeground());
                button.setBackground(UIManager.getColor("Button.background"));
            }

If I remove this block from my code the search query runs fine but buttons are not displayed in the 5th column. 如果我从代码中删除了该块,则搜索查询运行良好,但按钮未显示在第5列中。 So where lies the error and how should I correct that ? 那么错误在哪里,我该如何纠正? Thanks ! 谢谢 !

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;


public class r_search_2 extends JFrame implements ActionListener
{

    JFrame frame1;
    JLabel l0, l1, l2;
    JComboBox c1;
    JButton b1;
    Connection con;
    ResultSet rs, rs1;
    Statement st, st1;
    PreparedStatement pst;
    String ids;
    static JTable table  = new JTable(new JTableModel());
   // static JTable table  = new JTable();
    String[] columnNames = {"SECTION NAME", "REPORT NAME", "CONTACT", "LINK","METRICS"};
    String from;
    Vector v = new Vector();
    JMenuBar menu = new JMenuBar();
    JPanel mainPanel = new JPanel(new BorderLayout());
    JPanel topPanel = new JPanel(new FlowLayout(SwingConstants.LEADING, 60,25));
    JScrollPane scroll = new JScrollPane(table);

    r_search_2() 
    {

        l1 = new JLabel("Search");
        b1 = new JButton("submit");
        l1.setBounds(75, 110, 75, 20);
        b1.setBounds(150, 150, 150, 20);
        b1.addActionListener(this);

        topPanel.add(l1,BorderLayout.LINE_START);

        try 
        {

            File dbFile = new File("executive_db.accdb");
                String path = dbFile.getAbsolutePath();
                con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ= " + path);
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            st = con.createStatement();
            rs = st.executeQuery("select index_name from Index1");
           while (rs.next())
           {
                ids = rs.getString(1);
                v.add(ids);

            }
            c1 = new JComboBox(v);
            c1.setEditable(true);c1.setSelectedItem("");
            c1.setBounds(150, 110, 150, 20);
            topPanel.add(c1,BorderLayout.CENTER);
            topPanel.add(b1,BorderLayout.LINE_END);
            mainPanel.add(topPanel, BorderLayout.PAGE_START);

            st.close();
            rs.close();
        } 
        catch (Exception e)
        {
        }
       // setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) 
    {
        if (ae.getSource() == b1)
        {
            showTableData();
        }
     }

    public void showTableData()
    {

        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(columnNames);

        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setFillsViewportHeight(true);

        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        from = (String) c1.getSelectedItem();


        TableCellRenderer buttonRenderer = new JTableButtonRenderer();
        table.getColumn("METRICS").setCellRenderer(buttonRenderer);
        //System.out.println(table.getColumn("METRICS"));
        //table.getColumn("Button2").setCellRenderer(buttonRenderer);
        table.addMouseListener(new JTableButtonMouseListener(table));

        String section_name = "";
        String report_name = "";
        String contact_name = "";
        String link = "";


        try
        {

        pst = con.prepareStatement("select distinct Section.Section_Name,Report.Report_Name,Report.Link,Contact.Contact_Name "
                                        + "FROM (( Section INNER JOIN Report ON Report.Section_ID=Section.Section_ID ) INNER JOIN Contact ON Contact.Contact_ID=Report.Contact_ID )  LEFT JOIN Metrics ON Metrics.Report_ID=Report.Report_ID  "
                                                                + " WHERE Section.Section_Name LIKE '%"+from+"%' OR Report.Report_Name LIKE '%"+from+"%' OR Metrics.Metric_Name LIKE '%"+from+"%' OR Contact.Contact_Name LIKE '%"+from+"%' ");
            ResultSet rs = pst.executeQuery();
            int i = 0;
            while (rs.next()) {
                section_name = rs.getString("Section_Name");
                report_name = rs.getString("Report_Name");
                contact_name = rs.getString("Contact_Name");
                link = rs.getString("Link");
               // String m="apple";

                model.addRow(new Object[]{section_name, report_name, contact_name, link});

                i++;
            }

            if (i < 1)
            {
                JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
            }
            if (i == 1)
            {
                System.out.println(i + " Record Found");
            } else {
                System.out.println(i + " Records Found");
            }
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        mainPanel.add(scroll);

        mainPanel.revalidate();
        mainPanel.repaint();

    }
    private static class JTableButtonRenderer implements TableCellRenderer
    {       
        @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JButton button = (JButton)value;
            if (isSelected) {
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } 
            else
            {
                button.setForeground(table.getForeground());
                button.setBackground(UIManager.getColor("Button.background"));
            }
            return button;  
        }
    }

    private static class JTableButtonMouseListener extends MouseAdapter
    {
        private final JTable table;

        public JTableButtonMouseListener(JTable table)
        {
            this.table = table;
        }

        public void mouseClicked(MouseEvent e)
        {
            int column = table.getColumnModel().getColumnIndexAtX(e.getX());
            int row    = e.getY()/table.getRowHeight(); 

            if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0)
            {
                Object value = table.getValueAt(row, column);
                if (value instanceof JButton) 
                {
                    ((JButton)value).doClick();
                }
            }
        }
    }

    public static class JTableModel extends AbstractTableModel 
    {

        private static final long serialVersionUID = 1L;
        //private static final String[] COLUMN_NAMES = new String[] {"Id", "Stuff", "METRICS"};
          String[] columnNames = {"SECTION NAME", "REPORT NAME", "CONTACT", "LINK","METRICS"};
        private static final Class<?>[] COLUMN_TYPES = new Class<?>[] {String.class, String.class,String.class, String.class,  JButton.class};

        @Override public int getColumnCount()
        {
            return columnNames.length;
        }

        @Override public int getRowCount()
        {
            return 4;
        }

        @Override public String getColumnName(int columnIndex)
        {
            return columnNames[columnIndex];
        }

        @Override public Class<?> getColumnClass(int columnIndex)
        {
            return COLUMN_TYPES[columnIndex];
        }

        @Override public Object getValueAt(final int rowIndex, final int columnIndex)
        {
            switch (columnIndex) {
                case 0: //return rowIndex;
                case 1: //return "Text for "+rowIndex;
                case 2: // fall through
                case 3: final JButton button = new JButton(columnNames[columnIndex]);
            //  button.setPreferredSize(new Dimension(100, 100));
                button.setSize(new Dimension(10,10));
                        button.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent arg0) {
                                JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button), 
                                        "Button clicked for row "+rowIndex);
                            }
                        });
                        return button;
                default: return "";
            }
        }   
    }


    public static void main(String args[])
    {


                r_search_2 s=new r_search_2();
                //new r_search_2();
                JFrame fr=new JFrame("Search Executive Reports");
                //fr.add(s.getUI());
                fr.add(s.mainPanel);
                fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                fr.setLocationByPlatform(true);
                fr.setSize(1000, 400);
                //fr.pack();
                fr.setVisible(true);

      //  new r_search_2();
    }
}

Don't use a MouseListener for this; 请勿为此使用MouseListener use a TableCellEditor , illustrated here . 使用TableCellEditor ,说明这里 Conveniently, DefaultCellEditor can delegate to a JComboBox as a popup component. 方便地, DefaultCellEditor可以委托给JComboBox作为弹出组件。

There is something is wrong with below code in JTableButtonRenderer JTableButtonRenderer中的以下代码有问题

JButton button = (JButton)value; JButton按钮=(JButton)值;

Most probably "value" is null here. 最有可能“值”在此处为空。 For testing create button as JButton button = new JButton("Button"); 为了进行测试,将按钮创建为JButton button = new JButton(“ Button”);

Let me know if that works. 让我知道是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM