如何将比较器与JButton链接?
我有一个" ascent"," descent"按钮。当我按下按钮时,我想在面板中对名称进行排序。我想使用比较器类对它们进行排序。
我知道这是很多代码。遗憾!
我有4个类:MainWindow,JListData,Start,MyComparator
主窗口:
public class MainWindow extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField txName = null;
private JButton butOK = new JButton("ok");
private JButton butDelete = new JButton("delete");
private JButton butAscent = new JButton("ascending");
private JButton butDescent = new JButton("descending");
private int selectedIndex =-1;
private String selectedName = null;
private JListData dataModel = null;
public MainWindow() throws HeadlessException {
this("Demo JList");
}
public MainWindow(String titel) throws HeadlessException {
super(titel);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//ContentPane
Container cpane = this.getContentPane();
cpane.setLayout( new BorderLayout() ); // Festlegung des Layouts
JPanel pnlOben = new JPanel();
txName = new JTextField(20);
pnlUp.add(txName);
pnlUp.add(butOK);
pnlUp.add(butDelete);
pnlUp.add(butAscent);
pnlUp.add(butDescent);
cpane.add(pnlUp, BorderLayout.NORTH);
// Listener
butOK.addActionListener(new ActionAdapter1());
butDelete.addActionListener(new ActionAdapter2());
butAscent.addActionListener(new ActionAdapter3());
//butDescent.addActionListener(new ActionAdapter4());
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel = makeTextPanel();
tabbedPane.addTab("A-Z", null, panel, "Name with \"" + "A-Z" + "\"");
// JTabbedPane on ContentPane
cpane.add(tabbedPane, BorderLayout.SOUTH);
this.pack();
}
public JPanel makeTextPanel()
{
JPanel panel = new JPanel(false);
panel.setLayout(new GridLayout(1, 1));
dataModel = new JListData();
JList liste = new JList(dataModel);
liste.setLayoutOrientation(JList.VERTICAL);
liste.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
liste.setVisibleRowCount(-1);
JScrollPane scrollPane = new JScrollPane(liste);
scrollPane.setPreferredSize(new Dimension(450, 260));
liste.addListSelectionListener(new ListSelectionAdapter(liste));
panel.add(scrollPane);
return panel;
}
class ListSelectionAdapter implements ListSelectionListener
{
JList myList;
public ListSelectionAdapter(JList list)
{
myList= list;
}
public void valueChanged(ListSelectionEvent ev )
{
if(ev.getValueIsAdjusting() == false){
selectedName = (String)myList.getSelectedValue();
System.out.println("selected String " + selectedName);
selectedIndex = myList.getSelectedIndex();
System.out.println("selected Index " + selectedIndex);
}
}
}// end of inner class ListSelectionAdapter
class ActionAdapter1 implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
System.out.println("ok Button " + txName.getText());
dataModel.addElement(txName.getText());
}
}
class ActionAdapter2 implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
System.out.println("delete Button " + selectedName);
if(selectedIndex > -1){
dataModel.removeElement(selectedName, selectedIndex);
}
}
}
**// SHOULD SORTING BE HERE?! #help**
class ActionAdapter3 implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
}
}}
JListData:
public class JListData extends AbstractListModel {
private static final long serialVersionUID = 1L;
private Vector <String> dlist;
public JListData() {
dlist = new Vector <String> ();
makeData();
}
private Vector <String> makeData()
{
String[] data = {"Otto", "Gustav", "Hugo", "Anna", "Emil", "Marianne"};
for(int i = 0; i < data.length; i++){
dlist.addElement( (String) data[i]);
}
return dlist;
}
public Object getElementAt(int index)
{
return dlist.elementAt(index);
}
public int getSize()
{
return dlist.size();
}
public void addElement(String s){
dlist.addElement(s);
this.fireIntervalAdded(this, dlist.size()-1, dlist.size());
}
public void removeElement(String s, int index){
System.out.println("removeElement: s = "
+ s);
System.out.println("removeElement: index = "
+ index);
dlist.remove(s);
int index0 = index;
int index1 = index;
this.fireIntervalRemoved(this, index0, index1);
}}
开始:
public class Start {
public static void main(String[] args) {
MainWindow window = new MainWindow();
window.setVisible(true);
}}
MyComparator:
public class MyComparator implements Comparator<JListData> {
public int compare(JListData name1, JListData name2) {
int tmp = 0;
tmp = name1.getName.compareTo(name2.getName());
return tmp;
}}