خرید بک لینک

Vote count: 0

While developing a small task manager I have noticed that columns aren't sorted correctly. To discard problems with my program I have created a minimal version but it still fails to order the unique column right.

import java.awt.BorderLayout;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class TableSortTest extends JFrame
{
    private final JTable table;
    private final ATableModel model;

    public TableSortTest ()
    {
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        setSize (1366, 768);
        setLocationRelativeTo (null);

        model = new ATableModel ();
        table = new JTable ();
        table.setFillsViewportHeight (true);
        table.setAutoCreateRowSorter (true);
        table.setModel (model);

        add (new JScrollPane (table), BorderLayout.CENTER);

        setVisible (true);

        Worker worker = new Worker ();
        worker.execute ();
    }

    private class Pair
    {
        int index;
        int value;
    }

    private class Worker extends SwingWorker <Void, Pair>
    {
        @Override
        protected Void doInBackground ()
        {
            while (!isCancelled ())
            {
                Random r = new Random ();
                for (int i = 0; i < 100; i++)
                {
                    int indice = getIndexInRange (0, 99);
                    Pair p = new Pair ();
                    p.index = indice;
                    p.value = Math.abs (r.nextInt ());
                    publish (p);
                }

                try
                {
                    Thread.sleep (1000);
                }
                catch (InterruptedException ie)
                {
                    ie.printStackTrace ();
                }
            }

            retu null;
        }

        @Override
        public void process (List <Pair> items)
        {
            for (Pair p : items)
            {
                model.setValueAt (p.value, p.index, 0);
            }
        }
    }

    public static int getIndexInRange (int min, int max)
    {
        retu (min + (int) (Math.random () * ((max - min) + 1)));
    }

    private class ATableModel extends AbstractTableModel
    {
        private final Integer [] data;

        public ATableModel ()
        {
            data = new Integer [100];

            Random r = new Random ();

            for (int i = 0; i < 100; i++)
            {
                data [i] = Math.abs (r.nextInt ());
            }
        }

        @Override
        public int getColumnCount ()
        {
            retu 1;
        }

        @Override
        public int getRowCount ()
        {
            retu data.length;
        }

        @Override
        public Object getValueAt (int rowIndex, int columnIndex)
        {
            retu data [rowIndex];
        }

        @Override
        public void setValueAt (Object value, int rowIndex, int columnIndex)
        {
            data [rowIndex] = (Integer) value;
            fireTableRowUpdated (rowIndex, columnIndex);
        }

        @Override
        public Class getColumnClass (int columnIndex)
        {
            retu Integer.class;
        }

        @Override
        public String getColumnName (int col)
        {
            retu "Column";
        }
    }

    public static final void main (String [] args)
    {
        SwingUtilities.invokeLater (() ->
        {
            try
            {
                new TableSortTest ();
            }
            catch (Exception e)
            {
                e.printStackTrace ();
            }
        });
    }
}

I have tried with a ScheduledExecutorService + Ruable and a Timer + TimerTask just to test if it was a threading problem, but the behavior is the same. I have also read the Java Tutorial page about the subject. Given that my table only uses standard types I think that a simple table.setAutoCreateRowSorter (true); should do the job, shouldn't it?

Shouldn't the table be sorted after every modification/addition/removal even is fired?

Greetings.

asked 37 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 22 فروردين 1395 ساعت: 1:01

صفحه بندی