How do I divide windows in using Swing? - java

I am writing a program in Java and I'm learning Swing as I go, but I seem to be at a roadblock. I have attached a sketch to explain what I'm trying to do, and hopefully you can help me understand what to do.
+===============================================================================+
|File Edit View Help |
+================================================================================
| | |
| | |
|Content 1 | Content 2 |
| | |
| | |
+===============================================================================+
I am using JFrames and JPanels to construct this program, but I cannot understand how to make the two frames for the content. Also, I want the left column to always be 150px wide, and the entire height of the frame, while the right should always fill the remaining width of the window.
package mycookbook;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class MyCookBook extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new MyCookBook();
}
public MyCookBook() {
super("My Cook Book vers. 0.0.0.1");
setSize(1920, 1030);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setMenu();
setWindows();
}
public void setMenu() {
//Setting up the menubar.
JMenuBar menubar = new JMenuBar();
//Adding the menu bar.
setJMenuBar(menubar);
//Setting up the file menu.
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenu nwFile = new JMenu("New >>");
JMenuItem cbNwFile = new JMenuItem("Cookbook");
JMenuItem chNwFile = new JMenuItem("Chapter");
JMenuItem rcNwFile = new JMenuItem("Recipe");
JMenu opFile = new JMenu("Open >>");
JMenuItem cbOpFile = new JMenuItem("Cookbook");
JMenuItem chOpFile = new JMenuItem("Chapter");
JMenuItem rcOpFile = new JMenuItem("Recipe");
JMenuItem svFile = new JMenuItem("Save");
JMenuItem ipFile = new JMenuItem("Import");
JMenuItem epFile = new JMenuItem("Export");
JMenuItem pnFile = new JMenuItem("Print");
JMenuItem upFile = new JMenuItem("Update");
JMenuItem prFile = new JMenuItem("Properties");
JMenuItem exFile = new JMenuItem("Exit");
//Adding all the file menu and its contents to the menubar.
menubar.add(fileMenu);
fileMenu.add(nwFile);
nwFile.add(cbNwFile);
nwFile.add(chNwFile);
nwFile.add(rcNwFile);
fileMenu.add(opFile);
opFile.add(cbOpFile);
opFile.add(chOpFile);
opFile.add(rcOpFile);
fileMenu.addSeparator();
fileMenu.add(svFile);
fileMenu.add(ipFile);
fileMenu.add(epFile);
fileMenu.add(pnFile);
fileMenu.addSeparator();
fileMenu.add(upFile);
fileMenu.add(prFile);
fileMenu.addSeparator();
fileMenu.add(exFile);
exFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Setting up the edit menu.
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem cbEdit = new JMenuItem("Cookbook");
JMenuItem chEdit = new JMenuItem("Chapter");
JMenuItem rcEdit = new JMenuItem("Recipe");
menubar.add(editMenu);
editMenu.add(cbEdit);
editMenu.add(chEdit);
editMenu.add(rcEdit);
//Setting up the view menu.
JMenu viewMenu = new JMenu("View");
viewMenu.setMnemonic(KeyEvent.VK_V);
JMenu mdView = new JMenu("Mode >>");
JMenuItem pnView = new JMenuItem("Panel View");
JMenuItem pgView = new JMenuItem("Page View");
JMenuItem lsView = new JMenuItem("List View");
JMenu rsView = new JMenu("Resolution >>");
JMenuItem smRes = new JMenuItem("1024x718");
JMenuItem mdRes = new JMenuItem("1440x910");
JMenuItem lgRes = new JMenuItem("1920x1030");
menubar.add(viewMenu);
viewMenu.add(mdView);
viewMenu.add(rsView);
mdView.add(pnView);
mdView.add(pgView);
mdView.add(lsView);
rsView.add(smRes);
rsView.add(mdRes);
rsView.add(lgRes);
smRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1024,718);
setLocationRelativeTo(null);
}
});
mdRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1440,910);
setLocationRelativeTo(null);
}
});
lgRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1920,1030);
setLocationRelativeTo(null);
}
});
exFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Setting up the help menu.
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem hpHelp = new JMenuItem("My Cookbook Help");
JMenuItem abHelp = new JMenuItem("About My Cookbook");
menubar.add(helpMenu);
helpMenu.add(hpHelp);
helpMenu.add(abHelp);
revalidate();
}
public void setWindows() {
JScrollPane bookPane = new JScrollPane();
JScrollPane recPane = new JScrollPane();
JSplitPane content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,bookPane, recPane);
Dimension bpDim = new Dimension(150, 400);
Dimension rpDim = new Dimension(650, 400);
bookPane.setMinimumSize(bpDim);
recPane.setMinimumSize(rpDim);
add(content);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}

