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,
- Get the selected index from the added JTabbedPane.
- Cast the component of selected index of JTabbedPane with JScrollPane(if JScrollPane added).
- Get the Viewport from the casted JScrollPane.
- Finally Cast the component of index 0 with JTextPane.
Here's the code to get added JTextPane component from the added current Tab.
- int sel = _tabbedPane.getSelectedIndex();
- JScrollPane jscroll=(JScrollPane)_tabbedPane.getComponentAt(sel);
- JViewport jview=jscroll.getViewport();
- 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.
- int sel = _tabbedPane.getSelectedIndex();
- 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.
- public void File_New_Action()
- {
-
- JTextPane _textPane=new JTextPane();
-
- _textPane.setFont(new Font("Calibri",Font.PLAIN,14));
-
- if(isDarkTheme){
- _textPane.setBackground(new Color(10, 10, 20));
- _textPane.setForeground(new Color(250, 250, 250));
- }
-
- JScrollPane jsp=new JScrollPane(_textPane);
-
- _textPane.addKeyListener(new KeyTypedAction());
- _textPane.getDocument().addUndoableEditListener(_undoManager);
-
- _tabbedPane.addTab("Document "+count+" ",jsp);
-
- _textPane.addCaretListener(new CaretAction());
- _textPane.addMouseListener(new TextPane_MouseAction());
- int index=_tabbedPane.getTabCount()-1;
-
- _tabbedPane.setSelectedIndex(index);
-
-
- _tabbedPane.setIconAt(index, new ImageIcon(this.getClass().getResource("resources/save.png")));
- listModel.addElement("Document "+count+" ");
-
- _list.setSelectedIndex(index);
-
-
- setTitle("Tabbed Notepad in Java - [ Document "+count+" ]");
- filenameLabel.setText("Document "+count);
-
- count++;
-
- }
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.
- public void File_Open_Action()
- {
- FileDialog fd = new FileDialog(new JFrame(), "Select File",FileDialog.LOAD);
- fd.setMultipleMode(true);
- fd.show();
- if (fd.getFiles()!=null)
- {
- File[] files=fd.getFiles();
- for(File item : files)
- {
- String filename = item.toString();
- String file=filename;
- if(filename.contains("\\")){
- file = filename.substring(filename.lastIndexOf("\\") + 1);
- }
- else if(filename.contains("/")){
- file = filename.substring(filename.lastIndexOf("/") + 1);
- }
-
- int count=_tabbedPane.getTabCount();
-
- JTextPane _textPane=new JTextPane();
- _textPane.setFont(new Font("Calibri",Font.PLAIN,14));
-
- if (isDarkTheme) {
- _textPane.setBackground(new Color(10, 10, 20));
- _textPane.setForeground(new Color(250, 250, 250));
- }
-
- JScrollPane jsp=new JScrollPane(_textPane);
- _textPane.addKeyListener(new KeyTypedAction());
- _textPane.getDocument().addUndoableEditListener(_undoManager);
- _textPane.addCaretListener(new CaretAction());
- _textPane.addMouseListener(new TextPane_MouseAction());
- _tabbedPane.addTab(file,jsp);
- _tabbedPane.setSelectedIndex(count);
- _tabbedPane.setIconAt(count, new ImageIcon(this.getClass().getResource("resources/save.png")));
- listModel.addElement(file);
- _list.setSelectedIndex(count);
-
- setTitle("Tabbed Notepad in Java - [ "+file+" ]");
- filenameLabel.setText(filename);
- filesHoldListModel.addElement(filename);
-
- BufferedReader d;
- StringBuffer sb = new StringBuffer();
- try
- {
- d = new BufferedReader(new FileReader(filename));
- String line;
- while((line=d.readLine())!=null)
- sb.append(line + "\n");
- _textPane.setText(sb.toString());
- d.close();
- }
- catch(FileNotFoundException fe)
- {
- System.out.println("File not Found");
- }
- catch(IOException ioe){}
-
- _textPane.requestFocus();
-
- }
- }
-
- }
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.
- public void File_SaveAll_Action()
- {
- if (_tabbedPane.getTabCount() > 0)
- {
- int maxindex = _tabbedPane.getTabCount() - 1;
- for (int i = 0; i <= maxindex; i++)
- {
- _tabbedPane.setSelectedIndex(i);
- String filename = filenameLabel.getText();
- int sel = _tabbedPane.getSelectedIndex();
- JTextPane textPane = (JTextPane) (((JScrollPane) _tabbedPane.getComponentAt(sel)).getViewport()).getComponent(0);
- if (filename.contains("\\")||filename.contains("/"))
- {
- File f = new File(filename);
- if (f.exists())
- {
- try
- {
- DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
- String line = textPane.getText();
- d.writeBytes(line);
- d.close();
-
- String tabtext = _tabbedPane.getTitleAt(sel);
- if (tabtext.contains("*")) {
- tabtext = tabtext.replace("*", "");
- _tabbedPane.setTitleAt(sel, tabtext);
- setTitle("Tabbed Notepad in Java - [ " + tabtext + " ]");
- _tabbedPane.setIconAt(sel, new ImageIcon(this.getClass().getResource("resources/save.png")));
- }
-
- }
- catch (Exception ex)
- {
- System.out.println("File not found");
- }
- textPane.requestFocus();
- }
- }
-
- }
- }
- }
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.
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.
- class WindowMenuAction implements MenuListener
- {
- @Override
- public void menuSelected(MenuEvent me) {
- if (_tabbedPane.getTabCount() > 0) {
-
- windowMenu.removeAll();
-
- JMenuItem window_restart=new JMenuItem(" Restart ");
- window_restart.addActionListener(new WindowRestartAction());
- windowMenu.add(window_restart);
-
- windowMenu.addSeparator();
-
- int tabcount = _tabbedPane.getTabCount();
- String tabtext=_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
- for (int i = 0; i < tabcount; i++) {
- String title = _tabbedPane.getTitleAt(i);
- JCheckBoxMenuItem witem=new JCheckBoxMenuItem(title);
- witem.addActionListener(new Window_MenuItemsAction());
- if(tabtext.equals(title)){
- witem.setSelected(true);
- }
- windowMenu.add(witem);
- }
- }
- }
-
- @Override
- public void menuDeselected(MenuEvent me) {
- }
-
- @Override
- public void menuCanceled(MenuEvent me) {
- }
- }
-
-
-
- class WindowRestartAction implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
- File_CloseAll_Action();
- dispose();
- count = 1;
- LookAndFeelAction.setBasicLookAndFeel();
- }
- }
-
-
-
-
-
- class Window_MenuItemsAction implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent ae) {
-
- String menutext=ae.getActionCommand().trim();
-
- if (_tabbedPane.getTabCount() > 0) {
- int tabcount = _tabbedPane.getTabCount();
- for (int i = 0; i < tabcount; i++) {
- String title = _tabbedPane.getTitleAt(i).trim();
- if (title.contains("*")) {
- title = title.replace("*", "").trim();
- }
-
- if (title.equals(menutext)) {
- _tabbedPane.setSelectedIndex(i);
- setTitle("Tabbed Notepad in Java - [ " + _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex()) + " ]");
- }
- }
- }
- }
- }
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,
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- import java.io.IOException;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.DOMException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
-
- public final class FontAction extends JDialog implements ListSelectionListener,ActionListener
- {
- String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
- String[] fontStyles={
- " Plain ",
- " Bold ",
- " Italic ",
- " Plain+Bold ",
- " Plain+Italic ",
- " Bold+Italic ",
- " Plain+Bold+Italic "
- };
- List lst=new List();
- JList fontsList;
- JList fontStyleList;
- JList fontSizeList;
- JPanel jp1,jp2;
- DefaultListModel model;
- JLabel displayLabel;
- JButton ok,cancel;
- JTextPane textPane;
-
- static boolean isDarkTheme = false;
-
- public FontAction(JTextPane tx)
- {
- textPane=tx;
-
- Container cp=getContentPane();
-
- isDarkTheme = getNodeTextContent("lookAndFeel").equals("GlobalDark");
-
- fontsList=new JList(fontNames);
- fontStyleList=new JList(fontStyles);
-
- fontsList.setFont(new Font("Calibri",Font.PLAIN,14));
- fontStyleList.setFont(new Font("Calibri",Font.PLAIN,14));
-
- model=new DefaultListModel();
- fontSizeList = new JList(model);
- fontSizeList.setFont(new Font("Calibri", Font.PLAIN, 14));
-
-
- for(int i=1;i<=160;i++)
- {
- model.addElement(" "+i+" ");
- }
-
-
- fontsList.setSelectedIndex(8);
- fontStyleList.setSelectedIndex(0);
- fontSizeList.setSelectedIndex(21);
-
- fontsList.addListSelectionListener(this);
- fontStyleList.addListSelectionListener(this);
- fontSizeList.addListSelectionListener(this);
-
- jp1=new JPanel();
- jp2=new JPanel();
- JPanel jp3=new JPanel();
- jp3.add(new JScrollPane(fontsList));
-
-
- JPanel jp4=new JPanel();
- jp4.setLayout(new GridLayout(0,2));
- jp4.add(new JScrollPane(fontStyleList));
- jp4.add(new JScrollPane(fontSizeList));
-
- jp1.add(jp3,BorderLayout.WEST);
- jp1.add(jp4,BorderLayout.EAST);
-
- displayLabel=new JLabel("Java Programming",JLabel.CENTER);
- displayLabel.setFont(new Font("Arial",Font.PLAIN,21));
-
- jp1.add(displayLabel);
-
- ok=new JButton(" OK ");
- cancel=new JButton(" Cancel ");
-
- if (isDarkTheme) {
- fontsList.setBackground(new Color(40, 40, 40));
- fontStyleList.setBackground(new Color(40, 40, 40));
- fontSizeList.setBackground(new Color(40, 40, 40));
- displayLabel.setForeground(new Color(240, 240, 240));
- }
-
- ok.addActionListener(this);
- cancel.addActionListener(this);
-
- jp2.add(ok);
- jp2.add(cancel);
-
- cp.add(jp1,BorderLayout.CENTER);
- cp.add(jp2,BorderLayout.SOUTH);
-
- }
-
- @Override
- public void valueChanged(ListSelectionEvent evt)
- {
- String fontname=fontsList.getSelectedValue().toString();
- String fontstyle=fontStyleList.getSelectedValue().toString().trim();
- int fontsize=Integer.parseInt(fontSizeList.getSelectedValue().toString().trim());
-
- switch(fontstyle)
- {
- case "Plain":
- displayLabel.setFont(new Font(fontname, Font.PLAIN, fontsize));
- break;
-
- case "Bold":
- displayLabel.setFont(new Font(fontname, Font.BOLD, fontsize));
- break;
-
- case "Italic":
- displayLabel.setFont(new Font(fontname, Font.ITALIC, fontsize));
- break;
-
- case "Plain+Bold":
- displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.BOLD, fontsize));
- break;
-
- case "Plain+Italic":
- displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.ITALIC, fontsize));
- break;
-
- case "Bold+Italic":
- displayLabel.setFont(new Font(fontname, Font.BOLD + Font.ITALIC, fontsize));
- break;
-
- case "Plain+Bold+Italic":
- displayLabel.setFont(new Font(fontname, Font.PLAIN + Font.BOLD + Font.ITALIC, fontsize));
- break;
- }
- }
-
- @Override
- public void actionPerformed(ActionEvent evt)
- {
- Object source=evt.getSource();
- if(source==ok)
- {
- String fontname = fontsList.getSelectedValue().toString();
- String fontstyle = fontStyleList.getSelectedValue().toString().trim();
- int fontsize = Integer.parseInt(fontSizeList.getSelectedValue().toString().trim());
-
- switch (fontstyle)
- {
- case "Plain":
- textPane.setFont(new Font(fontname, Font.PLAIN, fontsize));
- break;
-
- case "Bold":
- textPane.setFont(new Font(fontname, Font.BOLD, fontsize));
- break;
-
- case "Italic":
- textPane.setFont(new Font(fontname, Font.ITALIC, fontsize));
- break;
-
- case "Plain+Bold":
- textPane.setFont(new Font(fontname, Font.PLAIN + Font.BOLD, fontsize));
- break;
-
- case "Plain+Italic":
- textPane.setFont(new Font(fontname, Font.PLAIN + Font.ITALIC, fontsize));
- break;
-
- case "Bold+Italic":
- textPane.setFont(new Font(fontname, Font.BOLD + Font.ITALIC, fontsize));
- break;
-
- case "Plain+Bold+Italic":
- textPane.setFont(new Font(fontname, Font.PLAIN + Font.BOLD + Font.ITALIC, fontsize));
- break;
- }
-
- this.dispose();
- }
- else if(source==cancel)
- {
- this.dispose();
- }
- }
-
-
-
-
-
-
- public String getNodeTextContent(String nodetag) {
- String content = "";
-
- try {
-
- File fXmlFile = new File("files/viewsfile.xml");
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document doc = dBuilder.parse(fXmlFile);
-
- doc.getDocumentElement().normalize();
-
- NodeList nList = doc.getElementsByTagName("views");
-
- for (int temp = 0; temp < nList.getLength(); temp++) {
-
- Node nNode = nList.item(temp);
-
- Element eElement = (Element) nNode;
-
- content = eElement.getElementsByTagName(nodetag).item(0).getTextContent();
-
- }
- } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {
- }
-
- return content;
-
- }
- }
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.
- class SelectTabFromListItem implements ListSelectionListener
- {
- public void valueChanged(ListSelectionEvent evt)
- {
- if(_list.getSelectedValue()!=null)
- {
- String list_item=_list.getSelectedValue().toString().trim();
-
- if(_tabbedPane.getTabCount() >0)
- {
- int tabcount=_tabbedPane.getTabCount();
- for(int i=0;i<tabcount;i++)
- {
- String title=_tabbedPane.getTitleAt(i).trim();
- if (title.contains("*"))
- {
- title = title.replace("*", "").trim();
- }
-
- if(title.equals(list_item))
- {
- _tabbedPane.setSelectedIndex(i);
- setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");
- }
- }
- }
-
- }
- }
- }
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.
- class TabChanged implements ChangeListener
- {
- @Override
- public void stateChanged(ChangeEvent evt)
- {
- if(_tabbedPane.getTabCount()>0)
- {
- Object[] files=filesHoldListModel.toArray();
- String tabtext=_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex()).trim();
- if(tabtext.contains("*"))
- {
- tabtext=tabtext.replace("*", "");
- }
-
- for(Object filename : files)
- {
- String file=filename.toString().substring(filename.toString().lastIndexOf("\\")+1);
-
- if(file.equals(tabtext))
- {
- filenameLabel.setText(filename.toString());
- setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");
- }
- }
-
- if(tabtext.contains("Document "))
- {
- filenameLabel.setText(tabtext);
- setTitle("Tabbed Notepad in Java - [ "+_tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex())+" ]");
- }
-
- }
- }
- }
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.
- class JavaGlobalDarkTheme {
-
- DefaultMetalTheme darkTheme = new DefaultMetalTheme() {
-
- @Override
- public ColorUIResource getPrimary1() {
- return new ColorUIResource(new Color(30, 30, 30));
- }
-
-
- @Override
- public ColorUIResource getPrimary2() {
- return new ColorUIResource(new Color(20, 20, 20));
- }
-
- @Override
- public ColorUIResource getPrimary3() {
- return new ColorUIResource(new Color(30, 30, 30));
- }
-
- @Override
- public ColorUIResource getBlack(){
- return new ColorUIResource(new Color(30, 30, 30));
- }
-
- @Override
- public ColorUIResource getWhite() {
- return new ColorUIResource(new Color(240, 240, 240));
- }
-
-
- @Override
- public ColorUIResource getMenuForeground() {
- return new ColorUIResource(new Color(200, 200, 200));
- }
-
- @Override
- public ColorUIResource getMenuBackground() {
- return new ColorUIResource(new Color(25, 25, 25));
- }
-
- @Override
- public ColorUIResource getMenuSelectedBackground(){
- return new ColorUIResource(new Color(50, 50, 50));
- }
-
- @Override
- public ColorUIResource getMenuSelectedForeground() {
- return new ColorUIResource(new Color(255, 255, 255));
- }
-
-
- @Override
- public ColorUIResource getSeparatorBackground() {
- return new ColorUIResource(new Color(15, 15, 15));
- }
-
-
- @Override
- public ColorUIResource getUserTextColor() {
- return new ColorUIResource(new Color(240, 240, 240));
- }
-
- @Override
- public ColorUIResource getTextHighlightColor() {
- return new ColorUIResource(new Color(80, 40, 80));
- }
-
-
- @Override
- public ColorUIResource getAcceleratorForeground(){
- return new ColorUIResource(new Color(30, 30,30));
- }
-
-
- @Override
- public ColorUIResource getWindowTitleInactiveBackground() {
- return new ColorUIResource(new Color(30, 30, 30));
- }
-
-
- @Override
- public ColorUIResource getWindowTitleBackground() {
- return new ColorUIResource(new Color(30, 30, 30));
- }
-
-
- @Override
- public ColorUIResource getWindowTitleForeground() {
- return new ColorUIResource(new Color(230, 230, 230));
- }
-
- @Override
- public ColorUIResource getPrimaryControlHighlight() {
- return new ColorUIResource(new Color(40, 40, 40));
- }
-
- @Override
- public ColorUIResource getPrimaryControlDarkShadow() {
- return new ColorUIResource(new Color(40, 40, 40));
- }
-
- @Override
- public ColorUIResource getPrimaryControl() {
-
- return new ColorUIResource(new Color(60, 60, 60));
- }
-
- @Override
- public ColorUIResource getControlHighlight() {
- return new ColorUIResource(new Color(20, 20, 20));
- }
-
- @Override
- public ColorUIResource getControlDarkShadow() {
- return new ColorUIResource(new Color(50, 50, 50));
- }
-
- @Override
- public ColorUIResource getControl() {
- return new ColorUIResource(new Color(25, 25, 25));
- }
-
- @Override
- public ColorUIResource getControlTextColor() {
- return new ColorUIResource(new Color(230, 230, 230));
- }
-
- @Override
- public ColorUIResource getFocusColor() {
- return new ColorUIResource(new Color(0, 100, 0));
- }
-
- @Override
- public ColorUIResource getHighlightedTextColor() {
- return new ColorUIResource(new Color(250, 250, 250));
- }
-
- };
- }
- public static void setGlobalDarkLookAndFeel()
- {
- MetalLookAndFeel.setCurrentTheme(new JavaGlobalDarkTheme().darkTheme);
- try {
- UIManager.setLookAndFeel(new MetalLookAndFeel());
- } catch (Exception ev) {
- }
-
- JFrame.setDefaultLookAndFeelDecorated(true);
- TabbedNotepad tb = new TabbedNotepad();
- tb.setExtendedState(JFrame.MAXIMIZED_BOTH);
- BufferedImage image = null;
- try {
- image = ImageIO.read(tb.getClass().getResource("resources/myicon.png"));
- } catch (IOException e) {
- }
- tb.setIconImage(image);
- tb.setSize(800, 600);
- tb.setLocation(100, 50);
- tb.setVisible(true);
-
- }