첫째, 예제 코드는 의미가 없습니다. 실제 Java 코드의 형태는 아니며 어떤 식 으로든 SSCCE 원칙을 준수하지 않습니다.
귀하의 질문은 귀하의 요구 사항을 확인하기위한 충분한 단서를 제공합니다. GlazedLists는 목록을 동적으로 필터링하기위한 프레임 워크를 제공하며 모두 MatcherEditor
클래스를 통해 수행됩니다.
GlazedLists Developer에는 멋진 스크린 캐스트가 제공되며 동적 필터링을 트리거하기 위해 MatcherEditor와 JComboBox 선택 항목을 연결하는 방법에 대해 정확히 설명한 작업을 처리하는 simple example이 있습니다. 이 예제의 source
여기에 포함하기에 충분히 짧다 :
package ca.odell.glazedlists.example;
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.AbstractMatcherEditor;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.swing.EventTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CustomMatcherEditorExample {
public static class AmericanIdol {
private String name;
private int votes;
private String nationality;
public AmericanIdol(String name, int votes, String nationality) {
this.name = name;
this.votes = votes;
this.nationality = nationality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public int getVotes() {
return votes;
}
public void setVotes(int votes) {
this.votes = votes;
}
public void incrementVotes() {
this.votes++;
}
}
public static void main(String[] args) {
// create an EventList of AmericanIdol
final EventList idols = new BasicEventList();
idols.add(new AmericanIdol("Simon Cowell", 0, "British"));
idols.add(new AmericanIdol("Paula Abdul", 0, "American"));
idols.add(new AmericanIdol("Randy Jackson", 0, "American"));
idols.add(new AmericanIdol("Ryan Seacrest", 0, "American"));
final NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
final FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);
// build a JTable
String[] propertyNames = new String[] {"name", "votes"};
String[] columnLabels = new String[] {"Name", "Votes"};
TableFormat tf = GlazedLists.tableFormat(AmericanIdol.class, propertyNames, columnLabels);
JTable t = new JTable(new EventTableModel(filteredIdols, tf));
// place the table in a JFrame
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.add(nationalityMatcherEditor.getComponent(), BorderLayout.NORTH);
f.add(new JScrollPane(t), BorderLayout.CENTER);
// show the frame
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class NationalityMatcherEditor extends AbstractMatcherEditor implements ActionListener {
private JComboBox nationalityChooser;
public NationalityMatcherEditor() {
this.nationalityChooser = new JComboBox(new Object[] {"British", "American"});
this.nationalityChooser.getModel().setSelectedItem("Filter by Nationality...");
this.nationalityChooser.addActionListener(this);
}
public Component getComponent() {
return this.nationalityChooser;
}
public void actionPerformed(ActionEvent e) {
final String nationality = (String) this.nationalityChooser.getSelectedItem();
if (nationality == null)
this.fireMatchAll();
else
this.fireChanged(new NationalityMatcher(nationality));
}
private static class NationalityMatcher implements Matcher {
private final String nationality;
public NationalityMatcher(String nationality) {
this.nationality = nationality;
}
public boolean matches(Object item) {
final AmericanIdol idol = (AmericanIdol) item;
return this.nationality.equals(idol.getNationality());
}
}
}
}
당신은 당신의 특별한 요구에 대한 자신 MatcherEditor을 구축해야하지만, 예를 들어 위의 좋은 템플릿을 제공합니다. MatcherEditor의 목적은 필터링 된 것을 결정하는 로직을 제공하거나 기술적으로 특정 입력에 대해 무엇이 머무르는지를 결정하는 것입니다.
MatcherEditor에는 필터링을 트리거하려는 구성 요소에 대한 일종의 액세스 권한이 필요합니다. 많은 예제에서는 MatcherEditor가 특정 Swing 구성 요소의 생성자이자 소유자가되었지만 전달 된 경우에도 똑같이 적용됩니다.
그리고 나서 MatcherEditor를 FilterList에 연결하는 경우 일뿐입니다.이 필터는 텍스트 필터링을 수행 한 적이 있다면 익숙 할 것입니다.
당신의 답장을위한 고맙습니다. 사실 당신의 예제는 나를 위해 작동합니다.하지만 위에서 언급 한 코드는 자바 코드가 아닙니다. yaml.so의 디자인 코드는 자바 코드와 유사합니다. – Ajay
@ user2668129 그래, 원래 질문은 여러 수준에서 이해가되지 않았다. 1) yaml의 형식이 올바르지 않았다. 2) yaml 자체가 스윙 인터페이스를 만드는 전형적인 방법이 아니기 때문에 아마도 일부 타사 라이브러리를 사용하고있을 것이다. yaml 코드를 이해할 수 있지만 언급하지 못합니다. 3) GlazedLists는 yaml 디자인에서 선언 할 수있는 스윙 구성 요소가 아닙니다. 따라서 Java 코드를 사용하여 JComboBox를 GlazedLists에 연결하는 방법을 설명해야합니다. – arooaroo
당신이 이런 것들을 많이 안다면 왜 yaml 코드와 java 코드를 구별 할 수 없습니까? – Ajay