Creating Advanced Tabbed Notepad In Java

   
 
   
 
  

Introduction 
 
As you all know Java is the world's most popular & powerful programming language which is plateform independant. There are many editors developed using Java that we use like Eclipse,NetBeans,Intelli-J,JEdit etc.These editors are same as notepad but with added and developed extended features.
 
In this article we will cover only main functions such as open, save, close, run etc but will not see how to create syntax highlighting in java.
Well, it is easy to create syntax highlighting in java using StyledDocument. You can easily find source code to create syntax highlighting in java on the internet.

We will also change the icon of tabs when document text is changed or saved, like Notepad++ provides. First design your notepad with Menus,Menu Items,ToolBars etc. Download the source code to view complete code of advanced tabbed notepad and also created jar application.

I am using JTextPane object instead of JTextArea.

It is easy to perform New & Open operations. But to perform save or edit operations first you need to get the current textpane control from the JTabbedPane after performing New or Open operations for use/perform other operations.  
 
Let's Start

Create the object of JList,JTabbedPane,JSplit.

Add JList object & JTabbedPane object to JSplit.

Add JSplit object to the Container of the Frame. 
 
Getting JTextPane Control from Current Tab(Selected Tab) in JTabbedPane,
  1. Get the selected index from the added JTabbedPane.

  2. Cast the component of selected index of JTabbedPane with JScrollPane(if JScrollPane added).

  3. Get the Viewport from the casted JScrollPane.

  4. Finally Cast the component of index 0 with JTextPane.
Here's the code to get added JTextPane component from the added current Tab.
  1. int sel = _tabbedPane.getSelectedIndex();  
  2. JScrollPane jscroll=(JScrollPane)_tabbedPane.getComponentAt(sel);  
  3. JViewport jview=jscroll.getViewport();  
  4. JTextPane textpane=(JTextPane)jview.getComponent(0);   
It's a four line of code. But you can do this only in one/two lines like this.
  1. int sel = _tabbedPane.getSelectedIndex();  
  2. JTextPane textPane = (JTextPane)(((JScrollPane)_tabbedPane.getComponentAt(sel)).getViewport()).getComponent(0);  
Ok Now let's see the functions that are important. Add ActionListener to that menu item according to the actions you want. 
 
New

This function is easy to implement just by adding new Tab & textPane to JTabbedPane.Here i am also checking if dark theme is enabled or not,if it is then set background color to components.

Here's the code.
  1. public void File_New_Action()  
  2. {  
  3.     //crerate textpane object  
  4.      JTextPane _textPane=new JTextPane();  
  5.        
  6.      _textPane.setFont(new Font("Calibri",Font.PLAIN,14));  
  7.        
  8.      if(isDarkTheme){  
  9.          _textPane.setBackground(new Color(101020));  
  10.          _textPane.setForeground(new Color(250250250));  
  11.      }  
  12.        
  13.      JScrollPane jsp=new JScrollPane(_textPane);  
  14.      // add key listener & Undoable edit listener to text pane  
  15.      _textPane.addKeyListener(new KeyTypedAction());  
  16.      _textPane.getDocument().addUndoableEditListener(_undoManager);  
  17.      //add tab to _tabbedPane with control textpane  
  18.      _tabbedPane.addTab("Document "+count+" ",jsp);  
  19.      //add caret listener & mouse listener to text pane  
  20.      _textPane.addCaretListener(new CaretAction());  
  21.      _textPane.addMouseListener(new TextPane_MouseAction());  
  22.      int index=_tabbedPane.getTabCount()-1;  
  23.   
  24.      _tabbedPane.setSelectedIndex(index);  
  25.   
  26.      // set save icon to added tab  
  27.      _tabbedPane.setIconAt(index, new ImageIcon(this.getClass().getResource("resources/save.png")));  
  28.      listModel.addElement("Document "+count+" ");  
  29.   
  30.     _list.setSelectedIndex(index);  
  31.   
  32.     //change the title  
  33.      setTitle("Tabbed Notepad in Java - [ Document "+count+" ]");  
  34.      filenameLabel.setText("Document "+count);  
  35.   
  36.      count++;  
  37.   
  38. }   
Open 

This function is  the same as New function only just needs to read files. Here I am using FileDialog by setting setMultipleMode to true for getting multiple files.  Adding tab to tabbedpane as selected file name,changing title of frame etc. Once object of textPane is created then add KeyListener & UndoableListener to it.

