jtoolbar
The JToolBar
class is a container that provides a set of buttons, combo boxes, and other components for use in a toolbar. A JToolBar
object can be positioned on any side of a window, and it can contain any components that you want to include.
Here's an example of how to create a simple JToolBar
:
import javax.swing.*; import java.awt.*; public class Example extends JFrame { public Example() { JToolBar toolBar = new JToolBar(); JButton button1 = new JButton("Button 1"); toolBar.add(button1); JButton button2 = new JButton("Button 2"); toolBar.add(button2); JComboBox<String> comboBox = new JComboBox<>(new String[]{"Option 1", "Option 2", "Option 3"}); toolBar.add(comboBox); add(toolBar, BorderLayout.NORTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public static void main(String[] args) { Example ex = new Example(); ex.setVisible(true); } }
In this example, we create a JToolBar
and add a couple of JButton
s and a JComboBox
to it. We then add the JToolBar
to the frame's content pane using a BorderLayout
layout manager, positioning it at the top. Finally, we set the size of the frame, set the default close operation to exit the application when the frame is closed, and position the frame in the center of the screen. When you run this code, you should see a window with a toolbar containing the buttons and combo box.