如何从Java中的另一个类访问comboBox

时间:2022-02-06 16:00:25

comboBoxEnvIn Java is it possible to access a comboBox when it is sitting within a different class entirely?I want to use it as a whole and not just pass through it's values.

comboBoxEnvIn Java是否可以在完全坐在不同的类中时访问它?我想将它作为一个整体使用而不只是传递它的值。

I have two classes - each has a GUI. I have my main.java (main) class and my ExportImportWindow.java

我有两个类 - 每个类都有一个GUI。我有我的main.java(主)类和我的ExportImportWindow.java

I am trying to access the comboBox that I use in my ExportImportWindow class called comboBoxEnv

我正在尝试访问我在名为comboBoxEnv的ExportImportWindow类中使用的comboBox

Currently my code in my main class refers/uses the wrong comboBox called comboBox.

目前我的主类中的代码引用/使用名为comboBox的错误comboBox。

It's causing the information that is written to a file to be the incorrect information.

它导致写入文件的信息是不正确的信息。

My code from main is this:

我的主要代码是:

if (comboBox.getSelectedItem() == null ){

JOptionPane.showMessageDialog( null, "Please choose an ei");
}

else {

String env = comboBox.getSelectedItem().toString();

ExportImportWindow frm = new ExportImportWindow("Export",env,xmlFile , null, me); 

frm.setVisible( true );
}

I am unsure as to how I can get access to the correct comboBox which sits in the other class like so. I have already declared it at the top of my code as to attempt to access it within my main class.

我不确定如何能够访问位于其他类中的正确的comboBox。我已经在我的代码顶部声明它尝试在我的主类中访问它。

public final JComboBox comboBoxEnv ;

public ExportImportWindow( final String mode, final String env,String FromFile,String ToFile, final Main main){

comboBoxEnv = new JComboBox();

//other code to populate comboBoxEnv
}

This is what I currently have after my last attempt did not work:

这是我上次尝试后无法正常工作的内容:

    ExportImportWindow exportImport = new ExportImportWindow(//not sure what to pass through);
    exportImport.comboBoxEnv; // trying to access the method the ComboBoxEnv sits in

I feel all the articles I look at are simply asking how to pass the values whether as I want to use the entire comboBox and not just it's values. Really appreciate any one who could share their knowledge on this. I did get access to it but it was only by creating it as a method which did not work.

我觉得我所看到的所有文章都只是询问如何传递值,无论是否要使用整个comboBox而不仅仅是它的值。真的很感激能够分享他们的知识的人。我确实可以访问它,但它只是通过创建它作为一个不起作用的方法。

1 个解决方案

#1


1  

You can define getter in your ImpexWindow (better) or just use the class' field because the field is public (not recommended way).

您可以在ImpexWindow中定义getter(更好)或只使用class'字段,因为该字段是公共的(不推荐使用方式)。

So in your ExportImportWindow you should keep a reference to the ImpexWindow where the combo box is defined and use the ImpexWindow's field

因此,在ExportImportWindow中,您应该保留对定义组合框的ImpexWindow的引用,并使用ImpexWindow的字段

#1


1  

You can define getter in your ImpexWindow (better) or just use the class' field because the field is public (not recommended way).

您可以在ImpexWindow中定义getter(更好)或只使用class'字段,因为该字段是公共的(不推荐使用方式)。

So in your ExportImportWindow you should keep a reference to the ImpexWindow where the combo box is defined and use the ImpexWindow's field

因此,在ExportImportWindow中,您应该保留对定义组合框的ImpexWindow的引用,并使用ImpexWindow的字段