Duvida com JTree

2 respostas
L

Bom dia pessoal!

Estou tentando utilizar o componente Jtree. Ja consegui montar minha tree do jeito que desejava, mas agora qria adicionar “links” nos nodes do menu.

Por exemplo: se ele clica na opção Cadastrar usuario, eu carregaria a tela que realiza essa função…

Obrigado!!

[]'s

2 Respostas

L

Usando o JTree, segue um "exemplo" de como poderia ficar. Porém, recomendo que você utilize um JMenu para isso...

public static void main(String[] args) {
		final JPanel jp = new JPanel(new FlowLayout());
		final JPanel telaA = new JPanel();
		telaA.add(new JLabel("Tela A"));
		final JPanel telaB = new JPanel();
		telaB.add(new JLabel("Tela B"));
		final JPanel telaC = new JPanel();
		telaC.add(new JLabel("Tela C"));

		// Chave -> Tela
		final Hashtable<String, JPanel> telas = new Hashtable<String, JPanel>();
		telas.put("A", telaA);
		telas.put("B", telaB);
		telas.put("C", telaC);

		JTree jTree = new JTree(telas);
		jp.add(jTree);

		final JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jp,
				telaA);

		jTree.addTreeSelectionListener(new TreeSelectionListener() {

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				TreePath tp = e.getNewLeadSelectionPath();

				JTree.DynamicUtilTreeNode novoNo = (DynamicUtilTreeNode) tp
						.getLastPathComponent();
				jsp.setRightComponent(telas.get(novoNo.getUserObject()));
				jsp.validate();
			}
		});

		JFrame jf = new JFrame("Demo");
		jf.add(new JScrollPane(jsp));

		jf.pack();
		jf.setSize(400, 400);
		jf.setVisible(true);

		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}
ViniGodoy

Tópico movido para o fórum de interface gráfica.

Criado 16 de maio de 2011
Ultima resposta 19 de mai. de 2011
Respostas 2
Participantes 3