Here's the code for open function.
  1. public void File_Open_Action()  
  2. {  
  3.      FileDialog fd = new FileDialog(new JFrame(), "Select File",FileDialog.LOAD);  
  4.      fd.setMultipleMode(true);  
  5.      fd.show();  
  6.      if (fd.getFiles()!=null)  
  7.      {  
  8.         File[] files=fd.getFiles();  
  9.         for(File item : files)  
  10.         {  
  11.            String  filename = item.toString();  
  12.            String file=filename;  
  13.            if(filename.contains("\\")){  
  14.                file = filename.substring(filename.lastIndexOf("\\") + 1);  
  15.            }  
  16.            else if(filename.contains("/")){  
  17.                file = filename.substring(filename.lastIndexOf("/") + 1);  
  18.            }  
  19.   
  20.            int count=_tabbedPane.getTabCount();  
  21.   
  22.            JTextPane _textPane=new JTextPane();  
  23.            _textPane.setFont(new Font("Calibri",Font.PLAIN,14));  
  24.              
  25.            if (isDarkTheme) {  
  26.                 _textPane.setBackground(new Color(101020));  
  27.                 _textPane.setForeground(new Color(250250250));  
  28.             }  
  29.              
  30.            JScrollPane jsp=new JScrollPane(_textPane);  
  31.            _textPane.addKeyListener(new KeyTypedAction());  
  32.             _textPane.getDocument().addUndoableEditListener(_undoManager);  
  33.             _textPane.addCaretListener(new CaretAction());  
  34.             _textPane.addMouseListener(new TextPane_MouseAction());  
  35.            _tabbedPane.addTab(file,jsp);  
  36.            _tabbedPane.setSelectedIndex(count);  
  37.            _tabbedPane.setIconAt(count, new ImageIcon(this.getClass().getResource("resources/save.png")));  
  38.            listModel.addElement(file);  
  39.            _list.setSelectedIndex(count);  
  40.   
  41.            setTitle("Tabbed Notepad in Java - [ "+file+" ]");  
  42.            filenameLabel.setText(filename);  
  43.            filesHoldListModel.addElement(filename);  
  44.   
  45.            BufferedReader d;  
  46.            StringBuffer sb = new StringBuffer();  
  47.            try  
  48.             {  
  49.               d = new BufferedReader(new FileReader(filename));  
  50.               String line;  
  51.               while((line=d.readLine())!=null)  
  52.                        sb.append(line + "\n");  
  53.                        _textPane.setText(sb.toString());  
  54.               d.close();  
  55.             }  
  56.            catch(FileNotFoundException fe)  
  57.             {  
  58.                System.out.println("File not Found");  
  59.             }  
  60.              catch(IOException ioe){}  
  61.   
  62.               _textPane.requestFocus();  
  63.   
  64.            }  
  65.        }  
  66.   
  67. }   
Save All

Well it is easy to implement Save As & Save function.Here we will see save all function. It is easy to implement this function. Use for loop from 0 to the maximimum index of tabbedpane & set that to the selected index of tabbedpane. Get the filename from added filenameLabel & save that file by getting the current textpane from current tab. To identitfy the full filename path, I have created the list filesHoldList and set the fullfilename path to the filenameLabel which is added to the statusStrip for saving the document.

Here's the code.
  1. public void File_SaveAll_Action()  
  2. {  
  3.     if (_tabbedPane.getTabCount() > 0)  
  4.     {  
  5.         int maxindex = _tabbedPane.getTabCount() - 1;  
  6.         for (int i = 0; i <= maxindex; i++)  
  7.         {  
  8.             _tabbedPane.setSelectedIndex(i);  
  9.             String filename = filenameLabel.getText();  
  10.             int sel = _tabbedPane.getSelectedIndex();  
  11.             JTextPane textPane = (JTextPane) (((JScrollPane) _tabbedPane.getComponentAt(sel)).getViewport()).getComponent(0);  
  12.             if (filename.contains("\\")||filename.contains("/"))  
  13.             {  
  14.                 File f = new File(filename);  
  15.                 if (f.exists())  
  16.                 {  
  17.                     try  
  18.                     {  
  19.                         DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));  
  20.                         String line = textPane.getText();  
  21.                         d.writeBytes(line);  
  22.                         d.close();  
  23.   
  24.                         String tabtext = _tabbedPane.getTitleAt(sel);  
  25.                         if (tabtext.contains("*")) {  
  26.                             tabtext = tabtext.replace("*""");  
  27.                             _tabbedPane.setTitleAt(sel, tabtext);  
  28.                             setTitle("Tabbed Notepad in Java - [ " + tabtext + " ]");  
  29.                             _tabbedPane.setIconAt(sel, new ImageIcon(this.getClass().getResource("resources/save.png")));  
  30.                         }  
  31.   
  32.                     }  
  33.                     catch (Exception ex)  
  34.                     {  
  35.                         System.out.println("File not found");  
  36.                     }  
  37.                     textPane.requestFocus();  
  38.                 }  
  39.             }  
  40.   
  41.         }  
  42.     }  
  43. }   
