1

How can i make a selection that is possible user select cell by cell without interverval?

How i get row and column position and store in _dvPosicoes = new double[7][7].

Number 0 done in excel

I've already used:

  1. setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  2. setColumnSelectionAllowed(false);
  3. setRowSelectionAllowed(false);
  4. Override method Component getTableCellRendererComponent

I tried this:

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

public class UserTable extends JPanel {

// Propriedades.
// Define o tamanho do Grid.
private static final int TAMANHOGRID = 7;

// Quarda os valores das posíções das células clícadas pelo Usuário.
private double[][] _dvPosicoes = new double[7][7];

// Possibilita a mudança das configurações do elemento tipo lista.
public DefaultListModel _dlmListaUsuario = new DefaultListModel();
public JList _listaUsuario = new JList(_dlmListaUsuario);
public JButton _btnVerificar = new JButton("Verificar");
public JLabel _lblTitulo = new JLabel("Desenhe o Dígito:");

public UserTable() {

    super(new FlowLayout(FlowLayout.CENTER, 10, 10));

    // Configurações da Lista.        
    _listaUsuario.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    _listaUsuario.setLayoutOrientation(JList.VERTICAL_WRAP);
    _listaUsuario.setVisibleRowCount(7);
    _listaUsuario.setVisibleRowCount(TAMANHOGRID);
    _listaUsuario.setCellRenderer(new RendenrizarLista());
    _listaUsuario.addListSelectionListener(new SelecionarHandler());

    /*int start = 0;
    int end = _listaUsuario.getModel().getSize() - 1;
    if (end >= 0) {
        _listaUsuario.setSelectionInterval(start, end);
    }*/
    System.out.println(_listaUsuario.getSelectedValuesList());

    // Mostra os valores das posições x e y das células clicadas pelo Usuário.
    for (int _iContadorI = 0; _iContadorI < _dvPosicoes.length; _iContadorI++) {
        for (int _iContadorJ = 0; _iContadorJ < _dvPosicoes[_iContadorI].length; _iContadorJ++) {

            _dvPosicoes[_iContadorI][_iContadorJ] = _iContadorI;

            //double _strPosPressionada = _dvPosicoes[_iContadorI][_iContadorJ];
            String _strPosPressionada = "X";

            // Muda a forma de como é mostrado posição de cada célula do Grid.
            _dlmListaUsuario.addElement(_strPosPressionada);

            //System.out.println(_dlmListaUsuario.getElementAt((int)_dvPosicoes[_iContadorI][_iContadorJ]));
        }

    }



    // Adiciona os Componentes.
    //this.add(_lblTitulo);
    this.add(_btnVerificar);
    this.add(_listaUsuario);
}

// Mudar as configurações de cada célula do grid.
private class RendenrizarLista extends DefaultListCellRenderer {

    // Set
    public RendenrizarLista() {
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 10, true));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        JComponent component = (JComponent) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        component.setBorder(BorderFactory.createEmptyBorder((int) TAMANHOGRID, (int) TAMANHOGRID, (int) TAMANHOGRID, (int) TAMANHOGRID));

        component.setBorder(BorderFactory.createEmptyBorder(TAMANHOGRID, TAMANHOGRID, TAMANHOGRID, TAMANHOGRID));
        return component;
    }

}

private class SelecionarHandler implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent e) {

        if (!e.getValueIsAdjusting()) {



            //System.out.println(_listaUsuario.getSelectedValuesList());
            //System.out.println(_dvPosicoes);

        }

    }

}

void mostrarJanelaPrincipal() {
    JFrame frmPrincipal = new JFrame("Reconhecedor de Dígitos");
    frmPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmPrincipal.setLocationRelativeTo(null);
    frmPrincipal.setResizable(false);
    frmPrincipal.add(this);
    frmPrincipal.pack();
    frmPrincipal.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        new UserTable().mostrarJanelaPrincipal();
    });
}
}
João Neto
  • 85
  • 1
  • 7

1 Answers1

1

ListSelectionModel.MULTIPLE_INTERVAL_SELECTION is also available to JList, where you can use HORIZONTAL_WRAP to arrange for arbitrary cell selection as shown here.

image

How I get position row and column and store in a matrix[N][N]?

As shown here, you can interconvert grid coordinates and list coordinates for a given array stride:

int index = row * N + col;

int row = index / N;
int col = index % N;
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045