Activate JButton from selection in JTable and remove the initial focus from the table

1

I have a window with a JTable , where only the selection of an entire row is valid, not just one field. The issue is that the window already opens with the first line selected, and I would like to disable it.

In this same window I have a JButton that is already disabled at the time of its creation, however I would like to activate it as soon as the user selects any line of JTable ...

In short: How do I disable automatic selection of the JTable line at the time the window opens? How to activate a button when the user selects any line of this same JTable ?

    
asked by anonymous 24.04.2016 / 21:19

2 answers

2

To avoid automatic selection, you need to change the focus of the table when opening the window.

If your class inherits from JFrame :

this.requestFocus();

If you start a window from a variable:

meuFrame.requestFocus();

requestFocus() will change the focus to its main window, thus preventing JTable from appearing with the first line selected.

To activate the button only when there is a selection, try the below:

this.tabela.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                //altera os botoes para ativados somente se houver linha selecionada
                myButton.setEnabled(!lsm.isSelectionEmpty());
            }
        });

This way button activation is dependent on whether or not there is any selected field / row in the table.

    
25.04.2016 / 03:10
0
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

class SelectTableExample
        extends     JFrame
        implements  ListSelectionListener
{
    // Instance attributes used in this example
   private  JPanel      topPanel;
   private  JTable      table;
   private  JScrollPane scrollPane;
   private  JButton     button;

    // Constructor of main frame
   public SelectTableExample()
   {
    // Set the frame characteristics
      setTitle( "Simple Table Application" );
      setSize( 300, 200 );
      setBackground( Color.gray );

    // Create a panel to hold all other components
      topPanel = new JPanel();
      topPanel.setLayout( new BorderLayout() );
      getContentPane().add( topPanel );

    // Create columns names
      String columnNames[] = { "Column 1", "Column 2", "Column 3" };

    // Create some data
      String dataValues[][] =
         {
         { "12", "234", "67" },
         { "-123", "43", "853" },
         { "93", "89.2", "109" },
         { "279", "9033", "3092" }
         };

    // Create a new table instance
      table = new JTable( dataValues, columnNames );

      // Block cell edition
      table.setModel(
            new DefaultTableModel(dataValues, columnNames) {
               public boolean isCellEditable(int row, int column) { 
                  return false;
               }
            });

        // Handle the listener
      ListSelectionModel selectionModel = table.getSelectionModel();
      selectionModel.addListSelectionListener( this );

    // Add the table to a scrolling pane
      scrollPane = new JScrollPane( table );
      topPanel.add( scrollPane, BorderLayout.CENTER );

      button = new JButton("continuar");
      button.setEnabled(false);
      topPanel.add( button, BorderLayout.SOUTH );
   }

    // Enable button for list selection changes
   public void valueChanged( ListSelectionEvent event )
   {
    // See if this is a valid table selection
      if( event.getSource() == table.getSelectionModel()
                    && event.getFirstIndex() >= 0 )
      {
         button.setEnabled(true);
      }
   }

    // Main entry point for this example
   public static void main( String args[] )
   {
    // Create an instance of the test application
      SelectTableExample mainFrame  = new SelectTableExample();
      mainFrame.setVisible( true );
   }
}
    
25.04.2016 / 02:47