[Java swing] JToolBar trong Java

JToolBar là một thanh dài gồm các nút với biểu tượng tương ứng thường nằm dưới menu, giống như thanh công cụ trong word, excel có các nút new, open, save, …

JToolBar trong java

Chúng ta sẽ đi thực hiện chương trình giống như hình trên, gồm 3 nút new, open, save trên thanh toolbar. Khi click vào các nút thì JTextArea bên dưới sẽ hiển thị sự kiện tương ứng.

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();
	}
}

Thanh ToolBar này các bạn có thể đưa chuột vào đầu thanh (đoạn có nhiều dấu chấm) để kéo thả đặt nó ở vị trí ngang hoặc dọc. Nếu muốn cố định nó bạn dùng phương thức toolBar.setFloatable(false);, ngoài ra bạn có thể thêm các đối tượng khác như JTextField,… để làm thanh tìm kiếm chẳng hạn.

Các icon new, open, save bạn để cùng pakage như sau:

jtoolbar in java

Đọc thêm: class JToolBar, user JToolBar, java swing