2013-01-24 2 views
-1

데이터베이스에서 데이터를 검색하고 scrollPane과 함께 textArea를 사용하여 표시하려고했습니다.jScrollPane 계속 하단에 스냅

public class eForumThreadContent { 

private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="74,36" 
private JPanel jContentPane = null; 
private JLabel jLoginLink = null; 
private JTextField jTextField_search = null; 
private JButton jButtonSearch = null; 
private JLabel jLabel_sessionborder = null; 
private JButton jButton_home = null; 
private JLabel jLabel_searchBackgroundColor = null; 
private JButton jButton_signOut = null; 
private JButton jButton_postNewThread = null; 
private JButton jButton_back = null; 
private JLabel jLabel_replyBy = null; 
private JLabel jLabel_username = null; 
private JLabel jLabel_reply = null; 
private JTextArea jTextArea_reply = null; 
private JButton jButton_postReply = null; 
private JTable jTableComment = null; 
private JScrollPane jScrollPaneComment = null; 
private JButton jButton_bookmark = null; 
private JButton jButton_myAcc = null; 
private String userName; // @jve:decl-index=0: 
DBController db = new DBController(); // @jve:decl-index=0: 
private int topicId; 
private JLabel jLabel_topicTitle = null; 
private JLabel jLabel_topicBy = null; 
private JLabel jLabel_separator = null; 
private JLabel jLabel_title = null; 
private JScrollPane jScrollPane_areaScrollPane = null; 
private JTextArea jTextArea_content = null; 
private JScrollPane jScrollPane_descContent = null; 
/** 
* This method initializes jFrame 
* 
* @return javax.swing.JFrame 
*/ 
JFrame getJFrame() { 
    if (jFrame == null) { 
     jFrame = new JFrame(); 
     jFrame.setSize(new Dimension(946, 762)); 
     jFrame.setContentPane(getJContentPane()); 
     jFrame.setTitle("eForum"); 
    } 
    return jFrame; 
} 

/** 
* This method initializes jContentPane 
* 
* @return javax.swing.JPanel 
*/ 
private JPanel getJContentPane() { 
    if (jContentPane == null) { 
     jLabel_title = new JLabel(); 
     jLabel_title.setBounds(new Rectangle(75, 167, 77, 51)); 
     jLabel_title.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLabel_title.setHorizontalAlignment(SwingConstants.LEFT); 
     jLabel_title.setText("Title : "); 
     jLabel_separator = new JLabel(); 
     jLabel_separator.setBounds(new Rectangle(73, 278, 784, 8)); 
     jLabel_separator.setBackground(new Color(102,102,102)); 
     jLabel_separator.setOpaque(true); 
     jLabel_topicBy = new JLabel(); 
     jLabel_topicBy.setBounds(new Rectangle(761, 217, 175, 41)); 
     jLabel_topicBy.setFont(new Font("Dialog", Font.PLAIN, 18)); 
     jLabel_topicTitle = new JLabel(); 
     jLabel_topicTitle.setBounds(new Rectangle(161, 167, 757, 50)); 
     jLabel_topicTitle.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLabel_reply = new JLabel(); 
     jLabel_reply.setBounds(new Rectangle(106, 528, 132, 37)); 
     jLabel_reply.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLabel_reply.setText("Comment"); 
     jLabel_username = new JLabel(); 
     jLabel_username.setBounds(new Rectangle(256, 483, 315, 32)); 
     jLabel_username.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLabel_username.setText(userName); 
     jLabel_replyBy = new JLabel(); 
     jLabel_replyBy.setBounds(new Rectangle(106, 481, 137, 38)); 
     jLabel_replyBy.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLabel_replyBy.setText("Reply by"); 
     jLabel_searchBackgroundColor = new JLabel(); 
     jLabel_searchBackgroundColor 
       .setBounds(new Rectangle(2, 64, 930, 44)); 
     jLabel_searchBackgroundColor 
       .setBackground(new Color(102, 102, 102)); 
     jLabel_searchBackgroundColor.setOpaque(true); 
     jLabel_sessionborder = new JLabel(); 
     jLabel_sessionborder.setBounds(new Rectangle(-1, 40, 930, 25)); 
     jLabel_sessionborder.setBorder(BorderFactory.createMatteBorder(0, 
       0, 1, 0, Color.black)); 
     jLabel_sessionborder.setText(""); 
     jLoginLink = new JLabel(); 
     jLoginLink.setBounds(new Rectangle(34, 34, 259, 25)); 
     jLoginLink.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0, 
       Color.black)); 
     jLoginLink.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jLoginLink.setText("Logged in as " + userName); 
     jContentPane = new JPanel(); 
     jContentPane.setLayout(null); 
     jContentPane.setBackground(Color.white); 
     jContentPane.add(jLoginLink, null); 
     jContentPane.add(getJTextField_search(), null); 
     jContentPane.add(getJButtonSearch(), null); 
     jContentPane.add(jLabel_sessionborder, null); 
     jContentPane.add(getJButton_home(), null); 
     jContentPane.add(jLabel_searchBackgroundColor, null); 
     jContentPane.add(getJButton_signOut(), null); 
     jContentPane.add(getJButton_postNewThread(), null); 
     jContentPane.add(getJButton_back(), null); 
     jContentPane.add(jLabel_replyBy, null); 
     jContentPane.add(jLabel_username, null); 
     jContentPane.add(jLabel_reply, null); 
     jContentPane.add(getJTextArea_reply(), null); 
     jContentPane.add(getJButton_postReply(), null); 
     jContentPane.add(getJScrollPaneComment(), null); 
     jContentPane.add(getJButton_bookmark(), null); 
     jContentPane.add(getJButton_myAcc(), null); 
     jContentPane.add(jLabel_topicTitle, null); 
     jContentPane.add(jLabel_topicBy, null); 
     jContentPane.add(jLabel_separator, null); 
     jContentPane.add(jLabel_title, null); 
     jContentPane.add(getJScrollPane_areaScrollPane(), null); 
     jContentPane.add(getJTextArea_content(), null); 
     jContentPane.add(getJScrollPane_descContent(), null); 
     SetUpTopicDetails(); 
    } 
    return jContentPane; 
} 