Close

This function is nothing but removing selected tab from tabbedpane. But before removing tab you must display a dialog box for saving modified changes before closing. If none of the document is modified then remove that tab otherwise show dialog for saving. Download the source code to view CloseAll function code.

Here's the code.
  1. public void File_Close_Action()  
  2. {  
  3.     if (_tabbedPane.getTabCount() > 0)  
  4.     {  
  5.         int sel = _tabbedPane.getSelectedIndex();  
  6.         String tabtext = _tabbedPane.getTitleAt(sel);  
  7.   
  8.         if (tabtext.contains("*"))  
  9.         {  
  10.             int n = JOptionPane.showConfirmDialog(null"Do you want to save " + tabtext + " before close ?",  
  11.                     "Save or Not", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);  
  12.   
  13.             tabtext.replace("*""");  
  14.   
  15.             if (n == 0)  
  16.             {  
  17.                 String filename = filenameLabel.getText();  
  18.                 JTextPane textPane = (JTextPane) (((JScrollPane) _tabbedPane.getComponentAt(sel)).getViewport()).getComponent(0);  
  19.   
  20.                 if (filename.contains("\\")||filename.contains("/"))  
  21.                 {  
  22.                     File_Save_Action();  
  23.   
  24.                     _tabbedPane.remove(sel);  
  25.                     listModel.removeAllElements();  
  26.   
  27.                     //adding all elements to list after removing the tab  
  28.                     for (int i = 0; i < _tabbedPane.getTabCount(); i++)  
  29.                     {  
  30.                         String item = _tabbedPane.getTitleAt(i);  
  31.                         if (item.contains("*"))  
  32.                         {  
  33.                             item = item.replace("*""").trim();  
  34.                         }  
  35.   
  36.                         listModel.addElement(item);  
  37.                     }  
  38.   
  39.                     _list.setSelectedIndex(_tabbedPane.getTabCount()-1);  
  40.   
  41.                     rowLabel.setText("Row :");  
  42.                     colLabel.setText("Col :");  
  43.   
  44.                     if(_tabbedPane.getTabCount()==0)  
  45.                     {  
  46.                         setTitle("Tabbed Notepad in Java");  
  47.                         filenameLabel.setText("");  
  48.                         rowLabel.setText("Row :");  
  49.                         colLabel.setText("Col :");  
  50.                     }  
  51.   
  52.                 }  
  53.   
  54.                 else if (filename.contains("Document "))  
  55.                 {  
  56.                     File_SaveAs_Action();  
  57.   
  58.                     _tabbedPane.remove(sel);  
  59.                     listModel.removeAllElements();  
  60.   
  61.                     //adding all elements to list after removing the tab  
  62.                     for (int i = 0; i < _tabbedPane.getTabCount(); i++)  
  63.                     {  
  64.                         String item = _tabbedPane.getTitleAt(i);  
  65.                         if (item.contains("*"))  
  66.                         {  
  67.                             item = item.replace("*""").trim();  
  68.                         }  
  69.   
  70.                         listModel.addElement(item);  
  71.                     }  
  72.   
  73.                     _list.setSelectedIndex(_tabbedPane.getTabCount() - 1);  
  74.   
  75.                     rowLabel.setText("Row :");  
  76.                     colLabel.setText("Col :");  
  77.   
  78.                     if (_tabbedPane.getTabCount() == 0)  
  79.                     {  
  80.                         setTitle("Tabbed Notepad in Java");  
  81.                         filenameLabel.setText("");  
  82.                         rowLabel.setText("Row :");  
  83.                         colLabel.setText("Col :");  
  84.                     }  
  85.                 }  
  86.   
  87.             }  
  88.   
  89.             if (n == 1)  
  90.             {  
  91.                 _tabbedPane.remove(sel);  
  92.                 listModel.removeAllElements();  
  93.   
  94.                 //adding all elements to list after removing the tab  
  95.                 for (int i = 0; i < _tabbedPane.getTabCount(); i++)  
  96.                 {  
  97.                     String item = _tabbedPane.getTitleAt(i);  
  98.                     if (item.contains("*"))  
  99.                     {  
  100.                         item = item.replace("*""").trim();  
  101.                     }  
  102.   
  103.                     listModel.addElement(item);  
  104.                 }  
  105.   
  106.                 _list.setSelectedIndex(_tabbedPane.getTabCount() - 1);  
  107.   
  108.                 rowLabel.setText("Row :");  
  109.                 colLabel.setText("Col :");  
  110.   
  111.                 if (_tabbedPane.getTabCount() == 0)  
  112.                 {  
  113.                     setTitle("Tabbed Notepad in Java");  
  114.                     filenameLabel.setText("");  
  115.                     rowLabel.setText("Row :");  
  116.                     colLabel.setText("Col :");  
  117.                 }  
  118.             }  
  119.         }  
  120.   
  121.         else  
  122.         {  
  123.             _tabbedPane.remove(sel);  
  124.             listModel.removeAllElements();  
  125.   
  126.             //adding all elements to list after removing the tab  
  127.             for (int i = 0; i < _tabbedPane.getTabCount(); i++)  
  128.             {  
  129.                 String item = _tabbedPane.getTitleAt(i);  
  130.                 if (item.contains("*"))  
  131.                 {  
  132.                     item = item.replace("*""").trim();  
  133.                 }  
  134.   
  135.                 listModel.addElement(item);  
  136.             }  
  137.   
  138.             _list.setSelectedIndex(_tabbedPane.getTabCount() - 1);  
  139.   
  140.             rowLabel.setText("Row :");  
  141.             colLabel.setText("Col :");  
  142.   
  143.             if (_tabbedPane.getTabCount() == 0)  
  144.             {  
  145.                 setTitle("Tabbed Notepad in Java");  
  146.                 filenameLabel.setText("");  
  147.                 rowLabel.setText("Row :");  
  148.                 colLabel.setText("Col :");  
  149.             }  
  150.   
  151.         }  
  152.     }  
  153.   
  154.     else  
  155.     {  
  156.         setTitle("Tabbed Notepad in Java");  
  157.         filenameLabel.setText("");  
  158.         rowLabel.setText("Row :");  
  159.         colLabel.setText("Col :");  
  160.   
  161.     }  
  162. }  
 
 
