setAutoCreateRowSorter() że niby jak to sortuje ?

0

Cześć.
Dany jest przykład z kisążki:

class PlanetTableFrame extends JFrame
{
   public PlanetTableFrame()
   {
      final JTable table = new JTable(cells, columnNames);
      add(new JScrollPane(table), BorderLayout.CENTER);

      JButton printButton = new JButton("Print");
      printButton.addActionListener(new ActionListener()
         {
	    @Override
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  table.print();
               }
               catch (java.awt.print.PrinterException e)
               {
                  e.printStackTrace();
               }
            }
         });
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(printButton);
      add(buttonPanel, BorderLayout.SOUTH);
   }
   private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" };

   private Object[][] cells = 
   { 
	 { "Mercury", 2440.0, 0, false, Color.YELLOW },
         { "Venus", 6052.0, 0, false, Color.YELLOW }, 
	 { "Earth", 6378.0, 1, false, Color.BLUE },
         { "Mars", 3397.0, 2, false, Color.RED },
	 { "Jupiter", 71492.0, 16, true, Color.ORANGE },
         { "Saturn", 60268.0, 18, true, Color.ORANGE },
         { "Uranus", 25559.0, 17, true, Color.BLUE }, 
	 { "Neptune", 24766.0, 8, true, Color.BLUE },
         { "Pluto", 1137.0, 1, false, Color.BLACK } 
   };

   private static final int DEFAULT_WIDTH = 400;
   private static final int DEFAULT_HEIGHT = 200;
}
 

ponoć wywołanie metody

table.setAutoCreateRowSorter(true)

powoduje automatyczne sortowanie wierszy kiedy klikniemy na nagłówek kolumny.
Mi wychodzi że nie bardzo.
Na pewno sortuje alfabetycznie nazwy planet - czy w takim razie sortuje tabelę według pierwszego obiektu w wierszu czy jak? Bo tylko po naciśnięciu tego nagłówka sortuje.
Taka błahostka a nie daje mi spokoju.
Ktoś coś wie na ten temat, coś?

0

Nie wierzę, sprawdziłem Twój przykład. Działa dobrze, tzn. sortuje po każdej kolumnie.

0

Nie wierzę - sprawdziłem, nic się nie zmieniło od ostatniego czasu :P chyba że ja o czymś nie wiem.

0

Może nie umiesz klikać.

import javax.swing.*;
import java.awt.*;

public class Planets
{
    public static void main(String[] args)
    {
        PlanetTableFrame okno=new PlanetTableFrame();
        okno.setVisible(true);
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        okno.setLocationRelativeTo(null);
    }
}
class PlanetTableFrame extends JFrame
{
   public PlanetTableFrame()
   {
      final JTable table = new JTable(cells, columnNames);
      table.setAutoCreateRowSorter(true);
      add(new JScrollPane(table), BorderLayout.CENTER);

      JButton printButton = new JButton("Print");
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(printButton);
      add(buttonPanel, BorderLayout.SOUTH);
      pack();
   }
   private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" };
 
   private Object[][] cells = 
   { 
         { "Mercury", 2440.0, 0, false, Color.YELLOW },
         { "Venus", 6052.0, 0, false, Color.YELLOW }, 
         { "Earth", 6378.0, 1, false, Color.BLUE },
         { "Mars", 3397.0, 2, false, Color.RED },
         { "Jupiter", 71492.0, 16, true, Color.ORANGE },
         { "Saturn", 60268.0, 18, true, Color.ORANGE },
         { "Uranus", 25559.0, 17, true, Color.BLUE }, 
         { "Neptune", 24766.0, 8, true, Color.BLUE },
         { "Pluto", 1137.0, 1, false, Color.BLACK } 
   };
 
   private static final int DEFAULT_WIDTH = 400;
   private static final int DEFAULT_HEIGHT = 200;
}
0

Mam wrażenie że cokolwiek powiem to żle wypadnę ale to mi nie sortuje - skoro mam księżyce to ja chcę mieć od 0 do 18 a mam 0,0,1,1,16,17,18,2,8. Po kliknięciu odwraca się tylko kolejność. Tak samo z innymi oprócz true-false.

0

To użyj innego modelu, w tym (Object[][]) do wyświetlania używana jest metoda toString() i liczby w trzeciej kolumnie sortowane są jak Stringi.

0

Masz przykład innego modelu (z usuniętym Plutonem). Sortowanie wg kolumn liczbowych jest sortowaniem wg wielkości.

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.table.*;

public class Planets
{
    public static void main(String[] args)
    {
        PlanetTableFrame okno=new PlanetTableFrame();
        okno.setVisible(true);
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        okno.setLocationRelativeTo(null);
    }
}
//----------------------------
class Planet
{
    public String name;
    public double radius;
    public int moons;
    public boolean gaseous;
    public Color color;
    //------------------------
    public Planet(String name,double radius,int moons,boolean gaseous,Color color)
    {
        this.name=name;
        this.radius=radius;
        this.moons=moons;
        this.gaseous=gaseous;
        this.color=color;
    }
}
//----------------------------
class PlanetTableFrame extends JFrame
{
    public PlanetTableFrame()
    {
        TableModel model=new PlanetsModel();
        JTable table = new JTable(model);
        table.setAutoCreateRowSorter(true);
        add(new JScrollPane(table), BorderLayout.CENTER);

        JButton printButton = new JButton("Print");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(printButton);
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
    }


    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 200;
}

class PlanetsModel extends AbstractTableModel
{
    private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" };
    private Class[] columnClasses={String.class,Double.class,Integer.class,Boolean.class,String.class};
    private ArrayList<Planet> planets=new ArrayList<Planet>();
    //------------------------
    public PlanetsModel()
    {
        planets.add(new Planet("Mercury", 2440.0, 0, false, Color.YELLOW ));
        planets.add(new Planet("Venus", 6052.0, 0, false, Color.YELLOW));
        planets.add(new Planet("Earth", 6378.0, 1, false, Color.BLUE));
        planets.add(new Planet("Mars", 3397.0, 2, false, Color.RED));
        planets.add(new Planet("Jupiter", 71492.0, 16, true, Color.ORANGE));
        planets.add(new Planet("Saturn", 60268.0, 18, true, Color.ORANGE));
        planets.add(new Planet("Uranus", 25559.0, 17, true, Color.BLUE));
        planets.add(new Planet("Neptune", 24766.0, 8, true, Color.BLUE));
    }
    //------------------------
    public int getColumnCount()
    {
        return columnNames.length;
    }
    //------------------------
    public int getRowCount()
    {
        return planets.size();
    }
    //------------------------
    public Object getValueAt(int row,int col)
    {
        Planet p=planets.get(row);
        switch(col)
        {
            case 0:
                 return p.name;
            case 1:
                 return p.radius;
            case 2:
                 return p.moons;
            case 3:
                 return p.gaseous;
            case 4:
                 return p.color;
        }
        return null;
    }
    //------------------------
    public Class getColumnClass(int col)
    {
    	return columnClasses[col];
    }
    //------------------------
    public String getColumnName(int col)
    {
    	return columnNames[col];
    }
    //------------------------
    public boolean isCellEditable(int row,int col)
    {
        return false;
    }
}
0

co wy tego Plutona tak nie lubicie. zniknęło go z nieba? ;-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1