[英]If the user selects a row in a JTreeTable SwingX, how do I find out which node is actually selected
[英]How do I change and instance variable for an object which the user selects from a combobox?
我是個菜鳥。 我剛讀完一本Java教科書,現在我正在做一個游戲作為學習實驗。 我感到難過...這是我的問題:
我從Person類創建了六個對象。
每個對象都有一個布爾變量,該變量會自動設置為false。
我可以從組合框中選擇每個Person對象並顯示有關它們的信息,但是我只能顯示用於將對象鏈接到組合框的信息(例如:person1.name允許我在組合框,並允許我在其他位置顯示名稱)。
我希望能夠更改每個對象內的其他實例變量。 我知道如何制作一種方法來更改每個對象,但是我必須制作六個方法,其中一個專門引用每個特定對象。
我想更改用戶從組合框中選擇的對象的布爾實例變量。 我以為可以用一個對象數組來做到這一點,但是我一直未能建立那個數組。 數組是正確的方法嗎? 還是有另一種方法?
JComboBox<String>
。 JComboBox<Person>
。 toString()
方法來返回它的名稱,從而控制Person對象的顯示方式。 getSelectedItem()
獲得選定的對象,然后調用適當的setter方法來設置選定的Person的布爾字段。 您聲明:
我知道如何制作一種方法來更改每個對象,但是我必須制作六個方法,其中一個專門引用每個特定對象。
不是這樣 一種方法可以很好地工作,它可以與任意數量的Person對象一起使用。
如果我有任何不清楚的建議,請通過評論告知我。 如果您還有其他更具體的問題,請編輯原始問題,同時也考慮發布相關代碼。
例如:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class PersonTest {
protected static final Color SELECTED_COLOR = Color.red;
@SuppressWarnings("serial")
private static void createAndShowGui() {
Person[] people = { new Person("Duck", "Donald"),
new Person("Mouse", "Mickey"), new Person("Trump", "Donald"),
new Person("Bunny", "Easter"), new Person("Claus", "Santa"),
new Person("Kringle", "Chris")};
final JComboBox<Person> combo = new JComboBox<>(people);
final JList<Person> personJList = new JList<>(people);
combo.setSelectedIndex(-1);
JPanel mainPanel = new JPanel();
mainPanel.add(new JScrollPane(combo));
mainPanel.add(new JScrollPane(personJList));
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Person selectedPerson = (Person) combo.getSelectedItem();
selectedPerson.setCheck(!selectedPerson.isCheck());
personJList.repaint();
}
});
personJList.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
Person person = (Person) value;
Component component = super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
if (person != null) {
if (person.isCheck()) {
component.setForeground(SELECTED_COLOR);
} else {
component.setForeground(null);
}
}
return component;
}
});
JFrame frame = new JFrame("PersonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Person {
private String firstName;
private String lastName;
private boolean check;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
this.check = false;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return String.format("%s, %s", lastName, firstName);
}
}
假設您的JComboBox
實際上包含對Person
對象的引用,則應提供適當的getter和setter,以允許您訪問對象中包含的值。
例如...
public class Person {
private String name;
public String getName() {
return name;
}
}
創建一個只讀Person
,名稱不能更改。
但是,如果我們這樣做...
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String value) {
name = value;
}
}
例如,這也使name屬性可寫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.