Window Menu :
 
We will also create Window menu on menubar for select tabs when clicking/selecting them(see image 3).
First create Window menu,add it to menubar and add MenuListener event to it. 
For action when window menu is selected, we will create JCheckBoxMenuItems with texts same as tabs text read from tabbedpane and will add action events to that menu items for select that tab.
  1. class WindowMenuAction implements MenuListener  
  2. {  
  3.     @Override  
  4.     public void menuSelected(MenuEvent me) {  
  5.         if (_tabbedPane.getTabCount() > 0) {  
  6.               
  7.             windowMenu.removeAll();  
  8.               
  9.             JMenuItem window_restart=new JMenuItem(" Restart                       ");  
  10.             window_restart.addActionListener(new WindowRestartAction());  
  11.             windowMenu.add(window_restart);  
  12.               
  13.             windowMenu.addSeparator();  
  14.               
  15.             int tabcount = _tabbedPane.getTabCount();  
  16.             String tabtext=_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());  
  17.             for (int i = 0; i < tabcount; i++) {  
  18.                 String title = _tabbedPane.getTitleAt(i);  
  19.                 JCheckBoxMenuItem witem=new JCheckBoxMenuItem(title);  
  20.                 witem.addActionListener(new Window_MenuItemsAction());  
  21.                 if(tabtext.equals(title)){  
  22.                     witem.setSelected(true);  
  23.                 }  
  24.                 windowMenu.add(witem);  
  25.             }  
  26.         }  
  27.   }  
  28.   
  29.     @Override  
  30.     public void menuDeselected(MenuEvent me) {  
  31.       }  
  32.   
  33.     @Override  
  34.     public void menuCanceled(MenuEvent me) {  
  35.     }  
  36. }  
  37.   
  38.   
  39. //Window Restart action class  
  40. class WindowRestartAction implements ActionListener  
  41. {  
  42.         @Override  
  43.         public void actionPerformed(ActionEvent ae) {  
  44.             File_CloseAll_Action();  
  45.             dispose();  
  46.             count = 1;  
  47.             LookAndFeelAction.setBasicLookAndFeel();  
  48.         }  
  49. }  
  50.   
  51.   
  52. //******************************************************  
  53. // Window menu item action  
  54. //******************************************************  
  55. class Window_MenuItemsAction implements ActionListener  
  56. {  
  57.         @Override  
  58.         public void actionPerformed(ActionEvent ae) {  
  59.               
  60.             String menutext=ae.getActionCommand().trim();  
  61.               
  62.             if (_tabbedPane.getTabCount() > 0) {  
  63.                 int tabcount = _tabbedPane.getTabCount();  
  64.                 for (int i = 0; i < tabcount; i++) {  
  65.                     String title = _tabbedPane.getTitleAt(i).trim();  
  66.                     if (title.contains("*")) {  
  67.                         title = title.replace("*""").trim();  
  68.                     }  
  69.   
  70.                     if (title.equals(menutext)) {  
  71.                         _tabbedPane.setSelectedIndex(i);  
  72.                         setTitle("Tabbed Notepad in Java - [ " + _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex()) + " ]");  
  73.                     }  
  74.                 }  
  75.             }  
  76.         }  
  77. }  
 
 
 
