Hi friend,
Please implement following code.
import java.awt.*;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
public class ExampleGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JTabbedPane tabbedPane;
private JTextPane messageArea;
private class NewTabListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
StyledDocument doc = (StyledDocument)messageArea.getDocument();
try{
doc.insertString(doc.getLength(), "new line\n", null);
}
catch (BadLocationException e){
e.printStackTrace();
}
tabbedPane.add("tab", new JPanel());
}
}
private GridBagConstraints setGridBagConstraints(int gridx,
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int fill,
int anchor,
int insets1,
int insets2,
int insets3,
int insets4){
GridBagConstraints gridConstr = new GridBagConstraints();
gridConstr.gridx = gridx;
gridConstr.gridy = gridy;
gridConstr.gridwidth = gridwidth;
gridConstr.gridheight = gridheight;
gridConstr.weightx = weightx;
gridConstr.weighty = weighty;
gridConstr.fill = fill;
gridConstr.anchor = anchor;
/* Insets(int top, int left, int bottom, int right) */
gridConstr.insets = new Insets(insets1, insets2, insets3, insets4);
return gridConstr;
}
public ExampleGUI(){
this.setTitle("Example GUI");
this.setResizable(true);
this.setSize(600, 400);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
messageArea = new JTextPane();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gridConstr;
gridConstr = setGridBagConstraints(0, 0, 1, 1, 1.0, 90.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0, 0, 0);
mainPanel.add(tabbedPane, gridConstr);
gridConstr = setGridBagConstraints(0, 1, 1, 1, 1.0, 10.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0, 0, 0);
mainPanel.add(messageArea, gridConstr);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem newTab = new JMenuItem("new tab");
menu.add(newTab);
menuBar.add(menu);
newTab.addActionListener(new NewTabListener());
JMenuItem newTab1 = new JMenuItem("Open");
menu.add(newTab1);
menuBar.add(menu);
JMenu menu1 = new JMenu("Edit");
JMenuItem newTab2 = new JMenuItem("Undo");
menu1.add(newTab2);
menuBar.add(menu1);
this.setJMenuBar(menuBar);
this.getContentPane().add(mainPanel);
}
public static void main(String[] args){
ExampleGUI exampleGUI = new ExampleGUI();
exampleGUI.setVisible(true);
}
}
-------------------------------------------
Visit for more information.
http://www.roseindia.net/java/example/java/swing/Thanks.