How can I create a grid in java, that allows me to auto-filter the data as in dev express?

2

I would like to know if there is any alternative to grid view in java, which allows me to perform several functions as auto-filters, or to customize to the maximum in a less tedious way. Thanks

    
asked by mymikemine 14.07.2016 в 16:47
source

1 answer

2

Being a desktop app and not specifying that you use let's say you have , the standard (for now) which comes with java and netbeans.

For this you need a JTable with RowSorter to perform the ordination, apart we will use:

First the data to show (this in your application can be done dynamically collecting data from the database)

// titulos
private final static String nombresDeColumna[] = 
        { "Nombre", "Apellido", "Edad" };

// datos
private final static Object[][] datos = { 
        { "Jordi", "Castilla", 35 },
        { "Mymike", "Mine", 25 }, 
        { "Luiggi", "Mendoza", 40 },
        { "Super", "Falete", 60 } };

Now the class main . To avoid rolling up and be easier to understand I will comment between lines

// creamos el modelo con los datos 
TableModel modelo = new DefaultTableModel(datos, nombresDeColumna) {
    private static final long serialVersionUID = 1L;
    public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }
};

// creamos la Table basados en el modelo de datos que hemos creado
JTable table = new JTable(modelo);

// ordenacion de filas (por defecto, al ser tipos primitivos)
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(modelo);
table.setRowSorter(sorter);

// creamos un scroll y le añadomos la tabla
JScrollPane scrollPane = new JScrollPane(table);

// contenedor principal 
JFrame frame = new JFrame("Tabla con filtros");
// le decimos que cierre 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// añadimos la tabla
frame.add(scrollPane);
// tamaño
frame.setSize(500, 150);
// mostramos
frame.setVisible(true);

The result, as you can see, is a table with orderly and interchangeable columns

IDEONE sample code
(You can not run it online since it does not have a graphics engine)

NOTES:

  • Imports needed

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
  • I hope no one is offended by age :P hehehehe
  • If you have questions, contact me :) .
answered by 15.07.2016 в 10:17