It is easy to implement Edit operations in java like Cut,Copy,Paste,Undo,Redo etc.
 
 
Java does not provide the pre created font dialog. you have to create your own font dialog box. Here's my created font dialog box code that set the font to the current textpane in current selected tab in tabbedpane.
 
   
 
Here's the code,
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import javax.swing.*;  
  6. import javax.swing.event.*;  
  7. import javax.xml.parsers.DocumentBuilder;  
  8. import javax.xml.parsers.DocumentBuilderFactory;  
  9. import javax.xml.parsers.ParserConfigurationException;  
  10. import org.w3c.dom.DOMException;  
  11. import org.w3c.dom.Document;  
  12. import org.w3c.dom.Element;  
  13. import org.w3c.dom.Node;  
  14. import org.w3c.dom.NodeList;  
  15. import org.xml.sax.SAXException;  
  16.   
  17. public final class FontAction extends JDialog implements ListSelectionListener,ActionListener  
  18. {  
  19.     String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();  
  20.     String[] fontStyles={  
  21.          "  Plain  ",  
  22.          "  Bold  ",  
  23.          "  Italic  ",  
  24.          "  Plain+Bold  ",  
  25.          "  Plain+Italic  ",  
  26.          "  Bold+Italic  ",  
  27.          "  Plain+Bold+Italic  "  
  28.                                  };  
  29.     List lst=new List();  
  30.     JList fontsList;  
  31.     JList fontStyleList;  
  32.     JList fontSizeList;  
  33.     JPanel jp1,jp2;  
  34.     DefaultListModel model;  
  35.     JLabel displayLabel;  
  36.     JButton ok,cancel;  
  37.     JTextPane textPane;  
  38.       
  39.     static boolean isDarkTheme = false;  
  40.       
  41.     public FontAction(JTextPane tx)  
  42.     {  
  43.         textPane=tx;  
  44.   
  45.         Container cp=getContentPane();  
  46.           
  47.         isDarkTheme = getNodeTextContent("lookAndFeel").equals("GlobalDark");  
  48.   
  49.         fontsList=new JList(fontNames);  
  50.         fontStyleList=new JList(fontStyles);  
  51.   
  52.         fontsList.setFont(new Font("Calibri",Font.PLAIN,14));  
  53.         fontStyleList.setFont(new Font("Calibri",Font.PLAIN,14));  
  54.   
  55.         model=new DefaultListModel();  
  56.         fontSizeList = new JList(model);  
  57.         fontSizeList.setFont(new Font("Calibri", Font.PLAIN, 14));  
  58.           
  59.   
  60.         for(int i=1;i<=160;i++)  
  61.         {  
  62.             model.addElement("  "+i+"        ");  
  63.         }  
  64.   
  65.   
  66.         fontsList.setSelectedIndex(8);  
  67.         fontStyleList.setSelectedIndex(0);  
  68.         fontSizeList.setSelectedIndex(21);  
  69.   
  70.         fontsList.addListSelectionListener(this);  
  71.         fontStyleList.addListSelectionListener(this);  
  72.         fontSizeList.addListSelectionListener(this);  
  73.   
  74.         jp1=new JPanel();  
  75.         jp2=new JPanel();  
  76.         JPanel jp3=new JPanel();  
  77.         jp3.add(new JScrollPane(fontsList));  
  78.   
  79.   
  80.         JPanel jp4=new JPanel();  
  81.         jp4.setLayout(new GridLayout(0,2));  
  82.         jp4.add(new JScrollPane(fontStyleList));  
  83.         jp4.add(new JScrollPane(fontSizeList));  
  84.   
  85.         jp1.add(jp3,BorderLayout.WEST);  
  86.         jp1.add(jp4,BorderLayout.EAST);  
  87.   
  88.         displayLabel=new JLabel("Java Programming",JLabel.CENTER);  
  89.         displayLabel.setFont(new Font("Arial",Font.PLAIN,21));  
  90.   
  91.         jp1.add(displayLabel);  
  92.   
  93.         ok=new JButton("  OK  ");  
  94.         cancel=new JButton("  Cancel  ");  
  95.           
  96.         if (isDarkTheme) {  
  97.             fontsList.setBackground(new Color(404040));  
  98.             fontStyleList.setBackground(new Color(404040));  
  99.             fontSizeList.setBackground(new Color(404040));  
  100.             displayLabel.setForeground(new Color(240240240));  
  101.         }  
  102.   
  103.         ok.addActionListener(this);  
  104.         cancel.addActionListener(this);  
  105.   
  106.         jp2.add(ok);  
  107.         jp2.add(cancel);  
  108.   
  109.         cp.add(jp1,BorderLayout.CENTER);  
  110.         cp.add(jp2,BorderLayout.SOUTH);  
  111.   
  112.     }  
  113.   
  114.     @Override  
  115.     public void valueChanged(ListSelectionEvent evt)  
  116.     {  
  117.         String fontname=fontsList.getSelectedValue().toString();  
  118.         String fontstyle=fontStyleList.getSelectedValue().toString().trim();  
  119.         int fontsize=Integer.parseInt(fontSizeList.getSelectedValue().toString().trim());  
  120.   
  121.         switch(fontstyle)  
  122.         {  
  123.             case "Plain":  
  124.                 displayLabel.setFont(new Font(fontname, Font.PLAIN, fontsize));  
  125.                 break;  
  126.   
  127.             case "Bold":  
  128.                 displayLabel.setFont(new Font(fontname, Font.BOLD, fontsize));  
  129.                 break;  
  130.   
  131.             case "Italic":  
  132.                 displayLabel.setFont(new Font(fontname, Font.ITALIC, fontsize));  
  133.                 break;  
  134.   
  135.             case "Plain+Bold":  
  136.                 displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.BOLD, fontsize));  
  137.                 break;  
  138.   
  139.             case "Plain+Italic":  
  140.                 displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.ITALIC, fontsize));  
  141.                 break;  
  142.   
  143.             case "Bold+Italic":  
  144.                 displayLabel.setFont(new Font(fontname, Font.BOLD + Font.ITALIC, fontsize));  
  145.                 break;  
  146.   
  147.             case "Plain+Bold+Italic":  
  148.                 displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.BOLD + Font.ITALIC, fontsize));  
  149.                 break;  
  150.         }  
  151.     }  
  152.   
  153.     @Override  
  154.     public void actionPerformed(ActionEvent evt)  
  155.     {  
  156.         Object source=evt.getSource();  
  157.         if(source==ok)  
  158.         {  
  159.             String fontname = fontsList.getSelectedValue().toString();  
  160.             String fontstyle = fontStyleList.getSelectedValue().toString().trim();  
  161.             int fontsize = Integer.parseInt(fontSizeList.getSelectedValue().toString().trim());  
  162.   
  163.             switch (fontstyle)  
  164.             {  
  165.                 case "Plain":  
  166.                     textPane.setFont(new Font(fontname, Font.PLAIN, fontsize));  
  167.                     break;  
  168.   
  169.                 case "Bold":  
  170.                     textPane.setFont(new Font(fontname, Font.BOLD, fontsize));  
  171.                     break;  
  172.   
  173.                 case "Italic":  
  174.                     textPane.setFont(new Font(fontname, Font.ITALIC, fontsize));  
  175.                     break;  
  176.   
  177.                 case "Plain+Bold":  
  178.                     textPane.setFont(new Font(fontname, Font.PLAIN + Font.BOLD, fontsize));  
  179.                     break;  
  180.   
  181.                 case "Plain+Italic":  
  182.                     textPane.setFont(new Font(fontname, Font.PLAIN + Font.ITALIC, fontsize));  
  183.                     break;  
  184.   
  185.                 case "Bold+Italic":  
  186.                     textPane.setFont(new Font(fontname, Font.BOLD + Font.ITALIC, fontsize));  
  187.                     break;  
  188.   
  189.                 case "Plain+Bold+Italic":  
  190.                     textPane.setFont(new Font(fontname, Font.PLAIN + Font.BOLD + Font.ITALIC, fontsize));  
  191.                     break;  
  192.             }  
  193.   
  194.             this.dispose();  
  195.         }  
  196.         else if(source==cancel)  
  197.         {  
  198.             this.dispose();  
  199.         }  
  200.     }  
  201.   
  202.       
  203.       
  204.     //**************************************************  
  205.     // returns content of node  
  206.     //**************************************************  
  207.     public String getNodeTextContent(String nodetag) {  
  208.         String content = "";  
  209.   
  210.         try {  
  211.   
  212.             File fXmlFile = new File("files/viewsfile.xml");  
  213.             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
  214.             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
  215.             Document doc = dBuilder.parse(fXmlFile);  
  216.   
  217.             doc.getDocumentElement().normalize();  
  218.   
  219.             NodeList nList = doc.getElementsByTagName("views");  
  220.   
  221.             for (int temp = 0; temp < nList.getLength(); temp++) {  
  222.   
  223.                 Node nNode = nList.item(temp);  
  224.   
  225.                 Element eElement = (Element) nNode;  
  226.   
  227.                 content = eElement.getElementsByTagName(nodetag).item(0).getTextContent();  
  228.   
  229.             }  
  230.         } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {  
  231.         }  
  232.   
  233.         return content;  
  234.   
  235.     }  
  236. }  
 
 
 
