2016-05-27 18 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

投票をのように閉じるために:

int column = target.getSelectedColumn(); 

この演習への鍵はここに、スタックトレースを無視しないように、それはを見てわかりますラインで批判的に見ることです誤植であり、真のプログラミング論理エラーではない。 –

答えて

1

は、あなたが問題を参照していますか? :

int column = target.getSelectedRow(); 

具体的にはgetSelectedColumn()を使用するときはgetSelectedRow()を使用しています。例えば、それは次のようになります。at CheckEmail$1.mouseClicked(CheckEmail.java:129)

+0

はい、これは私のせいです。プログラムは例外なく実行されます。ありがとうございました – Hosalone

関連する問題