0
添加文件JList中,我加入到JFrame中,一個JButton的,一個jtextview和一個JList的,我想補充文件的.txt但effectlly到jtextview例如:C:/Windows/file.txt添加文件的jList
JList.add(JList, null);
但有件事出了問題
感謝
添加文件JList中,我加入到JFrame中,一個JButton的,一個jtextview和一個JList的,我想補充文件的.txt但effectlly到jtextview例如:C:/Windows/file.txt添加文件的jList
JList.add(JList, null);
但有件事出了問題
感謝
您需要用列表模型,替你的JList。您不能像直接添加到JList一樣,因爲JList非常靈活,可以處理各種對象。這比這更復雜一點。
首先,你要確保你的JList實際上包含默認列表型號:
// Make sure the JList contains a List Model
try {
DefaultListModel dlm = (DefaultListModel)yourJListName.getModel();
}
catch (Exception e) {
// Nope...so let's set one.
yourJListName.setModel(new DefaultListModel());
}
現在,你肯定你的JList包含列表模型,你可以添加項目。
// Get the current List Model for your JList
DefaultListModel dlm = (DefaultListModel)yourJListName.getModel();
// Declare a list to represent it.
JList list = new JList(dlm);
// Get the last position within the List so
// as to append to it.
int pos = list.getModel().getSize();
// Add the text from the JTextField into your JList.
dlm.add(pos, yourJTextFieldName.getText());
也許創建一個方法來爲你做加法:
public void addToJList(JList yourJList, String stringToAdd) {
// Make sure the JList contains a List Model
try {
DefaultListModel dlm = (DefaultListModel)yourJList.getModel();
}
catch (Exception e) {
// Nope...so let's set one.
yourJList.setModel(new DefaultListModel());
}
// Get the current List Model for your JList
DefaultListModel dlm = (DefaultListModel)yourJList.getModel();
// Declare a list to represent it.
JList list = new JList(dlm);
// Get the last position within the List so
// as to append to it.
int pos = list.getModel().getSize();
// Add the text from the JTextField into your JList.
dlm.add(pos, stringToAdd);
}
和如何使用它:
addToJList(yourJListName, yourJTextFieldName.getText());
林推的JButton再要添加文件到JList和有效地jtextview前例如:C:/Windows/text.txt –
@ kasimirus - 該評論可能真的對你來說意味着什麼,但它對我來說毫無意義。你在說什麼文件?難道你想在JList中顯示**文件路徑**(C:\ Windows.file.txt),並且在JList中選擇了那個項目之後,你想要在JTextView中顯示文件內容?這給我帶來了另一個問題....什麼JTextView?你在哪個平臺工作。這是Android版嗎? – DevilsHnd
這是針對桌面應用程序c:/Windows/text.txt的一個位置文件並輸入到jlist的例子中。 300,400,500等 –