简体   繁体   English

从另一个类引用 JTable

[英]Referencing a JTable from another class

Java newbie here. Java新手在这里。 I am using NetBeans 11.2.我正在使用 NetBeans 11.2。 I am making a GUI awt app, I have a main class as a JFrame, and I need to reference the JTable on it or its model from another class.我正在制作一个 GUI awt 应用程序,我有一个作为 JFrame 的主类,我需要从另一个类中引用它的 JTable 或它的模型。

Not to overload this post with lots of code, I created a tiny project from the tutorial on the Oracle site .为了避免用大量代码使这篇文章过载,我根据 Oracle 站点上的教程创建了一个小项目。 Basically, a project named "CelciusConverterProject", a JFrame named "CelciusconverterGUI" as main class in package "learn".基本上,一个名为“CelciusConverterProject”的项目,一个名为“CelciusconverterGUI”的 JFrame 作为包“learn”中的主类。 Then I plopped a jTable1 onto the frame and added a getter to CelciusconverterGUI for the table model named "getMdl()" :然后我将一个 jTable1 放到框架上,并为名为“getMdl()”的表模型添加了一个 getter 到 CelciusconverterGUI:

package learn;

import javax.swing.table.DefaultTableModel;

public class CelsiusConverterGUI extends javax.swing.JFrame {

[...]

          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                    new CelsiusConverterGUI().setVisible(true);
               }
          });
     }

     // Variables declaration - do not modify                     
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JTable jTable1;
     // End of variables declaration                   

     public DefaultTableModel getMdl() {
          return (DefaultTableModel) this.jTable1.getModel();
     }

}

No errors so far, the Navigator pane shows getMdl as a public method.到目前为止没有错误,导航器窗格将 getMdl 显示为公共方法。

Now I have created a new class called "Testing", but I cannot figure out how to call getMdl() from the instantiated running JFrame:现在我创建了一个名为“Testing”的新类,但我不知道如何从实例化的运行 JFrame 中调用 getMdl():

package learn;

import javax.swing.table.DefaultTableModel;

public class Testing {

     tmdl = (DefaultTableModel) <...>

}

Neither两者都不

 tmdl = new (DefaultTableModel) learn.CelsiusConverterGUI.

nor也不

 tmdl = (DefaultTableModel) learn.CelsiusConverterGUI.

is showing me my getter in the code completion window.在代码完成窗口中向我展示了我的 getter。

Probably a totally noobish question, but sorry I can't even google anyone who has asked the same question.可能是一个完全noobish 的问题,但很抱歉,我什至无法搜索任何提出相同问题的人。

You say you are "...making a Java awt app...", but you are using Swing classes.您说您是“...制作 Java awt 应用程序...”,但您正在使用 Swing 类。 AWT is a Java UI library, and Swing is a different Java UI library that still uses some AWT components. AWT 是一个 Java UI 库,而 Swing 是一个不同的 Java UI 库,它仍然使用了一些 AWT 组件。 But I think it's more useful and more accurate to say that you're writing a Java Swing app.但我认为说您正在编写 Java Swing 应用程序更有用、更准确。

Your getMdl() method returns a DefaultTableModel -- does it need to return something that specific?您的getMdl()方法返回一个DefaultTableModel —— 是否需要返回特定的内容? Especially when you're just starting, it will be simpler if you can work with existing libraries without casting any more than you need to.特别是当您刚刚开始时,如果您可以使用现有库而无需进行任何超出您需要的转换,那将会更简单。 The JTable.getModel() method returns TableModel , an interface ( https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html ) that guarantees that whatever object is returned will implement all the methods in the interface. JTable.getModel()方法返回TableModel ,一个接口( https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html )保证返回的任何对象都将实现接口中的所有方法。 So unless you need to call a method that is in DefaultTableModel and is not in TableModel , then you can eliminate the castings to DefaultTableModel (the (DefaultTableModel) you have in a couple of places.因此,除非您需要调用DefaultTableModel而不是TableModel ,否则您可以消除对 DefaultTableModel(您在几个地方拥有的(DefaultTableModel)的转换。

It is possible the code completion is confused by this casting -- if you were, in fact, attempting to cast CelsiusConverterGUI to DefaultTableModel , then it would not show you the getMdl() method because that doesn't exist in DefaultTableModel .代码CelsiusConverterGUI可能会被这种转换混淆——如果您实际上试图将CelsiusConverterGUIDefaultTableModel ,那么它不会向您显示getMdl()方法,因为它在DefaultTableModel中不存在。 I know you were probably trying to cast the return of getMdl() to DefaultTableModel , but the code completion logic may not realize that.我知道您可能试图将getMdl()的返回值getMdl()DefaultTableModel ,但代码完成逻辑可能没有意识到这一点。

So my recommendation is to have getMdl() return TableModel and eliminate all casting to DefaultTableModel .所以我的建议是让getMdl()返回TableModel并消除所有到DefaultTableModel I think your code completion will then show you getMdl() in your `CelsiusConverterGUI' class.我认为您的代码完成将在您的 `CelsiusConverterGUI' 类中显示getMdl()

One more thing: Java convention calls for spelling out words in names of things;还有一件事:Java 约定要求在事物的名称中拼出单词; most Java programmers would use and expect getModel() instead of getMdl() .大多数 Java 程序员会使用并期望getModel()而不是getMdl()

I'm adding another answer because we now have a better definition of the question.我正在添加另一个答案,因为我们现在对问题有了更好的定义。

You want to get a reference to the CelsuisConverterGUI object that you instantiated;您想要获得对您实例化的 CelsuisConverterGUI 对象的引用; to do that, you need to assign the result of the instantiation of the class to a variable and then obtain that reference somehow.为此,您需要将类的实例化结果分配给一个变量,然后以某种方式获取该引用。 Below is an example that I have not even compiled, but should be close -- it has a static variable that keeps the (only) reference to the main class, and instantiates the class the first time the getter for the instance is used:下面是一个我什至没有编译过的例子,但应该接近——它有一个静态变量,它保持对主类的(唯一)引用,并在第一次使用实例的 getter 时实例化类:

public class CelsiusConverterGUI extends JFrame
{
  private static CelsiusConverterGUI mainFrame = null;
  public static getMainFrame() 
  { 
    if (mainFrame == null)
    {
      java.awt.EventQueue.invokeAndWait(new Runnable() { mainFrame = new CelsiusConverter(); }
    }
    return mainFrame; 
  }

  public static void main(String ... arguments)
  {
    java.awt.EventQueue.invokeLater(new Runnable()
    {
      mainFrame = getMainFrame();
      mainFrame.setVisible(true);
    }
  }

  // ...
}

public class Testing
{
  CelsiusConverterGUI mainFrame = CelsiusConverterGUI.getMainFrame();
  // ...
}

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

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