2016-05-27 67 views
-2

我目前的項目是簡單的電子郵件客戶端。現在我完成了主窗口和來自收件箱的消息列表。下一步是打開一個新窗口,點擊帶有消息列表的Jtable。 但在表格上一行時點擊即時得到此異常:Java swing:無法從JTable打開彈出框架

22 
Test problem 
"Alb." <[email protected]> 
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 = "[email protected]";// 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); 
    } 
} 
+0

投票爲關閉印刷錯誤,而不是真正的編程邏輯錯誤。 –

回答

1

你看到一個問題嗎? :

int column = target.getSelectedRow(); 

具體而言,當您打算使用getSelectedColumn()時,您正在使用getSelectedRow()。例如,它應該是:

int column = target.getSelectedColumn(); 

到這項工作的關鍵是不能忽視的堆棧跟蹤,而是批判地看,它會告訴你看看行了,在這裏:at CheckEmail$1.mouseClicked(CheckEmail.java:129)

+0

是的,這是我的錯。現在程序運行沒有例外。謝謝 – Hosalone