[Java swing] JToolBar in Java
JToolBar is a long bar with icons including the corresponding button is usually located under the menu, like toolbars in Word, excel with new buttons, open, save, …
We will implement programs like the image above, including 3 new button, open, Save on the toolbar. When you click on the button below JTextArea will display the corresponding event.
package nguyenvanquan7826.JToolBar; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- */ public class DemoJToolBar extends JFrame implements ActionListener { public static final String NEW = "new"; public static final String OPEN = "open"; public static final String SAVE = "save"; public static String iconNew = "iconNew.png"; public static String iconOpen = "iconOpen.png"; public static String iconSave = "iconSave.png"; private JTextArea ta; public DemoJToolBar() { setJMenuBar(createMenuBar()); addContent(); setDisplay(); } /** * set display for JFrame with title is "Demo JToolBar" */ private void setDisplay() { setTitle("Demo JToolBar"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * demo menu */ private JMenuBar createMenuBar() { JMenuBar mb = new JMenuBar(); mb.add(new JMenu("File")); mb.add(new JMenu("Help")); return mb; } /** * add content of JFrame, it contain a JToolBar and a JTextArea to display * event on toolbar */ private void addContent() { setLayout(new BorderLayout()); add(createToolBar(), BorderLayout.PAGE_START); add(new JScrollPane(ta = createTextArea(15, 30)), BorderLayout.CENTER); } /** * create a toolbar */ private JToolBar createToolBar() { JToolBar tb = new JToolBar(); tb.add(createButton(iconNew, "New", NEW, "Create a new file")); tb.add(createButton(iconOpen, "Open", OPEN, "Open a file")); tb.add(createButton(iconSave, "Save", SAVE, "Save this file")); return tb; } /** * create a JTextArea to display events */ private JTextArea createTextArea(int row, int col) { JTextArea ta = new JTextArea(row, col); ta.setWrapStyleWord(true); ta.setLineWrap(true); // ta.setEditable(false); return ta; } /** * create a Button, it has a icon, tex (if not found icon), actionCommand * and tool tip text */ private JButton createButton(String iconLink, String text, String command, String toolTip) { JButton btn = new JButton(); btn.setActionCommand(command); btn.setToolTipText(toolTip); ImageIcon icon = createIcon(iconLink); if (icon == null) { btn.setText(text); } else { btn.setIcon(icon); } btn.setActionCommand(command); btn.addActionListener(this); return btn; } /** * create icon */ private ImageIcon createIcon(String iconLink) { URL imageURL = JToolBarOracle.class.getResource(iconLink); if (imageURL != null) { return new ImageIcon(imageURL); } else { System.out.println("image " + iconLink + " not found"); return null; } } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); String action = "null"; if (command.equals(NEW)) { action = "new file"; } else if (command.equals(OPEN)) { action = "open file"; } else if (command.equals(SAVE)) { action = "save file"; } ta.append(action + "\n"); } public static void main(String[] args) { new DemoJToolBar(); } }
This toolbar you can put your mouse on top bar (sections with multiple dots) Drag put it in horizontal or vertical position. If you want fixed it using methods toolBar.setFloatable(false);, In addition, you can add other objects such as JTextField,… to make the search bar such.
The icon new, open, save pakage you to the following:
Read more: class JToolBar, user JToolBar, java swing
Thank you for this article offline. I'm learning about this part are met JToolBar few places with, Your instructions were oke then
Glad to post something for your help! visit the blog regularly offline!