Results 1 to 3 of 3
  1. #1
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default JTabbedPane problem

    To implement a tabbed interface for a notepad application, I created an array of JTextAreas for the tabs.

    Java Code:
    JTextArea tabs[] = new JTextArea[4];
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Tab1", new ImageIcon("new.gif"), tabs[0]);
    tabbedPane.addTab("Tab2", new ImageIcon("new.gif"), tabs[1]);
    tabbedPane.addTab("Tab3", new ImageIcon("new.gif"), tabs[2]);
    tabbedPane.addTab("Tab4", new ImageIcon("new.gif"), tabs[3]);
    In order to save tabs, I created a button on my toolbar:
    Java Code:
    if(actionCommand.equals("Save As...")) {    
        JFileChooser localJFileChooser = new JFileChooser();
        String saveasText = tabs[tabbedPane.getSelectedIndex()].getText();              		
        int j = localJFileChooser.showSaveDialog(null);	           					
        File selectedFile = localJFileChooser.getSelectedFile();           							              		
        try 
       {
    		FileOutputStream saveasOutputStream = new FileOutputStream(selectedFile);		 
    		for (int k = 0; k < saveasText.length(); k++) {
    			saveasOutputStream.write(saveasText.charAt(k));
    		}
    	}		               				
        catch (Exception exception) {
    		System.out.println(exception);
        }                     				
        tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), selectedFile.getName());		
    }
    The problem is that while the save as functionality works perfectly for the first tab,
    it just doesn't work for the rest of the tabs. When clicking on the 'Save As' when the
    first tab is selected, the text gets saved perfectly. But clicking 'Save As' when any
    other tab is selected causes a NullPointerException. What could be the cause of this?
    Thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    I don't see where you initialized any of the JTextAreas in the array. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates your problem. Code snippets out of context are useless for debugging.

    db

  3. #3
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    I thought if I put forth the code for initializing the JTextAreas and other supplementary code, then it would get too long. That's why I assumed its initialization.

    If you don't mind me putting somewhat lengthy code, I'll demonstrate my question better.
    Thanks.

Similar Threads

  1. Using JtabbedPane
    By simmi in forum AWT / Swing
    Replies: 9
    Last Post: 03-24-2010, 01:24 PM
  2. Problem extending JFrame with JTabbedPane in it
    By Cybex in forum AWT / Swing
    Replies: 9
    Last Post: 10-31-2009, 06:51 PM
  3. Problem Adding Content To JTabbedPane
    By JDCAce in forum AWT / Swing
    Replies: 5
    Last Post: 10-18-2008, 10:45 AM
  4. How to use JTabbedPane
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:50 PM
  5. Help with JTabbedPane
    By lenny in forum SWT / JFace
    Replies: 1
    Last Post: 08-07-2007, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •