I am creating a text editor similar to gedit. In toolbar, i have used 10 buttons with icons. I am using a JFrame
with BorderLayout
. Toolbar is placed at north. But the problem is that the buttons are becoming too big and toolbar is covering almost half of the frame. All the icons are of size 512x512px. I tried resizing the icons but it didnt work. It works properly if the icons of the images are removed.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class RSP extends WindowAdapter implements ActionListener, CaretListener
{
private JFrame f;
private JMenuBar mb;
private JMenu m1, m2, m3, m4, m5, m6, m7, m16;
private JMenuItem m11, m12, m13, m14, m15, m17, m18, m21, m22, m23, m24, m25, m26, m27, m28, m31, m32, m33, m41, m42, m51, m52, m53, m61, m62, m71, m72, m161, m162, m163;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
private JToolBar tb;
private JTabbedPane tp;
private JLabel l;
private JTextArea[] ta;
private int caretPos, lineNum, columnNum;
public RSP()
{
f=new JFrame("RSPEditor");
mb=new JMenuBar();
CreateMenuBar();
tb=new JToolBar();
tb.setFloatable(false);
CreateToolbar();
tp=new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
l=new JLabel("",JLabel.TRAILING);
ta=new JTextArea[10];
f.setSize(800,550);
f.setVisible(true);
f.setJMenuBar(mb);
f.add(tb,BorderLayout.NORTH);
f.add(tp,BorderLayout.CENTER);
f.add(l,BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addWindowListener(this);
}
public void CreateToolbar()
{
tb.setLayout(new GridLayout(1,10,5,5));
tb.setSize(f.getWidth(),20);
b1=new JButton("New",new ImageIcon(getClass().getResource("new.png")));
b2=new JButton("Open",new ImageIcon(getClass().getResource("open.png")));
b3=new JButton("Save",new ImageIcon(getClass().getResource("save.png")));
b4=new JButton("Undo",new ImageIcon(getClass().getResource("undo.png")));
b5=new JButton("Redo",new ImageIcon(getClass().getResource("redo.png")));
b6=new JButton("Cut",new ImageIcon(getClass().getResource("cut.png")));
b7=new JButton("Copy",new ImageIcon(getClass().getResource("copy.png")));
b8=new JButton("Paste",new ImageIcon(getClass().getResource("paste.png")));
b9=new JButton("Find",new ImageIcon(getClass().getResource("find.png")));
b10=new JButton("Replace",new ImageIcon(getClass().getResource("replace.png")));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.add(b5);
tb.add(b6);
tb.add(b7);
tb.add(b8);
tb.add(b9);
tb.add(b10);
}
public void CreateMenuBar()
{
m1=new JMenu("File");
m2=new JMenu("Edit");
m3=new JMenu("Search");
m4=new JMenu("Execute");
m5=new JMenu("Tools");
m6=new JMenu("Documents");
m7=new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);
mb.add(m6);
mb.add(m7);
m11=new JMenuItem("New");
m12=new JMenuItem("Open");
m13=new JMenuItem("Save");
m14=new JMenuItem("Save As");
m15=new JMenuItem("Print");
m16=new JMenu("Recent Files");
m161=new JMenuItem("A");
m162=new JMenuItem("B");
m163=new JMenuItem("C");
m16.add(m161);
m16.add(m162);
m16.add(m163);
m17=new JMenuItem("Close");
m18=new JMenuItem("Quit");
m21=new JMenuItem("Undo");
m22=new JMenuItem("Redo");
m23=new JMenuItem("Cut");
m24=new JMenuItem("Cop[y");
m25=new JMenuItem("Paste");
m26=new JMenuItem("Delete");
m27=new JMenuItem("Select All");
m28=new JMenuItem("Insert Date and Time");
m31=new JMenuItem("Find");
m32=new JMenuItem("Replace");
m33=new JMenuItem("Goto Line");
m41=new JMenuItem("Compile");
m42=new JMenuItem("Run");
m51=new JMenuItem("Indent");
m52=new JMenuItem("Spell Check");
m53=new JMenuItem("Full Screen");
m61=new JMenuItem("Save All");
m62=new JMenuItem("Close All");
m71=new JMenuItem("How To");
m72=new JMenuItem("About");
m1.add(m11);
m1.add(m12);
m1.addSeparator();
m1.add(m13);
m1.add(m14);
m1.addSeparator();
m1.add(m15);
m1.addSeparator();
m1.add(m16);
m1.addSeparator();
m1.add(m17);
m1.add(m18);
m2.add(m21);
m2.add(m22);
m2.addSeparator();
m2.add(m23);
m2.add(m24);
m2.add(m25);
m2.add(m26);
m2.addSeparator();
m2.add(m27);
m2.addSeparator();
m2.add(m28);
m3.add(m31);
m3.add(m32);
m3.addSeparator();
m3.add(m33);
m4.add(m41);
m4.add(m42);
m5.add(m51);
m5.add(m52);
m5.addSeparator();
m5.add(m53);
m6.add(m61);
m6.add(m62);
m7.add(m71);
m7.add(m72);
m11.addActionListener(this);
m11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
m12.addActionListener(this);
m12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
m13.addActionListener(this);
m13.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
m14.addActionListener(this);
m14.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
m15.addActionListener(this);
m15.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
m161.addActionListener(this);
m162.addActionListener(this);
m163.addActionListener(this);
m17.addActionListener(this);
m17.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
m18.addActionListener(this);
m18.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
m21.addActionListener(this);
m21.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
m22.addActionListener(this);
m23.addActionListener(this);
m23.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
m24.addActionListener(this);
m24.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
m25.addActionListener(this);
m25.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
m26.addActionListener(this);
m27.addActionListener(this);
m27.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
m28.addActionListener(this);
m31.addActionListener(this);
m31.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
m32.addActionListener(this);
m32.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
m33.addActionListener(this);
m33.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
m41.addActionListener(this);
m42.addActionListener(this);
m42.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F9, ActionEvent.CTRL_MASK));
m51.addActionListener(this);
m52.addActionListener(this);
m53.addActionListener(this);
m61.addActionListener(this);
m62.addActionListener(this);
m71.addActionListener(this);
m72.addActionListener(this);
}
public void createNewTab()
{
ta[tp.getTabCount()]=new JTextArea();
ta[tp.getTabCount()].addCaretListener(this);
ta[tp.getTabCount()].setFont(new Font("Ariel",Font.PLAIN,20));
tp.addTab("New File",new JScrollPane(ta[tp.getTabCount()]));
}
public void windowClosing(WindowEvent e) //prompt for save
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1) || e.getSource().equals(m11))
createNewTab();
else if(e.getSource().equals(b2) || e.getSource().equals(m12));
//fileOpen();
else if(e.getSource().equals(b3) || e.getSource().equals(m13));
//save;
else if(e.getSource().equals(m14));
//saveas
else if(e.getSource().equals(m15));
//print
else if(e.getSource().equals(m161));
//recent1
else if(e.getSource().equals(m162));
//recent2
else if(e.getSource().equals(m163));
//recent3
else if(e.getSource().equals(m17));
//close
else if(e.getSource().equals(m18));
//quit
else if(e.getSource().equals(b4) || e.getSource().equals(m21));
//undo
else if(e.getSource().equals(b5) || e.getSource().equals(m22));
//redo
else if(e.getSource().equals(b6) || e.getSource().equals(m23));
//cut
else if(e.getSource().equals(b7) || e.getSource().equals(m24));
//copy
else if(e.getSource().equals(b8) || e.getSource().equals(m25));
//paste
else if(e.getSource().equals(m16));
//delete
else if(e.getSource().equals(m17));
//select all
else if(e.getSource().equals(m18));
//insert d&t
else if(e.getSource().equals(b9) || e.getSource().equals(m31));
//find
else if(e.getSource().equals(b10) || e.getSource().equals(m32));
//replace
else if(e.getSource().equals(m33));
//goto
else if(e.getSource().equals(m41));
//compile
else if(e.getSource().equals(m42));
//run
else if(e.getSource().equals(m51));
//indent
else if(e.getSource().equals(m52));
//spell chk
else if(e.getSource().equals(m53));
//fullscreen
else if(e.getSource().equals(m61));
//saveall
else if(e.getSource().equals(m62));
//closeall
else if(e.getSource().equals(m71));
//howto
else if(e.getSource().equals(m72));
//about
}
public void caretUpdate(CaretEvent e)
{
JTextArea editArea=(JTextArea)e.getSource();
lineNum=1;
columnNum=1;
try
{
caretPos=editArea.getCaretPosition();
lineNum=editArea.getLineOfOffset(caretPos);
columnNum=caretPos-editArea.getLineStartOffset(lineNum);
lineNum++;
l.setText("L "+lineNum+", C "+columnNum+" ");;
}
catch(Exception ex) { }
}
public static void main(String args[])
{
new RSP();
}
}
scale the Images the following way and it should work
new JButton("New", new ImageIcon(new ImageIcon(getClass().getResource("new.png")).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH)));
I am scaling to 50X50 you can use any proper size that suit your application