/** 
* This constructor take in the userName from eForumLogin page. The username 
* will be display in jLoginLink. 
* 
* @param userName 
*/ 
public eForumThreadContent(String userName, int topicId) { 
    this.userName = userName; 
    this.topicId = topicId; 

} 

/** 
* This method initializes jTextField_search 
* 
* @return javax.swing.JTextField 
*/ 
private JTextField getJTextField_search() { 
    if (jTextField_search == null) { 
     jTextField_search = new JTextField(50); 
     jTextField_search.setDocument (new JTextFieldLimit(50)); 
     jTextField_search.setBounds(new Rectangle(500, 75, 285, 29)); 
     jTextField_search.setFont(new Font("Dialog", Font.PLAIN, 24)); 
    } 
    return jTextField_search; 
} 

/** 
* This method initializes jButtonSearch submitSearch is called when this 
* button is on click. 
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonSearch() { 
    if (jButtonSearch == null) { 
     jButtonSearch = new JButton(); 
     jButtonSearch.setBounds(new Rectangle(797, 73, 114, 28)); 
     jButtonSearch.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButtonSearch.setText("Search"); 
     jButtonSearch 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         submitSearch(e); 
        } 
       }); 
    } 
    return jButtonSearch; 
} 


public void submitSearch(ActionEvent e) { 
    String search = jTextField_search.getText(); 
    if (search.equals("")) { 
     JOptionPane.showMessageDialog(null, 
       "Please enter keyword to search"); 
    } else { 
     eForumSearch searchResult = new eForumSearch(userName, search); 
     if (searchResult.searchKeyword() == true) { 
      getJFrame().dispose(); 
      eForumSearchResult myWindow = new eForumSearchResult(userName, 
        search); 
      myWindow.getJFrame().setVisible(true); 
     } else { 
      JOptionPane.showMessageDialog(null, "No results found"); 
     } 
    } 
} 

/** 
* This method initializes jButton_home 
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButton_home() { 
    if (jButton_home == null) { 
     jButton_home = new JButton(); 
     jButton_home.setBounds(new Rectangle(819, 2, 94, 60)); 
     jButton_home.setBackground(Color.white); 
     jButton_home.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
     jButton_home.setIcon(new ImageIcon(getClass().getResource(
       "/Home.png"))); 
     jButton_home 
     .addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent e) { 
       getJFrame().dispose(); 
       kioskHomepage myWindow = new kioskHomepage(); 
       myWindow.getJFrame().setVisible(true); 
      } 
     }); 
    } 
    return jButton_home; 
} 

private JButton getJButton_signOut() { 
    if (jButton_signOut == null) { 
     jButton_signOut = new JButton(); 
     jButton_signOut.setBounds(new Rectangle(297, 35, 139, 24)); 
     jButton_signOut.setText("Sign out"); 
     jButton_signOut.setBorderPainted(false); 
     jButton_signOut.setOpaque(false); 
     jButton_signOut.setHorizontalAlignment(SwingConstants.LEFT); 
     jButton_signOut.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_signOut.setBackground(Color.lightGray); 
     jButton_signOut 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         getJFrame().dispose(); 
         eForumSignOut myWindow = new eForumSignOut(userName); 
         myWindow.getJFrame().setVisible(true); 

        } 
       }); 
    } 
    return jButton_signOut; 
} 

private JButton getJButton_postNewThread() { 
    if (jButton_postNewThread == null) { 
     jButton_postNewThread = new JButton(); 
     jButton_postNewThread.setBounds(new Rectangle(682, 118, 230, 36)); 
     jButton_postNewThread.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_postNewThread.setText("Post New Thread"); 
     jButton_postNewThread 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         getJFrame().dispose(); 
         eForumPostThread myWindow = new eForumPostThread(
           userName); 
         myWindow.getJFrame().setVisible(true); 
        } 
       }); 
    } 
    return jButton_postNewThread; 
} 


public void SetUpTopicDetails() { 
    db.setUp("IT Innovation Project"); 
    String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = " 
      + topicId + ""; 
    ResultSet resultSet = null; 
    resultSet = db.readRequest(sql); 
    try { 
     while (resultSet.next()) { 
      jLabel_topicTitle.setText(resultSet.getString("topic_title")); 
      jTextArea_content.setText(resultSet.getString("topic_description")); 
      jLabel_topicBy.setText(resultSet.getString("topic_by")); 
     } 
     resultSet.close(); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

private JButton getJButton_back() { 
    if (jButton_back == null) { 
     jButton_back = new JButton(); 
     jButton_back.setBounds(new Rectangle(735, 679, 166, 36)); 
     jButton_back.setText("Back"); 
     jButton_back.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_back.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent e) { 
       getJFrame().dispose(); 
       eForumMainpage myWindow = new eForumMainpage(userName); 
       myWindow.getJFrame().setVisible(true); 
      } 
     }); 
    } 
    return jButton_back; 
} 

/** 
* This method initializes jTextArea_reply 
* 
* @return javax.swing.JTextArea 
*/ 
private JTextArea getJTextArea_reply() { 
    if (jTextArea_reply == null) { 
     jTextArea_reply = new JTextArea(); 
     jTextArea_reply.setBounds(new Rectangle(256, 525, 573, 130)); 
     jTextArea_reply.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jTextArea_reply.setLineWrap(true); 
    } 
    return jTextArea_reply; 
} 

