Tuesday, August 27, 2013

JToolBar

JToolBar is a container that can be used to display commonly used controls in a program. You can drag and drop the JToolBar in a location of the window or drag it out into a separate window.  The summary of the JToolBar constructors and useful methods are shown below.

Constructors of the JToolBar class:
-JToolBar()
creates a horizontal tool bar without name;
-JToolBar(String name)
creates a horizontal tool bar with the specified name.
-JToolBar(int orient)
creates a tool bar with the specified orientation.
-JToolBar(String name, int orient)
creates a tool bar with the specified name, and orientation.

Useful methods of the JToolBar class:
-add(Action action):JButton
adds a new JButton to the tool bar.
-add(Component c):Component
adds a new component to the tool bar.
-addSeparator():void
adds the separator to the tool bar to separate its components.
-setFloatable(boolean float):boolean
allows or does not allow the tool bar to be moved or dragged.

Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

class JToolBarShow extends JFrame implements ItemListener{
JTextArea txta;
JComboBox cbfontnames;
JComboBox cbfontsizes;
JComboBox cbfontcolors;
JToolBarShow(String title){
setTitle(title);
setSize(550,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JMenuBar mbar=new JMenuBar();
JMenu mfile=new JMenu("File");
JMenuItem itemnew=new JMenuItem(new AAction("New",new ImageIcon("d:/newicon.png")));
mfile.add(itemnew);
JMenuItem itemclose=new JMenuItem(new AAction("Close",new ImageIcon("d:/closeicon.png")));
mfile.add(itemclose);
mbar.add(mfile);
setJMenuBar(mbar);
txta=new JTextArea(10,30);
JScrollPane scroll=new JScrollPane(txta);
JToolBar toolbar=new JToolBar();
toolbar.add(new JButton(new AAction("Copy",new ImageIcon("d:/copyicon.png"))));
toolbar.add(new JButton(new AAction("Paste",new ImageIcon("d:/pasteicon.png"))));
toolbar.add(new JButton(new AAction("Wrap",new ImageIcon("d:/wrapicon.png"))));
cbfontnames=new JComboBox(fnames());
cbfontsizes=new JComboBox(sizes());
cbfontcolors=new JComboBox(colors());
cbfontnames.addItemListener(this);
cbfontsizes.addItemListener(this);
cbfontcolors.addItemListener(this);
toolbar.add(cbfontnames);
toolbar.add(cbfontsizes);
toolbar.add(cbfontcolors);
toolbar.setFloatable(true);
contpane.add(scroll,BorderLayout.CENTER);
contpane.add(toolbar,BorderLayout.SOUTH);
cbfontnames.setSelectedItem("Arial");
cbfontsizes.setSelectedItem(12);
cbfontcolors.setSelectedItem(0);
validate();

}
class AAction extends AbstractAction{

AAction(String text){
super(text);

}

AAction(String text,Icon c){
super(text, c);

}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("New"))
txta.setText("");

else if(e.getActionCommand().equals("Close"))
System.exit(0);
else if(e.getActionCommand().equals("Wrap"))
{
if(txta.getLineWrap()==false)
txta.setLineWrap(true);
else
txta.setLineWrap(false);
}
else if(e.getActionCommand().equals("Copy"))
txta.copy();
else if(e.getActionCommand().equals("Paste"))
txta.paste();
}
}

public void itemStateChanged(ItemEvent e){
setFontToTextArea(cbfontnames.getSelectedItem(),cbfontcolors.getSelectedItem(),cbfontsizes.getSelectedItem());
}

public String[] fnames(){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
return ge.getAvailableFontFamilyNames();

}
public Object[] sizes(){
Object[] fsizes={8,9,10,11,12,13,14,15,16,17,18,19,20,30,31,32,33,36,40,45};
return(fsizes);
}

public Object[] colors(){
Object[] tcolor={0x000000,0xfffff,0xffff00,0xff0000,0xff00ff};
return(tcolor);
}

public void setFontToTextArea(Object fname,Object fcolor,Object fsize){
String fontname=fname.toString();
Color forecolor=new Color(Integer.parseInt(fcolor.toString()));
int fontsize=Integer.parseInt(fsize.toString());
txta.setFont(new Font(fontname,Font.PLAIN,fontsize));
txta.setForeground(forecolor);
}

}

public class JFrameSwing {
public static void main(String[] args){
new JToolBarShow("JToolBar");
}
}

JToolBar Swing

No comments:

Post a Comment