代码正在显示而不是组件

时间:2012-11-12 14:29:49

标签: java swing jtable jlabel jcheckbox

请查看以下代码

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TestSend extends JFrame 
    {
        private Box names, emails;
        private JButton ok;
        private Map mMap;
        private JLabel nameLabel, emailLabel;
        private JPanel mainPanel;
        private JScrollPane scroll;
        private JTable table;
        private Object[][] data;
        private int counter = 0;
        private List nameHolder;

        private String[] colNames = {"Names","Emails"};

        public TestSend()
        {
            nameHolder = new ArrayList();

            names = new Box(BoxLayout.Y_AXIS);
            emails = new Box(BoxLayout.Y_AXIS);

            nameLabel = new JLabel("Names");
            emailLabel = new JLabel("Email");

            mainPanel = new JPanel();
            mainPanel.setLayout(new GridLayout(2,2));

            scroll = new JScrollPane(mainPanel);

            mainPanel.add(nameLabel);
            mainPanel.add(emailLabel);
            mainPanel.add(names);
            mainPanel.add(emails);

            mMap = new HashMap();

            mMap.put("yohan", "yy@yahoo.com");
            mMap.put("Gihan", "gihan@yahoo.com");
            mMap.put("Sumi", "sumi@yahoo.com");
            mMap.put("mac", "mac@yahoo.com");
            mMap.put("Jay", "jay@yahoo.com");
            mMap.put("Rom", "rom@yahoo.com");
            mMap.put("shane", "shane@yahoo.com");
            mMap.put("Mafe", "mafe@yahoo.com");
            mMap.put("willi", "");


            data = new Object[mMap.size()][mMap.size()];

            Iterator iter = mMap.entrySet().iterator();



            while(iter.hasNext())
            {
                Map.Entry mEntry = (Map.Entry)iter.next();

                JCheckBox cBox = new JCheckBox((String)mEntry.getKey());
                JLabel lLabel = new JLabel();

                names.add(cBox);
                String cName = cBox.getText();

                nameHolder.add(cName);

                if((String)mEntry.getValue() != null && ((String)mEntry.getValue()).length() != 0  && !((String)mEntry.getValue()).equals(""))
                {
                    lLabel = new JLabel((String)mEntry.getValue());
                   // lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height));
                    emails.add(lLabel);
                    emails.add(new JPanel());

                }
                else
                {
                    lLabel = new JLabel();
                    //lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height));
                    emails.add(lLabel);
                    emails.add(new JPanel());
                }

                data[counter][0] = cBox;
                data[counter][1] = lLabel;

                counter++;

            }

            table = new JTable(data,colNames);

            this.add(new JScrollPane(table));
            this.pack();
            this.setVisible(true);

        }

        public static void main(String[]args)
        {
            new TestSend();
        }
    }

这里当我运行它时,显示的是代码,而不是JCheckBox或JLabel。情境显示在附件中。请帮忙

enter image description here

2 个答案:

答案 0 :(得分:9)

您正在使用JTable 从根本上错误。您不会将Component添加到Model显示的JTable。您的Model包含JTable使用Renderer来显示这些值。由于您的值为Component s(未注册特定渲染器的类型),因此使用默认渲染器:toString。这不是代码,而是组件的String表示。

请查看the JTable tutorial。你需要从头开始。

答案 1 :(得分:1)

更改:

...
data[counter][0] = cBox;
data[counter][1] = lLabel;
....

要:

....
data[counter][0] = cBox.getText();
data[counter][1] = lLabel.getText();
.....

enter image description here