0

I want to get the name of components on panel, and add this code:

package mvc.view;

import mvc.DAO.MusBandDAO;
import mvc.DAO.MusBandDAOImpl;
import mvc.model.MusBand;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

public class SecondView extends JFrame {
JPanel bot = new JPanel();

public SecondView() {
    setTitle("Test");
    setSize(900, 600);
    setResizable(false);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    bot.setName("bot");
    bot.setLayout(new BorderLayout());

    JTextArea textArea = new JTextArea();
    textArea.setName("groupList");
    JTextArea textArea1 = new JTextArea();
    textArea.setName("groupList1");
    JTextArea textArea2 = new JTextArea();
    textArea.setName("groupList2");

    JPanel panela=new JPanel();
    panela.add(textArea);
    panela.add(textArea1);
    panela.add(textArea2);
    getContentPane().add(panela);

    Component[] components = panela.getComponents();
    for (int i = 0; i < components.length; i++)
            System.out.println(">> "  + components[i].getName());
}
}

But isn't work, just write in console next:

>>0 groupList2

>>1 null

>>2 null The name of last added element is true, but for other just null.. What problem? Thanks

badCoder
  • 111
  • 1
  • 16
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). The first thing you need to do to post an MCVE is remove the non-J2SE imports/dependency. Then add a `main(String[])` so we can see it on-screen. – Andrew Thompson Feb 18 '15 at 23:18
  • 1
    BTW - `public class SecondView extends JFrame {` Is there a `public class FirstView extends JFrame {`? See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Feb 18 '15 at 23:19

1 Answers1

4

You are setting the name only for textArea. For all three components you are using the same statement: textArea.setName("groupList"); Change it to:

JTextArea textArea = new JTextArea();
textArea.setName("groupList");
JTextArea textArea1 = new JTextArea();
textArea1.setName("groupList1");
JTextArea textArea2 = new JTextArea();
textArea2.setName("groupList2");
tenorsax
  • 21,123
  • 9
  • 60
  • 107