Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,865 people online right now. Registration is fast and FREE... Join Now!




swings

 
Reply to this topicStart new topic

swings, creating menus and scrollbar using swings

mrithula
16 Sep, 2007 - 09:36 PM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2007
Posts: 11


My Contributions
we have problem creating menus as well scrollbars in one program using swings.the program compiles without an error and appletviewer window opens but it says that applet is not initialized and so we are not able to see the output.please help me with this





CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*<applet code="Scrolllist" width=200 height=200>
</applet>
*/

class example extends JMenuBar{
String[] fileItems=new String[]{"New","Open","Save","Exit"};
String[] editItems=new String[]{"Undo","Cut","Copy","Paste"};
char[] fileShortcuts={'N','O','S','X'};
char[] editShortcuts={'Z','X','C','V'};

public example(){
JMenu fileMenu=new JMenu("File");
JMenu editMenu=new JMenu("Edit");
JMenu otherMenu=new JMenu("Other");
JMenu subMenu=new JMenu("SubMenu");
JMenu subMenu2=new JMenu("SubMenu2");

ActionListener printListener=new ActionListener(){
public void actionPerformed(ActionEvent event){
System.out.println("MenuItem["+event.getActionCommand()+"] was pressed");
}
};

for(int i=0;i<fileItems.length;i++){
JMenuItem item=new JMenuItem(fileItems[i],fileShortcuts[i]);
item.addActionListener(printListener);
fileMenu.add(item);
}

for(int i=0;i<editItems.length;i++){
JMenuItem item=new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(),false));
item.addActionListener(printListener);
editMenu.add(item);
}

editMenu.insertSeparator(1);
JMenuItem item;
subMenu2.add(item=new JMenuItem("Extra 2"));
item.addActionListener(printListener);
subMenu.add(item=new JMenuItem("Extra 1"));
item.addActionListener(printListener);
subMenu.add(subMenu2);
otherMenu.add(subMenu);
otherMenu.add(item=new JCheckBoxMenuItem("Check Me"));
item.addActionListener(printListener);
otherMenu.addSeparator();
ButtonGroup buttonGroup=new ButtonGroup();
otherMenu.add(item=new JRadioButtonMenuItem("Radio 1"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item=new JRadioButtonMenuItem("Radio 2"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator();
otherMenu.add(item=new JMenuItem("Plotted Plant",new ImageIcon("image.gif")));
item.addActionListener(printListener);
add(fileMenu);
add(editMenu);
add(otherMenu);
}




public static void main(String args[]){
JFrame frame=new JFrame("Simple Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new example());
frame.pack();
frame.setVisible(true);

Scrolllist sl=new Scrolllist();
sl.setVisible(true);
}
}

class Scrolllist extends JFrame
{
JScrollPane scrollpane;
public Scrolllist(){
super("JScrollPane Demo");
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String categories[]={"fgaqxdfsgdasdhjdbshbdhbd"};
JList list=new JList(categories);
scrollpane=new JScrollPane(list);
getContentPane().add(scrollpane,BorderLayout.CENTER);
}
}


*1lacca: code tags added

This post has been edited by 1lacca: 16 Sep, 2007 - 10:02 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Swings
16 Sep, 2007 - 09:54 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You need to put your code in code blocks please, it even says that in the textbox you typed your post into smile.gif

To add code blocks simply select the code portion of your post, then hit the button with the # on it.

Thank you smile.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Swings
16 Sep, 2007 - 10:05 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
AppletViewer won't show your application, because it is not an Applet. Since it looks a regular Java application, simply run it from the command line, or from an IDE.
If you want to create an applet, then subclass JApplet instead of a frame.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month