Adding Menus to two panes of a splitPane - java

I want to have a layout like this:
The grey areas will be two different menus.
I managed to make the split panes, but I can't seem to add the menus, here's my code:
package View;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.plaf.basic.BasicSplitPaneUI;
public class TaskView extends JFrame{
JMenuBar menuBar;
JMenu addTask, refresh;
private int screenHeight,screenWidth;
public TaskView() {
setTitle("TASKS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Toolkit myScreen = Toolkit.getDefaultToolkit();
Dimension screenSize = myScreen.getScreenSize();
screenHeight = screenSize.height;
screenWidth = screenSize.width;
setSize(screenWidth/2,screenHeight/2);
System.out.println(screenWidth/2);
setLocation(screenWidth/4,screenHeight/4);
placeComponents(this.getContentPane());
}
private void placeComponents(Container contentPane) {
JPanel jsp1 = new JPanel();
JPanel jsp2 = new JPanel();
JLabel j1 = new JLabel("Area 1");
JLabel j2 = new JLabel("Area 2");
menuBar = new JMenuBar();
addTask = new JMenu("Add Task");
refresh = new JMenu("Refresh");
menuBar.add(addTask);
menuBar.add(refresh);
jsp1.add(menuBar);
jsp1.add(j1);
jsp2.add(j2);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
true, jsp1, jsp2);
splitPane.setUI(new BasicSplitPaneUI());
splitPane.setOneTouchExpandable(false);
contentPane.add(splitPane);
splitPane.setEnabled(false);
setVisible(true);
splitPane.setDividerLocation(300);
}
}
Every time I try to add a menu it makes a mess in the left panel and it dosn't look at all like a menu, how can i add the menus without it looking like shit?

try this
public class TaskView extends JFrame {
public TaskView() throws HeadlessException {
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setPreferredSize(new Dimension(600, 400));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createPanel(), createPanel());
splitPane.setResizeWeight(0.5);
add(splitPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
private JPanel createPanel() {
JPanel panel = new JPanel(new BorderLayout());
JMenuItem menuItem1 = new JMenuItem("MenuItem 1");
JMenuItem menuItem2 = new JMenuItem("MenuItem 2");
JMenuItem menuItem3 = new JMenuItem("MenuItem 3");
JMenu menu = new JMenu("Main");
menu.add(menuItem1);
menu.addSeparator();
menu.add(menuItem2);
menu.add(menuItem3);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
menuBar.add(new JMenu("View"));
panel.add(menuBar, BorderLayout.PAGE_START);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TaskView().setVisible(true));
}
}

Related

JFrame Components not being displayed

