要求是我需要访问类 showTableData()中定义的变量 JTableModel extends AbstractTableModel 。我需要使用值section_name,report_name ,contact_name,link_name,它们在JTableModel中的showTableData()中定义。现在怎么做?
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
}
}
答案 0 :(得分:1)
您的一个问题是您的关键变量都是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教程,也称为getter / setter。我们将无法帮助您,直到您首先了解基本Java的基础知识。