private JButton getJButton_postReply() { 
    if (jButton_postReply == null) { 
     jButton_postReply = new JButton(); 
     jButton_postReply.setBounds(new Rectangle(567, 678, 155, 36)); 
     jButton_postReply.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_postReply.setText("Post reply"); 
     jButton_postReply 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         submitReply(e); 
        } 
       }); 
    } 
    return jButton_postReply; 
} 

public void submitReply(ActionEvent e) { 
    String replyBy = userName; 
    String reply = jTextArea_reply.getText(); 
    if (reply.equals("")) { 
     JOptionPane.showMessageDialog(null, "Comment cannot leave blank"); 
    } else { 
     eForumTopics comment = new eForumTopics(replyBy, reply); 
     if (comment.createComment() == true) { 
      JOptionPane 
        .showMessageDialog(null, 
          "Reply submitted successfully."); 
      SetUpJTableComment(); 
      jTextArea_reply.setText(""); 
     } 
    } 

} 

private JTable getJTableComment() { 
    String header[] = { "Comment", "Reply By" }; 
    if (jTableComment == null) { 
    DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment 
      .getModel(); 
    tableModel1.setColumnIdentifiers(header); 

    jTableComment.getColumnModel().getColumn(0).setMinWidth(685); 
    jTableComment.getColumnModel().getColumn(0).setMaxWidth(750); 

    jTableComment.getColumnModel().getColumn(1).setMinWidth(97); 
    jTableComment.getColumnModel().getColumn(1).setMaxWidth(100); 

    return jTableComment; 
} 

public void SetUpJTableComment() { 
    DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment 
      .getModel(); 
    String[] data = new String[3]; 
    db.setUp("IT Innovation Project"); 
    String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = " 
      + topicId + ""; 
    ResultSet resultSet = null; 
    resultSet = db.readRequest(sql); 
    tableModel1.getDataVector().removeAllElements(); 

    try { 
     while (resultSet.next()) { 
      data[0] = resultSet.getString("reply_content"); 
      data[1] = resultSet.getString("reply_by"); 
      tableModel1.addRow(data); 
     } 
     resultSet.close(); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

private JScrollPane getJScrollPaneComment() { 
    if (jScrollPaneComment == null) { 
     jScrollPaneComment = new JScrollPane(); 
     jScrollPaneComment.setBounds(new Rectangle(75, 290, 785, 175)); 
     jScrollPaneComment.setBorder(BorderFactory.createEmptyBorder()); 
     jScrollPaneComment.setViewportView(getJTableComment()); 
    } 
    SetUpJTableComment(); 
    return jScrollPaneComment; 
} 

private JButton getJButton_bookmark() { 
    if (jButton_bookmark == null) { 
     jButton_bookmark = new JButton(); 
     jButton_bookmark.setBounds(new Rectangle(487, 119, 186, 36)); 
     jButton_bookmark.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_bookmark.setText("Bookmark"); 
     jButton_bookmark 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         submitBookmark(e); 
        } 
       }); 
    } 
    return jButton_bookmark; 
} 

