4

I am new to Java Swing.I want to design one JToolBar. The JToolBar should be placed in center of JPanel. Is it possible?

javax.swing.JPanel pane = new javax.swing.JPanel(); 
BorderLayout border = new BorderLayout(); 
pane.setLayout(border); 
JToolBar toolBar = new JToolBar(); 
pane.add(toolBar,BorderLayout.CENTER); 
javax.swing.JButton button1 = new javax.swing.JButton("Click Me"); 
toolBar.add(button1);
Amarnath
  • 8,736
  • 10
  • 54
  • 81
boopathy
  • 427
  • 2
  • 9
  • 20
  • If you have tried using _BorderLayout_ then post that code else it will be very hard to guess. – Amarnath Jan 23 '13 at 09:11
  • 1) See [Centering a JLabel on a JPanel](http://stackoverflow.com/a/7181197/418556) for tips. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 23 '13 at 09:29

2 Answers2

4

Read about How to use ToolBars.

The following code is taken straight from the doc.

public ToolBarDemo() {
    super(new BorderLayout());
    ...
    JToolBar toolBar = new JToolBar("Still draggable");
    addButtons(toolBar);
    ...
    setPreferredSize(new Dimension(450, 130));
    add(toolBar, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
}

See the usage of BorderLayout here. And do the necessary changes in your code.

UPDATE:

I have tried using your code which shows output like this. I have used addSeparator method with dimension. This is just a try to solve the problem. I am not sure whether this approach is the correct way.

enter image description here

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new BorderLayout()); 
    JToolBar toolBar = new JToolBar(); 
    panel.add(toolBar,BorderLayout.PAGE_START);

    toolBar.addSeparator(new Dimension(150, 0));

    JButton button1 = new JButton("Click Me"); 
    toolBar.add(button1);
    frame.setLayout(new BorderLayout());
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(new Dimension(400, 100));
    frame.setVisible(true);
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • I too got same result while I try my code.Is it possible to display that 'click me' button in center position of the toolbar? – boopathy Jan 23 '13 at 10:23
  • @boopathy Look at my update. I used addSeparator to do this. I am not sure whether this is a right approach or not. This will solve your purpose. But make sure you know the right way of doing this and please let me know. – Amarnath Jan 23 '13 at 10:40
  • @boopathy In _JPanel_ we have added _JToolBar_. But the _Button_ is inside the toolbar. So we have to align the button which is in the JToolBar. But I have read the doc it does not have any layout inside JToolBar. So I have added an _addSeparator_ with width of 150 with respect to my frame dimension. Hope this helps. – Amarnath Jan 23 '13 at 14:02
3

If you JPanel has a BorderLayout and you place the JToolBar in BorderLayout.CENTER and you have components in NORTH, SOUTH, EAST and WEST then I don't see a reason why it wouldn't work.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • I too tried that way, but that didn't worked ,that's why posted this question. – boopathy Jan 23 '13 at 08:39
  • Hard to guess what "didn't work" means in your case. Post some code, screenshots, whatever you have. – Dan D. Jan 23 '13 at 08:42
  • `javax.swing.JPanel pane = new javax.swing.JPanel(); BorderLayout border = new BorderLayout(); pane.setLayout(border); JToolBar toolBar = new JToolBar(); pane.add(toolBar,BorderLayout.CENTER); javax.swing.JButton button1 = new javax.swing.JButton("Click Me"); toolBar.add(button1);` – boopathy Jan 23 '13 at 09:14
  • As I said, add stuff to the other parts of the UI, not just in the center. – Dan D. Jan 23 '13 at 09:16
  • I tried all possibilities(BorderLayout.CENTER instead of BorderLayout.NORTH or EAST or WEST or SOUTH).All are produced same output only. – boopathy Jan 23 '13 at 10:47
  • You must add the toolbar to CENTER and add other panels, even empty to the other parts. – Dan D. Jan 23 '13 at 10:48