I have a JTextArea
, and I want to add a JMenuBar
to it, but it doesn't seem to work.
ta = new JTextArea();
ta.setBackground(Color.RED);
// ta.setLayout(null); I tried with a null layout and
non-null
pane = new JScrollPane(ta);
pane.setBounds(Main.WIDTH - (Main.WIDTH - 20), Main.HEIGHT - (Main.HEIGHT - 20), Main.WIDTH - 60, Main.HEIGHT - 500);
bar = new JMenuBar();
bar.setBounds(0, 0, ta.getWidth(), 20); // This won't be there if
// there is a non-null layout.
ta.add(bar); // I also tried pane.add(bar); that didn't work either.
Is there any way to add JMenuBar
to JTextArea
?
Done
e.g.,
JTextArea ta = new JTextArea(40, 20); // give columns and rows
JScrollPane scrollPane = new JScrollPane(ta);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
// add menu's to the menu bar here
borderLayoutPanel.add(menuBar, BorderLayout.PAGE_START);
Side notes:
ta.getWidth()
is likely returning a width value of 0, since it appears to be called before the JTextArea has been rendered.