Há alguns meses atrás foi feita uma pergunta na lista do genesis cuja resposta envolvia saber como determinar a ordem dos componentes numa interface Swing.
Quando falamos de ordem do ponto de vista do usuário, não necessariamente falamos da ordem em profundidade, que refletiria a “árvore” dos componentes em determinado container, mas sim a ordem de foco. Existe uma interface que foi adicionada no Java 1.4 chamada FocusTraversalPolicy
, que serve justamente para isso. Abaixo um exemplo de como determinar a ordem usando os nomes de componentes dados por quem fez a pergunta:
package br.com.michaelnascimento.test; import java.awt.Component; import java.awt.EventQueue; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.WindowConstants; import org.jdesktop.layout.GroupLayout; import org.jdesktop.layout.LayoutStyle; public class ComponentOrder extends JFrame { public ComponentOrder() { initComponents(); } private void findOrder() { List<Component> components = Arrays.asList(new Component[] {login, senha, endereco, empresa}); Collections.shuffle(components); for (Component component : components) { System.out.println(component.getName()); } Map<Component, Integer> componentPerPosition = new HashMap<Component, Integer>(); Component c = getFocusTraversalPolicy().getFirstComponent(this); int position = 0; while (componentPerPosition.size() < components.size()) { if (c == null) { break; } if (components.contains(c)) { componentPerPosition.put(c, position); } position++; c = getFocusTraversalPolicy().getComponentAfter(this, c); } for (Component component : components) { System.out.println(component.getName() + ": " + componentPerPosition. get(component)); } } // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new JPanel(); login = new JTextField(); senha = new JTextField(); endereco = new JTextField(); empresa = new JTextField(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jPanel1.setName("jPanel1"); // NOI18N login.setText("jTextField1"); login.setName("login"); // NOI18N senha.setText("jTextField1"); senha.setName("senha"); // NOI18N GroupLayout jPanel1Layout = new GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jPanel1Layout.createParallelGroup(GroupLayout.LEADING) .add(login, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .add(senha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addContainerGap(31, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(login, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.RELATED) .add(senha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addContainerGap(43, Short.MAX_VALUE)) ); endereco.setText("jTextField1"); endereco.setName("endereco"); // NOI18N empresa.setText("jTextField1"); empresa.setName("empresa"); // NOI18N GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(GroupLayout.LEADING) .add(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .add(endereco, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .add(empresa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addContainerGap(290, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.RELATED) .add(endereco, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.RELATED) .add(empresa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addContainerGap(137, Short.MAX_VALUE)) ); pack(); }// </editor-fold> public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { final ComponentOrder co = new ComponentOrder(); co.setVisible(true); co.findOrder(); } }); } // Variables declaration - do not modify private JTextField empresa; private JTextField endereco; private JPanel jPanel1; private JTextField login; private JTextField senha; // End of variables declaration }