我使用JFileChooser
打開了一個文件。如果我打開一個文件,它會首次打開,如果我第二次打開同一個文件,它也會在新選項卡中打開。我只想打開一個文件一次。如果我們打開一個文件,當用戶打開相同的文件時不會再次打開
這裏是我的代碼,
final JFileChooser jc = new JFileChooser();
int returnVal= jc.showOpenDialog(Open.this);
String title;
String sts;
File file=null;
if(returnVal == JFileChooser.APPROVE_OPTION)
file = jc.getSelectedFile();
JTextArea text = new JTextArea();
if (jc.getSelectedFile()!= null) {
tx = new JTextArea();
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
StringBuffer st = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
}
catch (IOException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
title=file.getName();
sts=file.getPath();
tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
//tx.setLineWrap(true);
internalFrame.add(tx);
i+=1;
internalFrame.setName("Doc "+i);
JScrollPane scrollpane=new JScrollPane(tx);
internalFrame.setTitle(title);
tp.add(internalFrame);
try{
tp.setSelectedIndex(i-1);
}
catch(IndexOutOfBoundsException ioe){
}
internalFrame.add(scrollpane);
internalFrame.setVisible(true);
internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
tp.remove(internalFrame);
}
});
tx.setText(t);
try {
br.close();
}
catch (IOException ex) {
Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex);
}
}
感謝
你在說哪個標籤?請包括**所有相關的代碼**。 – BackSlash 2014-09-26 10:00:36
jtabbedpane @backslash – lavanya 2014-09-26 10:02:38
這裏有問題嗎? – erikduvet 2014-09-26 10:03:31