Java swing:无法从JTable打开弹出框架

时间:2016-05-27 00:25:14

标签: java swing jtable

我目前的项目是简单的电子邮件客户端。现在我已完成主窗口,其中包含来自收件箱的邮件列表。下一步是打开新窗口,其中包含点击Jtable和消息列表的消息。 但是当我点击表格中的行时,我得到了这个例外:

22
Test problem
"Alb." <test@gmail.com>
Hello 

My PC is not working

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:4 >= 4
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at sun.swing.SwingUtilities2.convertColumnIndexToModel(SwingUtilities2.java:1896)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2582)
    at javax.swing.JTable.getValueAt(JTable.java:2717)
    at CheckEmail$1.mouseClicked(CheckEmail.java:129)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6519)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这是我的代码:

import java.awt.BorderLayout;
import org.apache.commons.codec.binary.Base64;
import java.awt.Dimension;
import java.util.*;
import javax.mail.*;
import javax.swing.*;
import javax.swing.table.*;
import org.apache.commons.codec.binary.Base64;
import java.awt.*;
import java.awt.event.*;

public class CheckEmail {

     static Object[][] mess = new Object[][]{};
     JTextField textMessage = null;
     String text = null;
     static Object messi = null;

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {

      Properties properties = new Properties();

      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);

      Store store = emailSession.getStore("pop3s");

      store.connect(host, user, password);

      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      Message[] messages = emailFolder.getMessages();
      JFrame frame = new JFrame("Main");
      JPanel panel = new JPanel();
      final String data[][] = null;
      String [] col = {"num","Subject","From", "Text"};
    DefaultTableModel model = new DefaultTableModel(data, col);

      final JTable table = new JTable(model);
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.getColumnModel().getColumn(0).setPreferredWidth(30);
     table.getColumnModel().getColumn(1).setPreferredWidth(400);
     table.getColumnModel().getColumn(2).setPreferredWidth(400);
     table.getColumnModel().getColumn(3).setPreferredWidth(1); 
     table.setSize(830, 600);

      for (int i = 0, n = messages.length; i < n; i++) {
          Message message = messages[i];

        int num = i + 1; 
        String subject =  message.getSubject();
        String from = message.getFrom()[0].toString();
        String text = message.getContent().toString();

        Object[] mess = new Object[]{num, subject, from, text};

        model.insertRow(i, mess);
      }

      panel.add(table);

     JScrollPane scrollPane = new JScrollPane(table);
      frame.add(scrollPane, BorderLayout.CENTER);
      frame.setSize(830, 600);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      emailFolder.close(false);
      store.close();

      table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(final MouseEvent e) {
            if (e.getClickCount() == 1) {

                final JTable target = (JTable)e.getSource();
                 int row = target.getSelectedRow();
                 int column = target.getSelectedRow();

                for(int i = 0; i < column; i++) {
                Object mess = (Object)target.getValueAt(row, i);
               System.out.println(target.getValueAt(row, i));
                }

                StringBuffer sb = new StringBuffer();

                    sb.append(mess);
                   TextFrame textFrame = new TextFrame(sb.toString());
                   textFrame.setVisible(true);
            }
        }
    });

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static void main(String[] args) {

      String host = "pop3.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "test@gmail.com";// change accordingly
      String password = "pass";// change accordingly
      check(host, mailStoreType, username, password);
}
}

和第二节课:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;

class TextFrame extends JFrame
{
   public TextFrame(String content) {
      super("TextFrame");

      JTextArea ta = new JTextArea();
      ta.setText(content);
      getContentPane().add(ta);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            dispose();
         }
      });

      setSize(600, 400);
   }
}

1 个答案:

答案 0 :(得分:1)

你看到了问题吗?:

int column = target.getSelectedRow();

具体而言,当您打算使用getSelectedRow()时,您正在使用getSelectedColumn()。例如,它应该是:

int column = target.getSelectedColumn();

这个练习的关键是不要忽略堆栈跟踪,而是要严格审视它告诉你要看的那一行,在这里:at CheckEmail$1.mouseClicked(CheckEmail.java:129)