2012-04-04 2 views
1

JTabbedPane에 문제가 있습니다. 개별 탭의 내용이 표시되지 않습니다 (새 탭을 클릭 할 때마다 활성 탭이 변경되지만 내용이 변경되지 않으므로 어떤 탭이 선택 되더라도 동일한 내용을보십시오.).JTabbedPane이 탭 내용을 전환하지 않습니다.

내 프로그래밍 언어 용 IDE를 작성하려고하지만 이전에는 JTabbedPane을 사용 해보지 않았습니다. 나의 탭 구획은, JScrollPane에 보관 유지되고있는 JEditTextArea (사용자 작성의 컴퍼넌트)로 구성됩니다. 여기에 책임있는 클래스입니다

package tide.editor; 

import javax.swing.*; 

import javax.swing.filechooser.FileNameExtensionFilter; 

import java.awt.Toolkit; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

import org.syntax.jedit.*; 
import org.syntax.jedit.tokenmarker.*; 

@SuppressWarnings("serial") 
public class Editor extends JFrame implements ActionListener { 

//Version ID 
final static String VERSION = "0.01a"; 

//The editor pane houses the JTabbedPane that allows for code editing 
//JPanel editorPane; 

JTabbedPane tabbedPanel; 

//The JTextPanes hold the source for currently open programs 
ArrayList<JEditTextArea> textPanes; 

//The toolbar that allows for quick opening, saving, compiling etc 
JToolBar toolBar; 

//Buttons for the toolbar 
JButton newButton, openButton, saveButton, compileButton, runButton; 


public Editor() 
{ 
    super("tIDE v" + VERSION); 
    setSize(Toolkit.getDefaultToolkit().getScreenSize()); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new BorderLayout()); 
    init(); 
    setVisible(true); 
    textPanes.get(0).requestFocus(); 
} 

public void init() 
{ 
    //Initialise toolbar 
    toolBar = new JToolBar(); 
    toolBar.setFloatable(false); 

    newButton = new JButton("New"); 
    newButton.addActionListener(this); 
    openButton = new JButton("Open"); 
    openButton.addActionListener(this); 
    saveButton = new JButton("Save"); 
    saveButton.addActionListener(this); 
    compileButton = new JButton("Compile"); 
    compileButton.addActionListener(this); 
    runButton = new JButton("Run"); 
    runButton.addActionListener(this); 

    toolBar.add(newButton); 
    toolBar.add(openButton); 
    toolBar.add(saveButton); 
    toolBar.add(compileButton); 
    toolBar.add(runButton); 


    tabbedPanel = new JTabbedPane(); 

    textPanes = new ArrayList<JEditTextArea>(); 

    JEditTextArea initialProgram = createTextArea("program.t"); 

     tabbedPanel.addTab(initialProgram.getName(), new JScrollPane(initialProgram)); 


    getContentPane().add(tabbedPanel, BorderLayout.CENTER); 
    add(toolBar, BorderLayout.NORTH); 
} 

public static void main(String[] args) 
{ 
java.awt.EventQueue.invokeLater(new Runnable() { 

    @Override 
    public void run() { 
     new Editor(); 
    } 
}); 

} 

JEditTextArea createTextArea(String name) 
{ 
    JEditTextArea editPane = new JEditTextArea(name); 
    editPane.setTokenMarker(new TTokenMarker()); 

    textPanes.add(editPane); 

    return editPane; 
} 