Selecting Tab by clicking on Document Selector
 
As you know every advanced editor has this function,where you can click on the document name then that name of document tab is activated. Like Solution Explorer in Visual Studio. It is easy to implement this function by just creating a class that implement the interface ListSelectionListener and define the valueChanged() function. And add addListSelectionListener() to the document selector list with implemented ListSelectionListener class object. Get each tab title from tabbedpane,compare that value with selected item from the list & set that tab to selected.

Here's the code.
  1. class SelectTabFromListItem implements ListSelectionListener  
  2. {  
  3.     public void valueChanged(ListSelectionEvent evt)  
  4.     {  
  5.         if(_list.getSelectedValue()!=null)  
  6.         {  
  7.             String list_item=_list.getSelectedValue().toString().trim();  
  8.               
  9.             if(_tabbedPane.getTabCount() >0)  
  10.             {  
  11.                 int tabcount=_tabbedPane.getTabCount();  
  12.                for(int i=0;i<tabcount;i++)  
  13.                 {  
  14.                     String title=_tabbedPane.getTitleAt(i).trim();  
  15.                     if (title.contains("*"))   
  16.                     {  
  17.                         title = title.replace("*""").trim();  
  18.                     }  
  19.                       
  20.                    if(title.equals(list_item))  
  21.                     {  
  22.                         _tabbedPane.setSelectedIndex(i);  
  23.                         setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");  
  24.                     }  
  25.                 }  
  26.             }  
  27.   
  28.         }  
  29.     }  
  30. }   
