繁体   English   中英

如何访问其他方法中定义的变量

[英]how to access variables defined in other method

要求是我需要访问类JTableModel扩展AbstractTableModel的 showTableData()方法中定义的变量。我需要使用JTableModel的showTableData()中定义的值section_name,report_name,contact_name,link_name。 现在该怎么做?

public class r_search_2 extends JFrame implements ActionListener
{
  //code
    r_search_2() 
    {

      //code

    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();
        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);
        //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");
                data_values(section_name,report_name,contact_name,link);



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

    }


    public static void main(String args[])
    {
          //code
    }
}

您的问题之一是您的关键变量对于showTableData方法都是本地的,因此仅在该方法中可见。 如果要使用mutator和accessor方法,则将TableModel设为一个字段,并提供类方法以从模型中提取数据,或在模型中添加或更改数据(尽管只能通过模型​​的方法)。

所以:

public class SomeClass extends JFrame implements ActionListener {
    private DefaultTableModel model;

    public Object getRowData(int row) {
       if (model != null) {
          // return row of data from model here 
       } else {
          // throw some exception
       }
    }


    SomeClass() 
    {

      //code
    }

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

    public void showTableData()
    {
        // DefaultTableModel model = new DefaultTableModel();
        model = new DefaultTableModel(); // don't re-declare the model here

        model.setColumnIdentifiers(columnNames);
        table.setModel(model);

您还需要阅读一些有关访问器/更改器(也称为获取器/设置器)的基本Java教程。 在您首先了解基本Java的基础知识之前,我们将无法为您提供很多帮助。

暂无
暂无

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

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