2016-01-01 22 views
0

新手在這裏。首先我很抱歉,如果這篇文章不遵守的規則stackoverflow。我想從3年前從這個源問同樣的問題(我認爲這是錯誤的答案): stackoverflow sourceJava從另一個類獲取選定的Combobox

如何從一個類獲得ComboBox的選擇項並使用新的所選項目的值類。

比方說,源類和其他類。我想從其他類的源類打印項目3(在ComboBox中的第三項)。

我已經使用了以上來源的答案。但是,它只返回第一個項目。因爲我認爲每次我從源類調用構造函數時,都會將所選項重新啓動到第一項。

如何當我使用javax.swing.JFrame中(我使用NetBeans)做呢?

public class Source extends javax.swing.JFrame{ 

final JComboBox test = new JComboBox(); 
test.setModel(new DefaultComboBoxModel(new String[] {"Item 1", "Item 2", "Item 3"})); 

... 

public String getSelectedItem() { 
    return (String) test.getSelectedItem(); 
} 

其他類:

public class Other extends javax.swing.JFrame{ 

public Other(){ 

Source sc = new Source(); 
String var = sc.getSelectedItem(); 

System.out.println(var); 
} 
} 

比方說,我選擇第3項的源類。那麼它會在其他課上獲得第3項?或者我做錯了構造函數?抱歉給你帶來不便。

+1

HTML?搖擺? AWT? JavaFX的? SWT? – MadProgrammer

+0

這是javax.swing.JFrame @MadProgrammer –

回答

2

我要問同樣的問題(它有錯誤的答案),從3年前...

不,答案是完全正確。

如何從一個類中獲取ComboBox的選定項並在新類中使用該選定項的值。

同樣,Reimeus告訴你如何正確地做到這一點。

比方說,源類和其他類。我想從其他類的源類打印項目3(在ComboBox中的第三項)。

不知道你的"item 3"的意思,但如果它是另一個選擇的項目,你又指的答案是正確的

我已經使用了上面的答案。但是,它只返回第一個項目。因爲我認爲每次我從源類調用構造函數時,都會將所選項重新啓動到第一項。

而這正是你的問題 - 你不應該重新調用構造函數,因爲這會給你錯誤的參考。它會給你一個全新的GUI參考,一個是圖形用戶界面,沒有顯示。您需要參考當前查看的感興趣的類。你如何做到這一點將取決於你的程序結構 - 請展示更多。

例如:注意下面的代碼有兩個Jbutton將,一個沒有它的ActionListener中正確的事(實際上,一個AbstractAction,但類似結構),以及一個做事不正確:

import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class GetCombo extends JFrame { 
    // the displayed ClassWithCombo object 
    private ClassWithCombo classWithCombo = new ClassWithCombo(this);; 
    private int columns = 10; 
    private JTextField textField1 = new JTextField(columns); 
    private JTextField textField2 = new JTextField(columns); 

    public GetCombo() { 
     classWithCombo.pack(); 
     classWithCombo.setLocationByPlatform(true); 
     classWithCombo.setVisible(true); 

     setLayout(new FlowLayout()); 

     add(textField1); 
     add(new JButton(new AbstractAction("Doing It Right") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // use the ClassWithCombo reference that is already displayed 
       String selectedString = classWithCombo.getSelectedItem(); 
       textField1.setText(selectedString); 
      } 
     })); 
     add(textField2); 
     add(new JButton(new AbstractAction("Doing It Wrong") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // create a new non-displayed ClassWithCombo reference. 
       ClassWithCombo classWithCombo = new ClassWithCombo(GetCombo.this); 
       String selectedString = classWithCombo.getSelectedItem(); 
       textField2.setText(selectedString); 
      } 
     })); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      GetCombo getCombo = new GetCombo(); 
      getCombo.setTitle("Get Combo Example"); 
      getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      getCombo.pack(); 
      getCombo.setLocationRelativeTo(null); 
      getCombo.setVisible(true); 
     }); 
    } 
} 

@SuppressWarnings("serial") 
class ClassWithCombo extends JDialog { 
    private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" }; 
    private JComboBox<String> combo = new JComboBox<>(DATA); 

    public ClassWithCombo(JFrame frame) { 
     super(frame, "Holds Combo Dialog", ModalityType.MODELESS); 
     setLayout(new FlowLayout()); 
     setPreferredSize(new Dimension(300, 250)); 
     add(combo); 
    } 