I am attempting to add components to a JFrame but the only thing being displayed is my ImageIcon when I run GameMenu.java. I have instantiated setVisible(); specifically after I have set my frame or added components to the panels or menubars. So I'm unsure as to why no components are showing up. I think it may have something to do with my formatting or main method.
Here are my two classes:
GameMenu.java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GameMenu{
public static void main(String[] args) {
FrameCaller obj = new FrameCaller();
}
}
class FrameCaller extends JFrame {
public FrameCaller(){
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel(new ImageIcon("logo.png")));
pack();
setLocationRelativeTo(null);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("Game List");
JMenu m2 = new JMenu("Help");
JMenu m3 = new JMenu("Stats");
mb.add(m1);
mb.add(m2);
mb.add(m3);
JMenuItem showRulesButton = new JMenuItem("View game rules");
m2.add(showRulesButton);
JMenuItem m77 = new JMenuItem("View past game stats");
m3.add(m77);
mb.setVisible(true);
JPanel panel = new JPanel();
JButton newGameButton = new JButton("New Game");
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new inGameFrame();
dispose();
}
});
panel.add(newGameButton);
panel.setVisible(true);
setVisible(true);
}
}
EightOff.java:
import javax.swing.*;
public class EightOff {
public static void main(String[] args)
{
inGameFrame obj = new inGameFrame();
}
}
class inGameFrame extends JFrame
{
public inGameFrame() {
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Any tips would be wonderful. Thanks.

Java GUI - Menu Bar and Split Pane

I know how to code Java but I'm having a lot of trouble with this. I've made a menubar but I want to put a split pane underneath it. The menubar is fine but the split pane is giving me a lot of errors and I don't know how to fix it.
Any help would be much appreciated.
package getcodinggui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class GetCodingGUI {
JTextArea output;
JScrollPane scrollPane;
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu;
menuBar = new JMenuBar();
menu = new JMenu("Home");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"File Menu Items");
menuBar.add(menu);
menu = new JMenu("About");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("Contact Us");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("FAQ");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("Log In");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu Items");
menuBar.add(menu);
return menuBar;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static class MyJFrameWin extends JFrame{
JSplitPane jSplitPane, jSplitPane2;
JPanel jPanel1, jPanel2a, jPanel2b;
jPanel1 = new JPanel();
jPanel2a = new JPanel();
jPanel2b = new JPanel();
jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
jPanel2a, jPanel2b);
jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jPanel1, jSplitPane2);
jSplitPane.setOneTouchExpandable(true);
jSplitPane.setDividerLocation(150);
getContentPane().add(jSplitPane);
}
}
//Create and set up the content pane.
GetCodingGUI demo = new GetCodingGUI();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
//Display the window.
frame.setSize(1280, 720);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(runJSplitPaneLater);
}
javax.swing.SwingUtilities.invokeLater(new Runnable, runJSplitPaneLater()
{
static Runnable runJSplitPaneLater = new Runnable(){
#Override
public void run() {
MyJFrameWin myJFrameWin = new MyJFrameWin();
myJFrameWin.setVisible(true);
createAndShowGUI();
}
});
}
}
I had to clean up 20 compile errors.
Here's the GUI I created.
Here are the changes I made.
I rearranged all of your code. Code is much easier to understand when it reads from top to bottom.
Since the SwingUtilities invokeLater method requires a Runnable, I made your GUI view class implement Runnable.
I put your content pane in one of the JSplitPanes. I just guessed which pane.
I put the outer JSplitPane into the JFrame.
I fixed your menu alt keys.
I returned a JPanel from your createContentPane method.
I formatted your code.
I reduced the size of your JFrame so it would fit on my screen.
Here's the code:
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class GetCodingGUI implements Runnable {
private JTextArea output;
private JScrollPane scrollPane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new GetCodingGUI());
}
#Override
public void run() {
// Create and set up the window.
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane jSplitPane, jSplitPane2;
JPanel jPanel1, jPanel2a, jPanel2b;
jPanel1 = new JPanel();
jPanel2a = new JPanel();
jPanel2b = createContentPane();
jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jPanel2a,
jPanel2b);
jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel1,
jSplitPane2);
jSplitPane.setOneTouchExpandable(true);
jSplitPane.setDividerLocation(150);
frame.add(jSplitPane);
frame.setJMenuBar(createMenuBar());
// Display the window.
frame.setSize(800, 600);
frame.setVisible(true);
}
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu;
menuBar = new JMenuBar();
menu = new JMenu("Home");
menu.setMnemonic(KeyEvent.VK_H);
menu.getAccessibleContext().setAccessibleDescription("File Menu Items");
menuBar.add(menu);
menu = new JMenu("About");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("Contact Us");
menu.setMnemonic(KeyEvent.VK_C);
menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("FAQ");
menu.setMnemonic(KeyEvent.VK_F);
menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
menuBar.add(menu);
menu = new JMenu("Log In");
menu.setMnemonic(KeyEvent.VK_L);
menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
menuBar.add(menu);
return menuBar;
}
public JPanel createContentPane() {
// Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
// Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
// Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
}

Implement copy/paste in JTextArea?