public void actionPerformed(ActionEvent e) { 

    if (e.getSource() == newButton) 
    { 
     String filename = "program2"; 
     boolean fileExists = true; 

     //Ensures that no duplicate files are created 
     while (fileExists) 
     { 
     fileExists = false; 
     //Get new file name from user 
     filename = JOptionPane.showInputDialog(null, "Enter a name for the file", "program.t"); 

     //Cancel was clicked in the new file dialog 
     if (filename == null) 
      return; 

      for (JEditTextArea panes: textPanes) 
      { 
       if (panes.getName().equals(filename) || panes.getName().equals(filename.concat(".t")) || panes.getName().concat(".t").equals(filename)) 
        fileExists = true; 
      } 
     } 

     //add extension if it is missing 
     if(!filename.endsWith(".t")) 
      filename = filename.concat(".t"); 

     //Add the new "file" to the editor window in a new tab 
     tabbedPanel.addTab(filename, new JScrollPane(createTextArea(filename))); 
     tabbedPanel.setSelectedIndex(tabbedPanel.getSelectedIndex()+1); 
    } 

    if (e.getSource() == openButton) 
    { 
     File f = null; 


     JFileChooser fileChooser = new JFileChooser(); 
     fileChooser.setDialogTitle("Choose target file location"); 
     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
     fileChooser.setAcceptAllFileFilterUsed(false); 
     FileNameExtensionFilter filter = new FileNameExtensionFilter(
       "t source or bytecode(.t, .tbc)", "t", "tbc"); 

     fileChooser.setFileFilter(filter); 

     if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
     { 
      f = fileChooser.getSelectedFile(); 
     } 

     //Cancel button was clicked on the open file dialog 
     if (f == null) 
      return; 

     //Load the contents of the selected file into the editor 
     else 
     { 
      JEditTextArea textArea = null; 
      StringBuilder inputText = null; 

      try { 
       Scanner sc = new Scanner(f); 

       //Add a new text area to the editor 
       textArea = createTextArea(f.getName()); 
       tabbedPanel.add(new JScrollPane(textArea), textArea.getName()); 

       //The newly added tab is set as the active tab 
       tabbedPanel.setSelectedIndex(tabbedPanel.getTabCount()-1); 
       textArea.requestFocus(); 

       inputText = new StringBuilder(); 
       while (sc.hasNext()) 
       { 
        inputText.append(sc.nextLine() + "\n"); 
       } 

      } catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } 

      //Set the contents of the current text area to that of the opened file 
      textArea.setText(inputText.toString()); 
     } 
    } 

} 

} 

답변

0

이 근본 원인에 대한 해결책이 될 않을 수도 있지만 거기 TestAreas에 페인트 문제를 일으키는 코드에서 약간의 불일치가있을 수 있습니다, 그래서 당신이 TabChageListener을 등록 제안 및 그 특정 탭에 대한의 ScrollPane을 다시 칠이 작동합니다 :

tabbedPanel.addChangeListener(new ChangeListener() { 
    public void stateChanged(ChangeEvent e) { 
    int selectedIndex = tabbedPane.getSelectedIndex(); 
    // get the ScrollPane for this selectedIndex and then repaint it. 

    } 
}); 
+0

나는 이것을 시도했지만 불행히도 작동하지 않았다. 같은 문제가 계속 발생합니다. –

3

1) 관련 FileIO에서 코드 줄을 제거 try - catch BLOK

textArea = createTextArea(f.getName()); 
tabbedPanel.add(new JScrollPane(textArea), textArea.getName()); 
//The newly added tab is set as the active tab 
tabbedPanel.setSelectedIndex(tabbedPanel.getTabCount()-1); 
textArea.requestFocus(); 
,

모두 세퍼레이터를 수용 (try - catch - finally)

3) JTextComponetsread()write() 구현 finally 블록에서 Input/OutputStreams에서의 close()) Input/OutputStreams

2 결국 Objects 전에 그 Object을 준비, 프로그램 후이 이유 호출 "\ n"

4)에 대한 FileIO

에 대한 SwingWorker 코드를 사용 0
+0

나는 당신이 여기서 제안한 것을 대부분 시도했지만 (나는 SwingWorker 사용법을 모르지만) 여전히 운이 없다. –

+0

@Peter : 튜토리얼에서 SwingWorker를 읽어 보시기 바랍니다. 문제가 동시성 문제 일 수 있습니다. –

+0

좋아, 내가 할거야. 감사 –

0

저는 지난 몇 시간 동안이 바로 그 문제에 고심하고 있습니다.

실제로 JEditTextArea을 얼마나 많이 인스턴스화하든 관계없이 모두 동일한 SyntaxDocument에 의존하므로 동일한 텍스트를 사용합니다. 이는 JEditTextArea에 코딩 된 것입니다.

setDocument(defaults.document);

과로 교체 : 당신이 선을 찾아야합니다 public JEditTextArea(TextAreaDefaults defaults) 생성자에서

하는 setDocument (새 SyntaxDocument());

JEditTextArea을 만들 때마다 새로운 Document을 인스턴스화합니다.