    public String getSelectedItem() { 
     return (String) combo.getSelectedItem(); 
    } 

} 


編輯
閱讀的最新文章後,我現在看到你正在試圖打開包含窗口的窗口組合呈現給用戶得到主程序在運行時使用的信息。在這種情況下,最好的辦法是使用對話框,即在對話框打開後凍結主窗口中的程序流的對話框,該對話框不允許用戶在對話框中與主窗口進行交互已經打開,然後在對話框窗口關閉時恢復程序流程和用戶交互。

請看下面對我的示例程序的更改。在這裏,我改變了用於JDialog的超級構造函數,使其成爲APPLICATION_MODAL,具有上述行爲。然後在主窗口的Butotn的ActionListener中打開對話框。由於組合窗口是一個模態對話框 - 程序不會從組合窗口中提取信息,直到它被關閉 - 這個位是非常重要的。這樣,您的主程序就可以獲取用戶的選擇,而不總是組合框中的第一項。我添加了一個名爲submitButton的新JButton,它所做的就是關閉當前窗口。如果需要,您可以將ActionListener添加到JComboBox中,以便在選擇完成後關閉它的窗口,但這不允許用戶改變主意,所以我更喜歡使用提交按鈕。

請注意更改標記爲\\ !!評論。

import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class GetCombo2 extends JFrame { 
    // the displayed ClassWithCombo object 
    private ClassWithCombo classWithCombo = new ClassWithCombo(this);; 
    private int columns = 10; 
    private JTextField textField1 = new JTextField(columns); 

    public GetCombo2() { 
     classWithCombo.pack(); 
     classWithCombo.setLocationByPlatform(true); 

     // !! don't do this here 
     // classWithCombo.setVisible(true); 

     setLayout(new FlowLayout()); 

     textField1.setFocusable(false); 
     add(textField1); 
     add(new JButton(new AbstractAction("Open Combo as a Dialog") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // open combo dialog as a **modal** dialog: 
       classWithCombo.setVisible(true); 

       // this won't run until the dialog has been closed 
       String selectedString = classWithCombo.getSelectedItem(); 
       textField1.setText(selectedString); 
      } 
     })); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      GetCombo2 getCombo = new GetCombo2(); 
      getCombo.setTitle("Get Combo Example"); 
      getCombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      getCombo.pack(); 
      getCombo.setLocationRelativeTo(null); 
      getCombo.setVisible(true); 
     }); 
    } 
} 

@SuppressWarnings("serial") 
class ClassWithCombo extends JDialog { 
    private static final String[] DATA = { "Mon", "Tues", "Wed", "Thurs", "Fri" }; 
    private JComboBox<String> combo = new JComboBox<>(DATA); 

    public ClassWithCombo(JFrame frame) { 
     // !! don't make it MODELESS 
     // !! super(frame, "Holds Combo Dialog", ModalityType.MODELESS); 
     // !! note the change. Made it APPLICATION_MODAL 
     super(frame, "Holds Combo Dialog", ModalityType.APPLICATION_MODAL); 

     JButton submitButton = new JButton(new AbstractAction("Submit") { 
      // !! add an ActionListener to close window when the submit button 
      // has been pressed. 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       ClassWithCombo.this.setVisible(false); 
      } 
     }); 

     setLayout(new FlowLayout()); 
     setPreferredSize(new Dimension(300, 250)); 
     add(combo); 
     add(submitButton); 
    } 

    public String getSelectedItem() { 
     return (String) combo.getSelectedItem(); 
    } 

} 
+0

謝謝。對不起,我的壞帖子(仍在學習)。我已經編輯過它。怎麼樣? –

+0

@ArbintoroMas:我無法編譯和運行你的發佈代碼,所以我不能告訴你如何解決它。關鍵是理解引用是什麼 - 您必須在引用包含JComboBox的可視化GUI的引用上調用getSelectedItem()方法(根據Reimeus的答案)。一個很好的測試,看你是否正確地做這件事情是'新的Source()'應該只在你的整個組合程序中出現一次。如果你看到它不止一次,那麼你可能會做錯事。 –

+0

@ArbintoroMas:請參閱代碼示例添加回答。並且不要使用兩個JFrame,因爲您的GUI應該只有一個JFrame。如果您有多個窗口,則應該是JFrame和其他JDialog。 –