Java 当弹出窗口应该只显示一次时,它会连续出现

Java 当弹出窗口应该只显示一次时,它会连续出现,java,swing,Java,Swing,首先,为问题的标题道歉。我不知道该写什么。因此,有关该问题的详细说明如下:- 因此,下面的代码片段运行非常好,只需一个故障。这段代码实际上根据搜索功能从数据库中获取相关结果,并显示在JTable中。在Jtable中的5列中,有4列从数据库检索它们的值,第5列包含Jbuttons。现在,当用户单击Jbutton,然后根据该特定行的节名的值,将获取一个弹出窗口。所有这些都运行良好。问题是弹出窗口最好只显示一次。但根据我的程序,弹出窗口连续出现的次数取决于用户搜索查询的次数。例如,在搜索3个术语后,用

首先,为问题的标题道歉。我不知道该写什么。因此,有关该问题的详细说明如下:-

因此,下面的代码片段运行非常好,只需一个故障。这段代码实际上根据搜索功能从数据库中获取相关结果,并显示在JTable中。在Jtable中的5列中,有4列从数据库检索它们的值,第5列包含Jbuttons。现在,当用户单击Jbutton,然后根据该特定行的节名的值,将获取一个弹出窗口。所有这些都运行良好。问题是弹出窗口最好只显示一次。但根据我的程序,弹出窗口连续出现的次数取决于用户搜索查询的次数。例如,在搜索3个术语后,用户搜索第4个搜索查询并按下按钮,条件也满足生成弹出窗口的条件,然后该弹出窗口连续显示4次。如何解决这个问题

public class r_search_2 extends JFrame implements ActionListener
{

     static int counter;
    static String sname="";
      DefaultTableModel model;
    //String section_name = "";

    JFrame frame1;
    JLabel l0, l1, l2;
    JComboBox c1;
    JButton b1;
    Connection con;
    ResultSet rs, rs1;
    Statement st, st1;
    PreparedStatement pst;
    String ids;
    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");

        //l0.setBounds(100, 50, 350, 40);
        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()
    {
        // int counter=0;

         table.setRowHeight(35);

         JTableHeader th = table.getTableHeader();
         th.setPreferredSize(new Dimension(30, 30));
         th.setFont(th.getFont().deriveFont(Font.BOLD));
         th.setForeground( Color.BLACK); 
         th.setBackground(Color.LIGHT_GRAY);

       // DefaultTableModel model = new 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();
        if(from.equals(""))
        {
              JOptionPane.showMessageDialog(null, "Please enter a search term", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else
        {

        TableCellRenderer buttonRenderer = new JTableButtonRenderer();
        table.getColumn("METRICS").setCellRenderer(buttonRenderer);

        table.addMouseListener(new JTableButtonMouseListener(table));
        //static int i=0;
         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");
              //  data_values(section_name,report_name,contact_name,link);


                model.addRow(new Object[]{section_name, report_name, contact_name, link});
                System.out.println("section name at row"+section_name);
               /* s_name(String section_name)
                {

                }*/
                sname=section_name;
              //  getsecname(section_name);
                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;
            JButton button = new JButton("LIST OF METRICS");
            button.setBackground(Color.YELLOW);
            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) {
            counter=0;
            System.out.println("***************************************************************");
            System.out.println("counter value="+counter++);
            System.out.println("/////////////////////////////////////////////////////////////////////");
            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);
                System.out.println("row clicked="+row);
                System.out.println("column clicked="+column);
                System.out.println("object value="+value);
              /* public void getsecname(String s)
                {
                    String ss=s;
                }*/

                if(value==null)
                {
                    Object v=table.getValueAt(row, 0);
                    System.out.println("--------------------------------------------");
                    System.out.println("object value="+v);
                     String s = v.toString();
                     if(s.equals("Executive Summary"))
                     {
                         JOptionPane.showMessageDialog(null, "list of metrics", "Error", JOptionPane.ERROR_MESSAGE);
                     }
                }
                if (value instanceof JButton) {
                    ((JButton)value).doClick();
                }
            }
        }
    }
     public  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  final Class<?>[] COLUMN_TYPES = new Class<?>[] {String.class, String.class,String.class, String.class,  JButton.class};

                    @Override public int getColumnCount()
                    {
                                  //  System.out.println("inside getcolumncount");
                                   // System.out.println(columnNames.length);
                                    return columnNames.length;

                    }

                    @Override public int getRowCount()
                    {
                                   // System.out.println("inside getrowcount");
                                    return 100;
                    }

                    @Override public String getColumnName(int columnIndex)
                    {
                                   // System.out.println("inside getColumnName");
                                    //System.out.println("column index="+columnIndex);
                                    //System.out.println(columnNames[columnIndex]);
                                   // System.out.println("section name="+section_name);
                                    return columnNames[columnIndex];
        }