Changing Title of frame,text of filenameLabel when Tab Changed
 
It is easy to implement this function by just creating a class that implements the interface ChangeListener and defines the stateChanged() function. And add addChangeListener() to the tabbedpane with implemented ChangeListener class object.

Here's the code.
  1. class TabChanged implements ChangeListener  
  2. {  
  3.     @Override  
  4.     public void stateChanged(ChangeEvent evt)  
  5.     {  
  6.         if(_tabbedPane.getTabCount()>0)  
  7.         {  
  8.             Object[] files=filesHoldListModel.toArray();  
  9.             String tabtext=_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex()).trim();  
  10.             if(tabtext.contains("*"))  
  11.              {  
  12.                  tabtext=tabtext.replace("*""");  
  13.              }  
  14.   
  15.             for(Object filename : files)  
  16.             {  
  17.                 String file=filename.toString().substring(filename.toString().lastIndexOf("\\")+1);  
  18.   
  19.                 if(file.equals(tabtext))  
  20.                 {  
  21.                     filenameLabel.setText(filename.toString());  
  22.                     setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");  
  23.                 }  
  24.             }  
  25.   
  26.             if(tabtext.contains("Document "))  
  27.             {  
  28.                 filenameLabel.setText(tabtext);  
  29.                 setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");  
  30.             }  
  31.   
  32.         }  
  33.     }  
  34. }  
 

To set dark theme we will set setDefaultLookAndFeelDecorated() to true and will call method MetalLookAndFeel.setCurrentTheme(). we will use javax.swing.plaf.ColorUIResource class. see following link for many infomation about ColorUIResource. 