Read the section from the Swing tutorial on How to Use Border Layout.
One component goes to BorderLayout.LINE_START the other to BorderLayout.CENTER.
Also, I want the left column to always be 150px wide
The width is determined by the preferred size of the component added to that area of the BorderLayout.

You can simply solve this problem by using a different layout for your GUI. I recommend you check out the GridBagLayout as this layout is highly customizible.
We can modify our GridBagLayout by supplying it with GridBagConstraints once you've learned how to modify your GridBagLayout using GridBagConstraints your problem will be extremely simple to solve.
You asked how to divide windows, simple just make three JPanels and put two of thoseJPanels in the main JPanel that will be displayed at the highest level.

Related

How to add ScrollBar on Textarea or JFrame in Java Swing?

Hi everyone ,i`m beginner in programmation and i wanted to make a text editor interface by using java Eclipse and i get problem each time i add scrollbar , the text area disappear ... How can i fix my code to continue on it ?... i saw many tutorials but nothing work>
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
public class Fenetre extends JFrame {
JMenuBar Menubar = new JMenuBar();
JMenu MenuFichier = new JMenu("Fichier");
JMenuItem MenuFichier1 = new JMenuItem("Nouveau");
JMenuItem MenuFichier2 = new JMenuItem("Ouvrir");
JMenuItem MenuFichier3 = new JMenuItem("Enregister");
JMenuItem MenuFichier4 = new JMenuItem("Enregister sous");
JMenuItem MenuFichier5 = new JMenuItem("Quitter");
JMenu MenuEdition = new JMenu("Edition");
JMenuItem MenuEdition1 = new JMenuItem("Couper");
JMenuItem MenuEdition2 = new JMenuItem("Copier");
JMenuItem MenuEdition3 = new JMenuItem("Coller");
JMenu MenuAide = new JMenu("Aide");
JMenuItem MenuAide1 = new JMenuItem("A propos");
JPanel p=new JPanel();
JScrollPane sp = new JScrollPane ();
JTextArea jt = new JTextArea (90,90);
public Fenetre() {
add(p);
setTitle("Text Editor");
setBounds(100,100, 500, 500);
//setExtendedState(JFrame.MAXIMIZED_BOTH);
sp.add(jt);
jt.setText(null);
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
jt.setVisible(true);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.add(sp);
sp.setVisible(true);
Menubar.add(MenuFichier);
Menubar.add(MenuEdition);
Menubar.add(MenuAide);
MenuFichier.add(MenuFichier1);
MenuFichier.add(MenuFichier2);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier3);
MenuFichier.add(MenuFichier4);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier5);
MenuEdition.add(MenuEdition1);
MenuEdition.add(MenuEdition2);
MenuAide.add(MenuAide1);
Menubar.setBackground(Color.PINK);
getContentPane().add(sp);
setJMenuBar(Menubar);
setVisible(true);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
and this is the main of the code
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class CreationEditor {
public static void main(String[] args) {
JFrame f = new Fenetre();
}
}

How to add Action Listener from another class java?

I want to develop a very basic application, well here is the code, I have 4 classes one is the main class, the second one is JMenuBar, the third one is Panel and the fourth one is for Action Listener. at the moment my problem is here how to add instance of Action Listener for class of JMenuBar, till i could do some action.
public class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
}
public class My_Menu extends JMenuBar {
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
JMenuItem item1 = new JMenuItem("file");
JMenuItem item2 = new JMenuItem("New");
JMenuItem item3 = new JMenuItem("Setting");
JMenuItem item4 = new JMenuItem("Color");
JMenuItem item5 = new JMenuItem("Print");
JMenuItem item6 = new JMenuItem("Exit");
file.add(item1);
file.add(item2);
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
file.addSeparator();
this.add(file);
this.add(Edit);
this.add(help);
item1.addActionListener(my_action);
}
public void actionPerformed(ActionEvent actionEvent){
System.exit(0);
}
}
This is my test program for you.
Check the console output when executing the menu program.
package just.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class My_Menu extends JMenuBar implements ActionListener {
private static final long serialVersionUID = 1L;
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
this.add(file);
this.add(Edit);
this.add(help);
JMenuItem item1 = new JMenuItem("file");
item1.setActionCommand("file");
JMenuItem item2 = new JMenuItem("New");
item2.setActionCommand("New");
JMenuItem item3 = new JMenuItem("Setting");
item3.setActionCommand("Setting");
JMenuItem item4 = new JMenuItem("Color");
item4.setActionCommand("Color");
JMenuItem item5 = new JMenuItem("Print");
item5.setActionCommand("Print");
JMenuItem item6 = new JMenuItem("Exit");
item6.setActionCommand("Exit");
JMenuItem item7 = new JMenuItem("edit1");
item7.setActionCommand("edit1");
JMenuItem item8 = new JMenuItem("edit2");
item8.setActionCommand("edit2");
JMenuItem item9 = new JMenuItem("about..");
item9.setActionCommand("about");
file.add(item1);
file.add(item2);
file.addSeparator();
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
Edit.add(item7);
Edit.add(item8);
help.add(item9);
item1.addActionListener(my_action);
item2.addActionListener(my_action);
item3.addActionListener(my_action);
item4.addActionListener(my_action);
item5.addActionListener(my_action);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
file.addActionListener(this);
Edit.addActionListener(this);
help.addActionListener(my_action);
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----1----\n" + command);
//System.exit(0);
}
class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----2----\n" + command);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
f.add(new My_Menu(), BorderLayout.NORTH);
f.add(jp, BorderLayout.CENTER);
jp.setSize(500,400);
f.setSize(800, 600);
f.setVisible(true);
//f.pack();
}
});
}
}
You can just add a addActionListener to the component in which you have to send a message.
item1.addActionListener(my_action);
or
item7.addActionListener(this);
Don't forget to implement a ActionListener on your My_Menu class.
What you have is right.
It should call action performed when you click on file menu item. Just put a print ln in that method to see it being called.

Java menu item enabling within event listener

Hello im trying to enable my JMenuItem from within an event listener but it seems to be out of scope. im new to java so how would i properly go about this. the said event listener will change to a new view and enable the disabled menu items.
//Create and add MenuItems
JMenuItem fileItem0 = new JMenuItem("Load");
collMenu.add(fileItem0);
JMenuItem fileItem1 = new JMenuItem("Add");
fileItem1.setEnabled(false);
collMenu.add(fileItem1);
JMenuItem fileItem2 = new JMenuItem("Delete");
fileItem2.setEnabled(false);
collMenu.add(fileItem2);
//Add Menu bar to frame
menubar.add(collMenu);
//Menu Item Functions
fileItem0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(null,"You selected: Load.");
//Enable fileitem1 & 2 here ??
}
});
I hope this small example will help you clear your doubts...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MenuExample extends JFrame {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenuItem item1 = new JMenuItem("New");
JMenuItem item2 = new JMenuItem("Open");
public MenuExample() {
setJMenuBar(menuBar);
setVisible(true);
setSize(400, 200);
menuBar.add(menu);
menuBar.add(menu1);
menu.add(item1);
menu.add(item2);
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
}
});
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
}
});
}
public static void main(String[] args) {
MenuExample ex = new MenuExample();
}
}
Don't declare these menu items (fileitem1 and fileitem2) in the same method. Just Declare your fileitem1 and fileitem2 outside of your method.
This solves your problem...
Delcare the JMenuItems you are trying to access as final to be accessed from within an inner class (addActionListener(new ActionListener() {...}).
Inner class needs JMenuItems accessed within ActionListener
to be final. To avoid strange side-effects with closures in java
variables referenced by an anonymous delegates.
Here is an example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenuItem itemLoad = new JMenuItem("Load");
//delcare them as final to be accessed from within an inner class
final JMenuItem itemAdd = new JMenuItem("Add");
final JMenuItem itemDel = new JMenuItem("Del");
itemAdd.setEnabled(false);
itemDel.setEnabled(false);
itemLoad.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
itemAdd.setEnabled(true);
itemDel.setEnabled(true);
}
});
menu1.add(itemLoad);
menu1.add(itemAdd);
menu1.add(itemDel);
menuBar.add(menu1);
frame.setJMenuBar(menuBar);
}
}

JToolBar not showing

I had a simple drawing application. I need to add a menu and a toolbar on the left side.
So now, instead of using a simple JFrame, I'm creating a simple class that extends JFrame. I was able to add the menu following some examples online, but can't figure out how to add a JToolBar. I've tried a few different ways, but nothing works. I don't get an error, everything complies just fine, but I don't see any JToolBar.
Here's the code for my JFrame, I hope you can help.
class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
public Menu(String title) {
openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change
setJMenuBar(menubar);
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);
// JPanel panel = new JPanel();
// panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton newb = new JButton("new");
toolbar.add(newb);
// toolbar.setOpaque(true);
toolbar.setLocation(100, 100);
toolbar.setVisible(true);
// toolbar.setMinimumSize(new Dimension(100, 100));
// toolbar.setAlignmentX(0);
// panel.add(toolbar);
add(toolbar, BorderLayout.NORTH);
getContentPane().add(toolbar, BorderLayout.NORTH);
// getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setTitle(title);
pack();
setLocationRelativeTo(null); // Center window.
}
// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}
}
its work fine, and you don't need to setVisible tool bar because its showing by default, also don't add the tool bar two time in the same place (NORTH)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuDemo {
public static void main(String... args) {
EventQueue.invokeLater(
new Runnable() {
#Override
public void run() {
JFrame menu = new Menu("Testing");
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setVisible(true);
}
}
);
}
}
class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
public Menu(String title) {
openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change
setJMenuBar(menubar);
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);
JButton newb = new JButton("new");
toolbar.add(newb);
add(toolbar, BorderLayout.NORTH);
setTitle(title);
setLocationRelativeTo(null);
pack();
}
// OpenAction
private class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}
}
My problem was that I was the way I was instantiating my JFrame. I was using a helper function like this one:
public static JFrame openInJFrame(Container content, int width, int height,
String title, Color bgColor) {
// ...
frame.setContentPane(content);
frame.setVisible(true);
return (frame);
}
So my JToolBar was getting replaced by the Container object...
Thanks guys! Your answers helped me figure out my problem.

Working with JApplet with Menus

I'm having problems with my code. The sub-menu for the (Music) menu should be a radio button type.
Here's my first code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenuItem music = new JMenuItem("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
music.group.add(play);
//group.add(loop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
When I try to run it, the Music menu doesn't appear. It changes to the Play (radio button). Does the button group help? When i tried to use the button group nothing happens.
Like this?
/* <applet code='AMBAT_FLAB1' width=220 height=100></applet> */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenu music = new JMenu("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
group.add(play);
group.add(loop);
group.add(stop);
music.add(play);
music.add(loop);
music.add(stop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
The mistakes in the code were basically:
If Music had children, it had to be a JMenu, not a JMenuItem
A ButtonGroup is a logical group (e.g. to make radio buttons out of a group of buttons), it is not a container. So besides adding the buttons to the group, it is necessary to add them to the Music JMenu.
You have a syntax error in your source code. Try commenting out the line that is failing and recompiling. That should give you a bit more information in your interface (GUI).

Categories

Resources