                    @Override public Class<?> getColumnClass(int columnIndex)
                    {
                                  //  System.out.println("inside columnIndex");
                                    //System.out.println(COLUMN_TYPES[columnIndex]);

                                    return COLUMN_TYPES[columnIndex];
                    }

                    @Override public Object getValueAt(final int rowIndex, final int columnIndex)
                    {
                                    System.out.println("inside getValueAt");
                                    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));
                                                    //System.out.println("halloween");
                                                                                    button.addActionListener(new ActionListener() {
                                                                                                    public void actionPerformed(ActionEvent arg0) {
                                                                                                                    JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button), 
                                                                                                                                                    "Button clicked for row "+rowIndex);
                                                                                                    }
                                                                                    });
                                                                                    return button;
                                                    default: return "";
                                    }
                    }              
    }

   /* public String getRowData(int row) {
        if (model != null) {
           return section_name;
            // return row of data from model here 
        } 
        else
        {
           // throw some exception
        }
        return contact_name;
     }*/

    public static void main(String args[])
    {

        /*try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {

                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        } */       

                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();
    }
}
公共类r\u search\u 2扩展JFrame实现ActionListener
{
静态整数计数器;
静态字符串sname=“”;
默认表格模型;
//字符串节_name=“”;
jframe1;
JLabel l0,l1,l2;
JComboBox c1;
按钮b1;
连接con;
结果集rs,rs1;
报表st、st1;
编制报表pst;
字符串ID;
JTable table=新的JTable(新的JTableModel());
//静态JTable表=新JTable();
String[]columnNames={“节名”、“报告名”、“联系人”、“链接”、“指标”};
串从;
向量v=新向量();
JMenuBar菜单=新建JMenuBar();
JPanel mainPanel=newjpanel(newborderlayout());
JPanel topPanel=新JPanel(新流程布局(SwingConstants.LEADING,60,25));
JScrollPane scroll=新的JScrollPane(表);
r_search_2()
{
l1=新的JLabel(“搜索”);
b1=新按钮(“提交”);
//l0.立根(100,50,350,40);
l1.立根(75、110、75、20);
b1.立根(150、150、150、20);
b1.添加ActionListener(本);
topPanel.add(l1,边界布局。行_开始);
尝试
{
File dbFile=新文件(“executive_db.accdb”);
字符串路径=dbFile.getAbsolutePath();
con=DriverManager.getConnection(“jdbc:odbc:Driver={Microsoft Access驱动程序(*.mdb,*.accdb)};DBQ=“+path”);
forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
st=con.createStatement();
rs=st.executeQuery(“从Index1中选择索引名称”);
while(rs.next())
{
ids=rs.getString(1);
v、 添加(ID);
}
c1=新的JComboBox(v);
c1.setEditable(真);c1.setSelectedItem(“”);
c1.立根(150、110、150、20);
topPanel.add(c1,BorderLayout.CENTER);
topPanel.添加(b1,边界布局。线_末端);
主面板。添加(topPanel,BorderLayout.PAGE_START);
圣克洛斯();
rs.close();
} 
捕获(例外e)
{
}
//setVisible(真);
}
已执行的公共无效行动(行动事件ae)
{
如果(ae.getSource()==b1)
{
showTableData();
}
}
public void showTableData()
{
//int计数器=0;
表3.设置行高(35);
JTableHeader th=table.getTableHeader();
设置首选尺寸(新尺寸(30,30));
th.setFont(th.getFont().deriveFont(Font.BOLD));
设置前景(颜色为黑色);
挫折背景(颜色:浅灰色);
//DefaultTableModel=新的DefaultTableModel();
模型=新的DefaultTableModel();
model.setColumnIdentifiers(columnNames);
表2.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
表.setFillsViewPerthweight(真);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar,根据需要);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u根据需要);
from=(字符串)c1.getSelectedItem();
if(from.equals(“”)
{
JOptionPane.showMessageDialog(null,“请输入搜索词”,“错误”,JOptionPane.Error\u消息);
}
其他的
{
TableCellRenderer buttonRenderer=新的JTableButtonRenderer();
表.getColumn(“度量”).setCellRenderer(按钮渲染器);
table.addMouseListener(新的JTableButtonMouseListener(table));
//静态int i=0;
字符串节_name=“”;
字符串报告_name=“”;
字符串contact_name=“”;
字符串链接=”;
尝试
{
pst=con.prepareStatement(“选择不同的节。节名称,报告。报告名称,报告。链接,联系人。联系人名称”
+“从((报告上的节内部联接报告。节\u ID=节。节\u ID)联系人上的内部联接联系人。联系人\u ID=报告。联系人\u ID)度量上的左联接度量。报告\u ID=报告。报告\u ID”
+“其中Section.Section_Name(如“%”+from+“%”或Report.Report_Name(如“%”+from+“%”或Metrics.Metric_Name(如“%”+from+“%”)或Contact.Contact_Name(如“%”+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”);
//数据值(部门名称、报告名称、联系人名称、链接);
addRow(新对象[]{section\u name,report\u name,contact\u name,link});
System.out.println(“第行的节名+节名”);
table.addMouseListener(new JTableButtonMouseListener(table));
MouseListener[] M = table.getMouseListeners();
        if(M.length>0)
            for(int i=0;i<M.length;i++)
        table.removeMouseListener(M[i]);
public class r_search_2 extends JFrame implements ActionListener {

    static int counter;
    static String sname = "";
    DefaultTableModel model;
    // String section_name = "";

    JFrame frame1;
    JLabel l0, l1, l2;
    JComboBox c1;
    JButton b1;
    Connection con;
    ResultSet rs, rs1;
    Statement st, st1;
    PreparedStatement pst;
    String ids;
    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");

        // l0.setBounds(100, 50, 350, 40);
        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);
//
//          }
            v.add("aa");
            v.add("bb");
            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() {
        // int counter=0;
        model = new DefaultTableModel();

        MouseListener[] M = table.getMouseListeners();
        if(M.length>0)
            for(int i=0;i<M.length;i++)
        table.removeMouseListener(M[i]);

        table.setRowHeight(35);

        JTableHeader th = table.getTableHeader();
        th.setPreferredSize(new Dimension(30, 30));
        th.setFont(th.getFont().deriveFont(Font.BOLD));
        th.setForeground(Color.BLACK);
        th.setBackground(Color.LIGHT_GRAY);

        // 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();
        if (from.equals("")) {
            JOptionPane.showMessageDialog(null, "Please enter a search term",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            TableCellRenderer buttonRenderer = new JTableButtonRenderer();
            table.getColumn("METRICS").setCellRenderer(buttonRenderer);

            table.addMouseListener(new JTableButtonMouseListener(table));
            // static int i=0;
            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 = "Section_Name";
                    report_name = "Report_Name";
                    contact_name = "Contact_Name";
                    link = "Link";
                    // data_values(section_name,report_name,contact_name,link);

                    model.addRow(new Object[] { section_name, report_name,
                            contact_name, link });
                    System.out.println("section name at row" + section_name);
                    /*
                     * s_name(String section_name) {
                     * 
                     * }
                     */
                    sname = section_name;
                    // getsecname(section_name);
                    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;
            JButton button = new JButton("LIST OF METRICS");
            button.setBackground(Color.YELLOW);
            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) {
            counter = 0;
            System.out
                    .println("***************************************************************");
            System.out.println("counter value=" + counter++);
            System.out
                    .println("/////////////////////////////////////////////////////////////////////");
            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);
                System.out.println("row clicked=" + row);
                System.out.println("column clicked=" + column);
                System.out.println("object value=" + value);
                /*
                 * public void getsecname(String s) { String ss=s; }
                 */

                if (value == null) {
                    Object v = table.getValueAt(row, 0);
                    System.out
                            .println("--------------------------------------------");
                    System.out.println("object value=" + v);
                    String s = v.toString();
                    if (s.equals("Section_Name")) {
                        JOptionPane.showMessageDialog(null, "list of metrics",
                                "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
                if (value instanceof JButton) {
                    ((JButton) value).doClick();
                }
            }
        }
    }

    public 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 final Class<?>[] COLUMN_TYPES = new Class<?>[] { String.class,
                String.class, String.class, String.class, JButton.class };

        @Override
        public int getColumnCount() {
            // System.out.println("inside getcolumncount");
            // System.out.println(columnNames.length);
            return columnNames.length;

        }

        @Override
        public int getRowCount() {
            // System.out.println("inside getrowcount");
            return 100;
        }

        @Override
        public String getColumnName(int columnIndex) {
            // System.out.println("inside getColumnName");
            // System.out.println("column index="+columnIndex);
            // System.out.println(columnNames[columnIndex]);
            // System.out.println("section name="+section_name);
            return columnNames[columnIndex];
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            // System.out.println("inside columnIndex");
            // System.out.println(COLUMN_TYPES[columnIndex]);

            return COLUMN_TYPES[columnIndex];
        }

        @Override
        public Object getValueAt(final int rowIndex, final int columnIndex) {
            System.out.println("inside getValueAt");
            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));
                // System.out.println("halloween");
                button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        JOptionPane.showMessageDialog(JOptionPane
                                .getFrameForComponent(button),
                                "Button clicked for row " + rowIndex);
                    }
                });
                return button;
            default:
                return "";
            }
        }
    }

    /*
     * public String getRowData(int row) { if (model != null) { return
     * section_name; // return row of data from model here } else { // throw
     * some exception } return contact_name; }
     */

    public static void main(String args[]) {

        /*
         * try { for (LookAndFeelInfo info :
         * UIManager.getInstalledLookAndFeels()) {
         * 
         * if ("Nimbus".equals(info.getName())) {
         * UIManager.setLookAndFeel(info.getClassName()); break;
         * 
         * } } } catch (Exception e) { // If Nimbus is not available, you can
         * set the GUI to another look and feel. }
         */

        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();
    }
}