I have a very simple code that creates a frame object from the class MyJFrame accepts the first string which is used as a title. Place the second string is the text to be displayed in a JScrollPane. You can see the code below. What I need is to use copy and paste of text highlighted. I need help implementing it. So that if copy selected from a menubar it copies the highlighted portion and if paste is pastes it.
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;
public class DisplayText
{
private static JTextArea text;
public DisplayText(String title, String info)
{
MyJFrame f = new MyJFrame(title);
Container c = f.getContentPane();
//default text
text = new JTextArea(info);
//Scrollpane
JScrollPane sp = new JScrollPane(text);
c.add( sp );
f.setBounds(100,200, 500, 400 );
f.setVisible(true);
}
Use the Actions that are available in the DefaultEditorKit including DefaultEditorKit.CopyAction, DefaultEditorKit.CutAction, and DefaultEditorKit.PasteAction.
For example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.text.*;
public class TestActions {
private String[] texts = {
"Hello", "Goodbye", "What the f***?", "Heck if I know", "Peace out man!"
};
private JTextArea textArea = new JTextArea(10, 30);
private Action[] textActions = { new DefaultEditorKit.CutAction(),
new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
private JPanel mainPanel = new JPanel();
private JMenuBar menubar = new JMenuBar();
private JPopupMenu popup = new JPopupMenu();
private PopupListener popupListener = new PopupListener();
public TestActions() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
JMenu menu = new JMenu("Edit");
for (Action textAction : textActions) {
btnPanel.add(new JButton(textAction));
menu.add(new JMenuItem(textAction));
popup.add(new JMenuItem(textAction));
}
menubar.add(menu);
JPanel textFieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (String text: texts) {
JTextField textField = new JTextField(text, 15);
textField.addMouseListener(popupListener);
textFieldPanel.add(textField);
textField.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
((JTextComponent)e.getSource()).selectAll();
}
});
}
textArea.addMouseListener(popupListener);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel textFieldPanelWrapper = new JPanel(new BorderLayout());
textFieldPanelWrapper.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout(5, 5));
mainPanel.add(btnPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(textFieldPanelWrapper, BorderLayout.EAST);
}
public JComponent getMainPanel() {
return mainPanel;
}
private JMenuBar getMenuBar() {
return menubar;
}
private class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
private static void createAndShowGui() {
TestActions testActions = new TestActions();
JFrame frame = new JFrame("Test Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testActions.getMainPanel());
frame.setJMenuBar(testActions.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
code borrowed from my answer here.
Edit
You ask in comment:
I appreciate the answer. However, could you make it a bit simpler to understand, I am fairly new to Java.
Sure, here is a simple JMenuBar that holds an edit JMenu that holds JMenuItems for copy, cut, and paste with just that code borrowed from my example. Note that as an aside, you should not setBounds on anything, you should instead set the rows and columns of your JTextArea, and that you should not use a static JTextArea, and in fact no Swing components should ever be static.
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultEditorKit;
public class DisplayText {
private JTextArea text;
private Action[] textActions = { new DefaultEditorKit.CutAction(),
new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
public DisplayText(String title, String info) {
JMenu menu = new JMenu("Edit");
for (Action textAction : textActions) {
menu.add(new JMenuItem(textAction));
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame f = new JFrame(title);
f.setJMenuBar(menuBar);
Container c = f.getContentPane();
text = new JTextArea(info, 20, 50);
JScrollPane sp = new JScrollPane(text);
c.add(sp);
// f.setBounds(100,200, 500, 400 );
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
new DisplayText("Title", "This is info text");
}
}

JScrollPane with JPanel nothing see

I wnat add to JFrame JSrollPane. ScrollPane contains a JPanels. But I have problem when I add first JPanel to ScrollPane i see nothing when I add JPanel to JFrame I see JPanels. So where I make mistake? Here code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class AddingJPanels {
public static void main(String... args) {
JFrame jF = new JFrame();
PanelMain pM = new PanelMain();
Panel p = new Panel("sas");
JPanel jp = makeJPanel(10);
p.setPreferredSize(new Dimension(600,600));
JScrollPane scroll = new JScrollPane();
scroll.add(jp);
JScrollBar verticalPane = scroll.getVerticalScrollBar();
verticalPane.setValue(verticalPane.getMinimum());
verticalPane.setValue(20);
//scroll.setPreferredSize(new Dimension(570, 300));
scroll.setPreferredSize(new Dimension(400,500));
pM.add(scroll);
//JTabbedPane tB = new JTabbedPane();
//tB.addTab(":]", null, pM, "Tab 3");
jF.add(jp);
jF.setSize(new Dimension(500,500));
jF.setVisible(true);
}
static JPanel makeJPanel(int i){
JPanel jPl = new JPanel();
jPl.setLayout(new GridLayout(i,0));
JLabel lebel;
for(int j=0;j<i;++j){
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
JButton b = new JButton("asa");
p.add(b);
p.setBorder(BorderFactory.createLineBorder(Color.black));
p.setPreferredSize(new Dimension(400,400));
lebel = new JLabel("Napis: "+j);
p.add(lebel);
JTextField jTF = new JTextField("Nic",20);
p.add(jTF);
jPl.add(p);
}
return jPl;
}
}
class Frame extends JFrame {
public Frame() {
super("Frame");
this.setPreferredSize(new Dimension(200, 200));
}
public void see() {
this.setVisible(true);
}
}
class PanelMain extends JPanel {
JButton b = new JButton("press me");
public PanelMain() {
this.add(b);
b.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Pressed");
}
});
}
}
class Panel extends JPanel {
JLabel l;
public Panel(String s) {
l = new JLabel(s);
this.add(l);
}
}
When I make jF.add(scroll) is no effect.
add() doesn't work on a JScrollPane. You need to use setViewport() or else pass a component in the contstructor.
JScrollPane scroll = new JScrollPane(jp);
or
JScrollPane scroll = new JScrollPane();
scroll.setViewport(jp);
JScrollPane scroll = new JScrollPane(jp);
use:
scroll.setViewportView(jp);

How to add two label with preferred size to a panel by using BorderLayout?

I am trying to add to label to north and south of a panel and add the panel to the centre of my frame. If I don't specify the size of the labels by myself the program is perfect but when I give them specific dimension:
label.setPreferredSize(di);
label2.setPreferredSize(di);
it gets messy! However the size of panel and frame are larger and (100,100) Any idea?
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class BorderLayoutDemo2 {
public static void main(String args[])
{
Frame frame = new Frame();
}
}
class Frame extends JFrame
{
private static final long serialVersionUID = 1L;
public Frame(){
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension di = new Dimension(50,50);
Dimension dim = new Dimension(200,200);
JPanel panel = new JPanel();
panel.setPreferredSize(dim);
JLabel label = new JLabel("Here is my label");
JLabel label2 = new JLabel("Here is my label2");
JMenuBar menu = new JMenuBar();
JMenu setting = new JMenu("Setting");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem add = new JMenuItem("Add");
setting.add(add);
setting.add(exit);
menu.add(setting);
label.setPreferredSize(di);
label2.setPreferredSize(di);
panel.add(label,BorderLayout.NORTH);
panel.add(label2,BorderLayout.SOUTH);
add(menu,BorderLayout.NORTH);
add(panel,BorderLayout.CENTER);
pack();
setVisible(true);
}
}
You are assuming the layout of the panel is BorderLayout when you add components, ie: panel.add(label,BorderLayout.NORTH);. But you are not setting the layout and JPanel uses FlowLayout which is its default. You can fix it like this:
JPanel panel = new JPanel(new BorderLayout());

Categories