Sometime we need to have something like multiple tabs like in chrome we have. So here i am posting a simple example of JTabbedPane which may help you in any way.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class TabDemo extends JFrame
{
JTabbedPane tb;
JTextArea txt;
private int tabCounter = 1;
public TabDemo()
{
tb = new JTabbedPane();
addTab();
setJMenuBar(createMenuBar());
getContentPane().add(tb,BorderLayout.CENTER);
//addTab();
setVisible(true);
setBounds(20,20,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenuBar createMenuBar()
{
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem newFile = new JMenuItem("New");
newFile.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_DOWN_MASK));
newFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addTab();
}
});
JMenuItem saveFile = new JMenuItem("Save");
saveFile.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK));
saveFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int x = tb.getSelectedIndex();
JScrollPane jsp = (JScrollPane)tb.getComponentAt(x);
JTextArea tx = (JTextArea) jsp.getViewport().getView();
//JOptionPane.showMessageDialog(TabDemo.this,tx.getText());
fileSaver(tx.getText());
}
});
file.add(newFile);
file.add(saveFile);
mb.add(file);
return mb;
}
void addTab()
{
txt = new JTextArea();
JScrollPane txtPnl = new JScrollPane(txt);
JPanel tbPnl = new JPanel();
tbPnl.setOpaque(false);
tbPnl.setLayout(new FlowLayout());
JLabel lbl = new JLabel("Tab - "+tabCounter);
JButton btnClose = new JButton("X");
tbPnl.add(lbl);
tbPnl.add(btnClose);
tb.addTab(null,txtPnl);
btnClose.setActionCommand(""+tabCounter);
btnClose.setFont(new Font("Serif",1,9));
lbl.setFont(new Font("Serif",1,9));
btnClose.setOpaque(false);
btnClose.setBorder(null);
btnClose.setBackground(Color.RED);
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton btn =(JButton) e.getSource();
String str = btn.getActionCommand();
for(int i =0 ;i <tb.getTabCount();i++)
{
JPanel pnl = (JPanel)tb.getTabComponentAt(i);
btn = (JButton)pnl.getComponent(1);
String str2 = btn.getActionCommand();
if(str.equals(str2))
{
tb.removeTabAt(i);
tabCounter--;
break;
}
}
}
};
btnClose.addActionListener(al);
//tb.setTabComponentAt(tb.getTabCount()-1 ,lbl);
tb.setTabComponentAt(tb.getTabCount()-1 , tbPnl);
tb.setSelectedIndex(tb.getTabCount() - 1);
tabCounter++;
}
public static void main(String []args)
{
new TabDemo();
}
private void fileSaver(String text)
{
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(TabDemo.this);
/*if(result != JFileChooser.APPROVE_OPTION)
{ return false; }*/
File file = chooser.getSelectedFile();
setTitle(file.getName());
try
{
FileOutputStream out = new FileOutputStream(file);
//String str = txt.getText();
byte b[] = text.getBytes();
out.write(b);
out.close();
//return true;
}
catch(Exception ex){}//return false;}
}
}
Now Type the above code in text editor save it as TabDemo.java.
Then compile it by command javac TabDemo.java
and execute it by command java TabDemo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class TabDemo extends JFrame
{
JTabbedPane tb;
JTextArea txt;
private int tabCounter = 1;
public TabDemo()
{
tb = new JTabbedPane();
addTab();
setJMenuBar(createMenuBar());
getContentPane().add(tb,BorderLayout.CENTER);
//addTab();
setVisible(true);
setBounds(20,20,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenuBar createMenuBar()
{
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem newFile = new JMenuItem("New");
newFile.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_DOWN_MASK));
newFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addTab();
}
});
JMenuItem saveFile = new JMenuItem("Save");
saveFile.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK));
saveFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int x = tb.getSelectedIndex();
JScrollPane jsp = (JScrollPane)tb.getComponentAt(x);
JTextArea tx = (JTextArea) jsp.getViewport().getView();
//JOptionPane.showMessageDialog(TabDemo.this,tx.getText());
fileSaver(tx.getText());
}
});
file.add(newFile);
file.add(saveFile);
mb.add(file);
return mb;
}
void addTab()
{
txt = new JTextArea();
JScrollPane txtPnl = new JScrollPane(txt);
JPanel tbPnl = new JPanel();
tbPnl.setOpaque(false);
tbPnl.setLayout(new FlowLayout());
JLabel lbl = new JLabel("Tab - "+tabCounter);
JButton btnClose = new JButton("X");
tbPnl.add(lbl);
tbPnl.add(btnClose);
tb.addTab(null,txtPnl);
btnClose.setActionCommand(""+tabCounter);
btnClose.setFont(new Font("Serif",1,9));
lbl.setFont(new Font("Serif",1,9));
btnClose.setOpaque(false);
btnClose.setBorder(null);
btnClose.setBackground(Color.RED);
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton btn =(JButton) e.getSource();
String str = btn.getActionCommand();
for(int i =0 ;i <tb.getTabCount();i++)
{
JPanel pnl = (JPanel)tb.getTabComponentAt(i);
btn = (JButton)pnl.getComponent(1);
String str2 = btn.getActionCommand();
if(str.equals(str2))
{
tb.removeTabAt(i);
tabCounter--;
break;
}
}
}
};
btnClose.addActionListener(al);
//tb.setTabComponentAt(tb.getTabCount()-1 ,lbl);
tb.setTabComponentAt(tb.getTabCount()-1 , tbPnl);
tb.setSelectedIndex(tb.getTabCount() - 1);
tabCounter++;
}
public static void main(String []args)
{
new TabDemo();
}
private void fileSaver(String text)
{
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(TabDemo.this);
/*if(result != JFileChooser.APPROVE_OPTION)
{ return false; }*/
File file = chooser.getSelectedFile();
setTitle(file.getName());
try
{
FileOutputStream out = new FileOutputStream(file);
//String str = txt.getText();
byte b[] = text.getBytes();
out.write(b);
out.close();
//return true;
}
catch(Exception ex){}//return false;}
}
}
Now Type the above code in text editor save it as TabDemo.java.
Then compile it by command javac TabDemo.java
and execute it by command java TabDemo
No comments:
Post a Comment