使用java.sql.Time对象对DefaultListModel进行排序

时间:2014-04-08 01:46:54

标签: java swing sorting jlist defaultlistmodel

我有DefaultListModel,我可以在运行时添加和删除项目,但我想在更新后对它们进行排序,使用sql.Time对象作为比较器,但我不是真的得到怎么做。他是我的代码: 这个DLM填充在JFrame1

public static final DefaultListModel m = new DefaultListModel();
// for cicle to retrieve from DB and add to DLM
m.addElement(new myObject(String,Time);
mylist.setModel(m)

使用以下命令从另一个JFrame访问和操作:

JFrame1.m.set(index, myObject); 

它实际上更新了JList但想要为此实现排序方法。

import java.text.SimpleDateFormat;
import java.sql.Time;
public class Cita implements Comparable<Cita> {
public Time horaInicio;
public Time horaTermino;
public Paciente paciente;
public String actividad;
public String observacion;
public String recordar;
public String ciudad;
public String TipoCita;
public String fecha;
public int idPaciente;
public int idCita;

SimpleDateFormat formatoInicio = new SimpleDateFormat("hh:mm");
SimpleDateFormat formatoTermino = new SimpleDateFormat("hh:mm aa");

public Cita() {
}

public Cita(String fecha, Time horaInicio, Time horaTermino, int idPaciente, String actividad,
        String observacion, String recordar, String ciudad, String tipoCita) {
    this.fecha = fecha;
    this.horaInicio = horaInicio;
    this.horaTermino = horaTermino;
    this.idPaciente = idPaciente;
    this.actividad = actividad;
    this.observacion = observacion;
    this.recordar = recordar;
    this.ciudad = ciudad;
    this.TipoCita = tipoCita;
}

public Cita(int idCita, String fecha, Time horaInicio, Time horaTermino, Paciente paciente, String actividad,
        String observacion, String recordar, String ciudad, String tipoCita) {
    this.idCita = idCita;
    this.fecha = fecha;
    this.horaInicio = horaInicio;
    this.horaTermino = horaTermino;
    this.paciente = paciente;
    this.actividad = actividad;
    this.observacion = observacion;
    this.recordar = recordar;
    this.ciudad = ciudad;
    this.TipoCita = tipoCita;
}

@Override
public int compareTo(Cita o) {
    return (this.getHoraInicio().compareTo(o.getHoraInicio()));
}

public int getIdCita() {
    return idCita;
}

public void setIdCita(int idCita) {
    this.idCita = idCita;
}

public Time getHoraInicio() {
    return horaInicio;
}

public void setHoraInicio(Time horaInicio) {
    this.horaInicio = horaInicio;
}

public Time getHoraTermino() {
    return horaTermino;
}

public void setHoraTermino(Time horaTermino) {
    this.horaTermino = horaTermino;
}

public Paciente getPaciente() {
    return paciente;
}

public void setPaciente(Paciente paciente) {
    this.paciente = paciente;
}

public String getActividad() {
    return actividad;
}

public void setActividad(String actividad) {
    this.actividad = actividad;
}

public String getObservacion() {
    return observacion;
}

public void setObservacion(String observacion) {
    this.observacion = observacion;
}

public String getRecordar() {
    return recordar;
}

public void setRecordar(String recordar) {
    this.recordar = recordar;
}

public String getCiudad() {
    return ciudad;
}

public void setCiudad(String ciudad) {
    this.ciudad = ciudad;
}

public String getTipoCita() {
    return TipoCita;
}

public void setTipoCita(String TipoCita) {
    this.TipoCita = TipoCita;
}

public int getIdPaciente() {
    return idPaciente;
}

public void setIdPaciente(int idPaciente) {
    this.idPaciente = idPaciente;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 71 * hash + this.idCita;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Cita other = (Cita) obj;
    if (this.idCita != other.idCita) {
        return false;
    }
    return true;
}

public String getFecha() {
    return fecha;
}

public void setFecha(String fecha) {
    this.fecha = fecha;
}

@Override
public String toString() {
    return paciente.getNombre() + ", "
            + formatoInicio.format(horaInicio) + "-"
            + formatoTermino.format(horaTermino);

}

}

很多参数但是我发布了全班。感谢

1 个答案:

答案 0 :(得分:1)

您可以MyObject实施Comparable,然后比较Time方法中的compareTo个对象。像

这样的东西
private class MyObject implements Comparable<MyObject> {

    private Time time;
    private String name;

    public MyObject(String name, Time time) {
        this.time = time;
        this.name = name;
    }

    @Override
    public int compareTo(MyObject o) {
        return (this.getTime().compareTo(o.getTime()));
    }

    public Time getTime() {
        return time;
    }

    @Override
    public String toString() {
        return name + " : " + time;
    }

}

然后您可以使用Collections.sort对列表进行排序。您需要将DefaultlistModel中的数据添加到单独的列表中,对该列表进行排序,从DefaultListModel中删除元素,然后再添加已排序的元素。像

这样的东西
private void sortModel(DefaultListModel model) {
    List<MyObject> list = new ArrayList<>();
    for (int i = 0; i < model.size(); i++) {
        list.add((MyObject)model.get(i));
    }
    Collections.sort(list);
    model.removeAllElements();
    for (MyObject s : list) {
        model.addElement(s);
    }
}

您可以在下面看到完整的示例。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class TestListSort {

    public TestListSort() {
        JList list = createList();
        JButton button = createButton(list);
        JScrollPane scroll = new JScrollPane(list);
        scroll.setPreferredSize(new Dimension(200, 150));

        JFrame frame = new JFrame();
        frame.add(scroll);
        frame.add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public JButton createButton(final JList list) {
        JButton button = new JButton("Sort");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                DefaultListModel model = (DefaultListModel)list.getModel();
                sortModel(model);
            }
        });
        return button;
    }

    private void sortModel(DefaultListModel model) {
        List<MyObject> list = new ArrayList<>();
        for (int i = 0; i < model.size(); i++) {
            list.add((MyObject)model.get(i));
        }
        Collections.sort(list);
        model.removeAllElements();
        for (MyObject s : list) {
            model.addElement(s);
        }
    }

    private JList createList() {
        JList list = new JList(createModel());
        return list;
    }

    private DefaultListModel createModel() {
        Random random = new Random();
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < 20; i++) {
            long time = random.nextLong();
            Time timeObj = new Time(time);
            model.addElement(new MyObject("Object " + i, timeObj));
        }
        return model;

    }


    private class MyObject implements Comparable<MyObject> {

        private Time time;
        private String name;

        public MyObject(String name, Time time) {
            this.time = time;
            this.name = name;
        }

        @Override
        public int compareTo(MyObject o) {
            return (this.getTime().compareTo(o.getTime()));
        }

        public Time getTime() {
            return time;
        }

        @Override
        public String toString() {
            return name + " : " + time;
        }

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestListSort();
            }
        });
    }
}

<强>更新

很抱歉,但我没有得到例外。以下是我唯一改变的事情

private DefaultListModel createModel() {
    Random random = new Random();
    DefaultListModel model = new DefaultListModel();
    for (int i = 0; i < 20; i++) {
        long horaInicio = random.nextLong();
        long horaTermino = random.nextLong();
        Time timeInicio = new Time(horaInicio);
        Time timeTermino = new Time(horaTermino);
        Paciente paciente = new Paciente("Paciente " + i);
        Cita cita = new Cita(i, "blah", timeInicio, timeTermino, paciente,
                "blah", "blah", "blah", "blah", "blah");
        model.addElement(cita);
    }
    return model;

}
......
private void sortModel(DefaultListModel model) {
    List<Cita> list = new ArrayList<>();
    for (int i = 0; i < model.size(); i++) {
        list.add((Cita) model.get(i));
    }
    Collections.sort(list);
    model.removeAllElements();
    for (Cita s : list) {
        model.addElement(s);
    }
}

我添加了自己的Paciente类来摆脱无法编译的代码

class Paciente {
    private String nombre;

    public Paciente(String nombre) {
        this.nombre = nombre;
    }

    public String getNombre() {
        return nombre;
    }
}