I have created a class JavaGlobalDarkTheme in which i defined all these methods and calling them in setGlobalDarkLookAndFeel() method.

  1. class JavaGlobalDarkTheme {  
  2.   
  3.     DefaultMetalTheme darkTheme = new DefaultMetalTheme() {  
  4.           
  5.         @Override  
  6.         public ColorUIResource getPrimary1() {  
  7.             return new ColorUIResource(new Color(303030));  
  8.         }  
  9.           
  10.           
  11.         @Override  
  12.         public ColorUIResource getPrimary2() {  
  13.             return new ColorUIResource(new Color(202020));  
  14.         }  
  15.           
  16.         @Override  
  17.         public ColorUIResource getPrimary3() {  
  18.             return new ColorUIResource(new Color(303030));  
  19.         }  
  20.   
  21.         @Override  
  22.         public ColorUIResource getBlack(){  
  23.                     return new ColorUIResource(new Color(303030));  
  24.                 }  
  25.           
  26.         @Override  
  27.         public ColorUIResource getWhite() {  
  28.             return new ColorUIResource(new Color(240240240));  
  29.         }  
  30.                   
  31.                   
  32.         @Override  
  33.         public ColorUIResource getMenuForeground() {  
  34.             return new ColorUIResource(new Color(200200200));  
  35.         }  
  36.                   
  37.         @Override  
  38.         public ColorUIResource getMenuBackground() {  
  39.             return new ColorUIResource(new Color(252525));  
  40.         }  
  41.   
  42.          @Override  
  43.         public ColorUIResource getMenuSelectedBackground(){  
  44.             return new ColorUIResource(new Color(505050));  
  45.         }  
  46.                   
  47.         @Override  
  48.         public ColorUIResource getMenuSelectedForeground() {  
  49.             return new ColorUIResource(new Color(255255255));  
  50.         }  
  51.   
  52.                   
  53.         @Override  
  54.         public ColorUIResource getSeparatorBackground() {  
  55.             return new ColorUIResource(new Color(151515));  
  56.         }  
  57.                   
  58.                   
  59.         @Override  
  60.         public ColorUIResource getUserTextColor() {  
  61.             return new ColorUIResource(new Color(240240240));  
  62.         }  
  63.                   
  64.         @Override  
  65.         public ColorUIResource getTextHighlightColor() {  
  66.             return new ColorUIResource(new Color(804080));  
  67.         }  
  68.   
  69.                   
  70.         @Override  
  71.         public ColorUIResource getAcceleratorForeground(){  
  72.             return new ColorUIResource(new Color(3030,30));  
  73.         }  
  74.                   
  75.   
  76.         @Override  
  77.         public ColorUIResource getWindowTitleInactiveBackground() {  
  78.             return new ColorUIResource(new Color(303030));  
  79.         }  
  80.   
  81.   
  82.         @Override  
  83.         public ColorUIResource getWindowTitleBackground() {  
  84.             return new ColorUIResource(new Color(303030));  
  85.         }  
  86.   
  87.   
  88.         @Override  
  89.         public ColorUIResource getWindowTitleForeground() {  
  90.             return new ColorUIResource(new Color(230230230));  
  91.         }  
  92.                   
  93.         @Override  
  94.         public ColorUIResource getPrimaryControlHighlight() {  
  95.             return new ColorUIResource(new Color(404040));  
  96.         }  
  97.   
  98.         @Override  
  99.         public ColorUIResource getPrimaryControlDarkShadow() {  
  100.             return new ColorUIResource(new Color(404040));  
  101.         }  
  102.   
  103.         @Override  
  104.         public ColorUIResource getPrimaryControl() {  
  105.             //color for minimize,maxi,and close  
  106.             return new ColorUIResource(new Color(606060));  
  107.         }  
  108.   
  109.         @Override  
  110.         public ColorUIResource getControlHighlight() {  
  111.             return new ColorUIResource(new Color(202020));  
  112.         }  
  113.                  
  114.         @Override  
  115.         public ColorUIResource getControlDarkShadow() {  
  116.             return new ColorUIResource(new Color(505050));  
  117.         }  
  118.   
  119.         @Override  
  120.         public ColorUIResource getControl() {  
  121.             return new ColorUIResource(new Color(252525));  
  122.         }  
  123.           
  124.         @Override  
  125.         public ColorUIResource getControlTextColor() {  
  126.             return new ColorUIResource(new Color(230230230));  
  127.         }  
  128.           
  129.         @Override  
  130.         public ColorUIResource getFocusColor() {  
  131.             return new ColorUIResource(new Color(01000));  
  132.         }  
  133.           
  134.         @Override  
  135.         public ColorUIResource getHighlightedTextColor() {  
  136.             return new ColorUIResource(new Color(250250250));  
  137.         }  
  138.    
  139.     };  
  140. }  
  1. public static void setGlobalDarkLookAndFeel()   
  2. {  
  3.     MetalLookAndFeel.setCurrentTheme(new JavaGlobalDarkTheme().darkTheme);  
  4.     try {  
  5.         UIManager.setLookAndFeel(new MetalLookAndFeel());  
  6.     } catch (Exception ev) {  
  7.     }  
  8.   
  9.     JFrame.setDefaultLookAndFeelDecorated(true);  
  10.     TabbedNotepad tb = new TabbedNotepad();  
  11.     tb.setExtendedState(JFrame.MAXIMIZED_BOTH);  
  12.     BufferedImage image = null;  
  13.     try {  
  14.         image = ImageIO.read(tb.getClass().getResource("resources/myicon.png"));  
  15.     } catch (IOException e) {  
  16.     }  
  17.     tb.setIconImage(image);  
  18.     tb.setSize(800600);  
  19.     tb.setLocation(10050);  
  20.     tb.setVisible(true);  
  21.   
  22. }