public void submitBookmark(ActionEvent e) { 
    String bookmarkBy = userName; 
    eForumTopics bookmark = new eForumTopics(bookmarkBy); 
    if (bookmark.bookmarkThread() == true) { 
     JOptionPane.showMessageDialog(null, "Thread has been bookmarked."); 
    } 
} 

private JButton getJButton_myAcc() { 
    if (jButton_myAcc == null) { 
     jButton_myAcc = new JButton(); 
     jButton_myAcc.setBounds(new Rectangle(427, 27, 175, 41)); 
     jButton_myAcc.setText("My account"); 
     jButton_myAcc.setBorderPainted(false); 
     jButton_myAcc.setOpaque(false); 
     jButton_myAcc.setHorizontalAlignment(SwingConstants.LEFT); 
     jButton_myAcc.setFont(new Font("Dialog", Font.PLAIN, 24)); 
     jButton_myAcc.setBackground(Color.lightGray); 
     jButton_myAcc 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent e) { 
         getJFrame().dispose(); 
         eForumMyAcc myWindow = new eForumMyAcc(userName); 
         myWindow.getJFrame().setVisible(true); 
        } 
       }); 
    } 
    return jButton_myAcc; 
} 

private JScrollPane getJScrollPane_areaScrollPane() { 
    if (jScrollPane_areaScrollPane == null) { 
     jScrollPane_areaScrollPane = new JScrollPane(jTextArea_reply); 
     jScrollPane_areaScrollPane.setBounds(new Rectangle(257, 527, 598, 125)); 

    } 
    return jScrollPane_areaScrollPane; 
} 

/** 
* This method initializes jTextArea_content  
* 
* @return javax.swing.JTextArea  
*/ 
private JTextArea getJTextArea_content() { 
    if (jTextArea_content == null) { 
     jTextArea_content = new JTextArea(); 
     jTextArea_content.setBounds(new Rectangle(75, 224, 678, 47)); 
    return jTextArea_content; 
} 

/** 
* This method initializes jScrollPane_descContent 
* 
* @return javax.swing.JScrollPane 
*/ 
private JScrollPane getJScrollPane_descContent() { 
    if (jScrollPane_descContent == null) { 
     jScrollPane_descContent = new JScrollPane(jTextArea_content); 
     jScrollPane_descContent.setBounds(new Rectangle(75, 224, 660, 47)); 
    } 
    return jScrollPane_descContent; 
} 

}

그러나, 스크롤 막대 대신 textArea의의 처음 몇 줄의 텍스트 영역의 하단에 스냅 유지 : 여기 내 코드입니다. 미리 감사드립니다.

+0

내가 그 동작을 복제 할 수 없습니다. 넌 다른 뭔가 잘못 했어. –

+0

하지만 잘못된 점을 지적 할 수 있습니까? 나는 이유를 전혀 모른다. – Newbies

+0

마지막 2 줄을 제외하고 코드를 복사했으며 필요에 따라 작동합니다. 그 외에도 다른 코드를 보지 않고 무엇을하고 있는지 추측 할 수 없습니다. –

답변

0

모두 내 코드입니다. 다른 건 없어. 코드의 나머지 부분은 이것과 관련이 없습니다. 그러나

, 내 SSCCE를 실행, 지금까지 우리가 제공 한 코드로, 그것은 마법처럼 작동합니다

import java.awt.Rectangle; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

public class TestTextArea3 { 

    private static final String TEXT = "This \r\n" + " Is\r\n" + " A\r\n" + " Testing\r\n" + " To\r\n" + " Test\r\n" 
      + " ScrollPane."; 

    private JTextArea jTextArea_content; 
    private JScrollPane jScrollPane_descContent; 

    private void initUI() { 
     JFrame frame = new JFrame(TestTextArea3.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     getJTextArea_content().setText(TEXT); 
     frame.add(getJScrollPane_descContent()); 
     frame.pack(); 
     frame.setVisible(true); 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       getJTextArea_content().scrollRectToVisible(new Rectangle()); 
      } 
     }); 
    } 

    private JTextArea getJTextArea_content() { 
     if (jTextArea_content == null) { 
      // Changed the number of rows so activate scrolling 
      jTextArea_content = new JTextArea(3, 30); 
      jTextArea_content.setBounds(new Rectangle(75, 224, 678, 47)); 
     } 
     return jTextArea_content; 
    } 

    private JScrollPane getJScrollPane_descContent() { 
     if (jScrollPane_descContent == null) { 
      jScrollPane_descContent = new JScrollPane(getJTextArea_content()); 
      jScrollPane_descContent.setBounds(new Rectangle(75, 224, 660, 47)); 
     } 
     return jScrollPane_descContent; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestTextArea3().initUI(); 
      } 
     }); 
    } 

} 
+0

내 문제는 모두 이해 했니? 또는 scrollpane 높이가있는 textArea는 두 행만 표시 할 수 있기 때문에 잘 설명하지 못했습니다. 그러나 처음 두 행 대신 주석의 마지막 두 행을 계속 표시합니다. – Newbies

+0

@Newbies 예제를 실행 해 보셨습니까? 그렇다면 실제로는 3 행만 표시되고 하단으로 스크롤되지는 않습니다 (아직 7 행이 표시됨). 그래서, 나는 당신의 문제를 아주 잘 이해하고 있다고 믿습니다. 그렇지만 당신은 문제를 재현하는 완전한 실례를 우리에게 보여주지 않습니다. 지금까지 보여준 것에 기반하여, 문제는 나타나지 않습니다. -> 뭔가 잘못되었거나, 다른 곳이 있지만, 우리에게 보여준 것이 아닙니다. 나는 단지 한 번 더 말할 수 있습니다 : [SSCCE] (http://sscce.org) –

+0

당신의 TestTextArea3.class.getSimpleName()은 무엇을합니까? – Newbies