473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Swing Cut info

1 New Member
I am trying to get cut and paste to work from menu. Below is code I have can someone point me where I am going wrong I have the same problem with File selection to open and save using JFileChooser.


import java.awt.*;
import java.awt.event. *;
import java.awt.datatr ansfer.Clipboar d;
import java.awt.datatr ansfer.DataFlav or;
import java.awt.datatr ansfer.StringSe lection;
import java.awt.datatr ansfer.Transfer able;

import javax.swing.*;

public class MenuCheck
{

static class MenuActionListe ner implements ActionListener
{
public void actionPerformed (ActionEvent actionEvent)
{
System.out.prin tln("Selected:" + actionEvent.get ActionCommand() );

}
}

public static void main(String[] args)
{

Runnable runner = new Runnable()
{
public void run()
{

JFrame.setDefau ltLookAndFeelDe corated(true);
JFrame aframe = new JFrame("LogTrac k");
aframe.setDefau ltCloseOperatio n(JFrame.EXIT_O N_CLOSE);

aframe.setSize( 800, 600);
aframe.setVisib le(true);



ActionListener menuListener = new MenuActionListe ner();


JMenuBar menuBar = new JMenuBar();
JMenu logMenu;
JMenuItem menuItem;

//Set up the File menu.
JMenu fileMenu = new JMenu("File");
fileMenu.setMne monic(KeyEvent. VK_F);
menuBar.add(fil eMenu);

//Set up the Open menu item for File menu.
JMenuItem OpenMenuItem = new JMenuItem("Open ");
OpenMenuItem.ad dActionListener (menuListener);
OpenMenuItem.se tMnemonic(KeyEv ent.VK_O);
OpenMenuItem.se tAccelerator(Ke yStroke.getKeyS troke(
KeyEvent.VK_O, ActionEvent.ALT _MASK));
OpenMenuItem.se tActionCommand( "open");

fileMenu.add(Op enMenuItem);

//Set up the Save menu item for File menu.
JMenuItem SaveMenuItem = new JMenuItem("Save ");
SaveMenuItem.se tMnemonic(KeyEv ent.VK_S);
SaveMenuItem.se tAccelerator(Ke yStroke.getKeyS troke(
KeyEvent.VK_S, ActionEvent.ALT _MASK));
SaveMenuItem.se tActionCommand( "save");
SaveMenuItem.ad dActionListener (menuListener);
fileMenu.add(Sa veMenuItem);

//Set up the Quit/Exit menu item for Log menu.
JMenuItem QuitMenuItem = new JMenuItem("Quit ");
QuitMenuItem.se tMnemonic(KeyEv ent.VK_Q);
QuitMenuItem.se tAccelerator(Ke yStroke.getKeyS troke(
KeyEvent.VK_Q, ActionEvent.ALT _MASK));
QuitMenuItem.se tActionCommand( "quit");
QuitMenuItem.ad dActionListener (menuListener);
fileMenu.add(Qu itMenuItem);

JMenu tileMenu = new JMenu("Tiling Frame"); tileMenu.setMne monic(KeyEvent. VK_F);
menuBar.add(til eMenu);

//Setup Copy and Paste

JMenu editMenu = new JMenu("Edit");
editMenu.setMne monic(KeyEvent. VK_E);
menuBar.add(edi tMenu);

JMenuItem cutMenuItem = new JMenuItem("Cut" , KeyEvent.VK_T);
cutMenuItem.add ActionListener( menuListener);
KeyStroke ctrlXKeyStroke = KeyStroke.getKe yStroke("contro l X");
editMenu.add(cu tMenuItem);

JMenuItem copyMenuItem = new JMenuItem ("Copy", KeyEvent.VK_C);
copyMenuItem.ad dActionListener (menuListener);
KeyStroke ctrlCKeyStroke = KeyStroke.getKe yStroke("contro l C");
copyMenuItem.se tAccelerator(ct rlCKeyStroke);
editMenu.add(co pyMenuItem);

JMenuItem pasteMenuItem = new JMenuItem("Past e", KeyEvent.VK_P);
pasteMenuItem.a ddActionListene r(menuListener) ;
KeyStroke ctrlVKeyStroke = KeyStroke.getKe yStroke("contro l V");
pasteMenuItem.s etAccelerator(c trlVKeyStroke);
pasteMenuItem.s etEnabled(false );
editMenu.add(pa steMenuItem);
aframe.setJMenu Bar(menuBar);
aframe.setVisib le(true);

JTextArea text = new JTextArea();
getContentPane( ).add(text);
Clipboard clipbd = getToolkit().ge tSystemClipboar d();

class CopyMenuItem implements ActionListener {
public void actionPerformed (ActionEvent e) {
String selection = text.getSelecte dText();
if (selection == null)
return;
StringSelection clipString = new StringSelection (selection);
clipbd.setConte nts(clipString, clipString);
}
}

class CutMenutItem implements ActionListener {
public void actionPerformed (ActionEvent e) {
String selection = text.getSelecte dText();
if (selection == null)
return;
StringSelection clipString = new StringSelection (selection);
clipbd.setConte nts(clipString, clipString);
text.replaceRan ge("", text.getSelecti onStart(), text
.getSelectionEn d());
}
}

class PastemenuItem implements ActionListener {
public void actionPerformed (ActionEvent e) {
Transferable clipData = clipbd.getConte nts(CutAndPaste .this);
try {
String clipString = (String) clipData
.getTransferDat a(DataFlavor.st ringFlavor);
text.replaceRan ge(clipString, text.getSelecti onStart(), text
.getSelectionEn d());
} catch (Exception ex) {
System.err.prin tln("Not String flavor");
}
}
}





}
};

EventQueue.invo keLater(runner) ;
}
}

thanks.
Sep 17 '06 #1
0 1899

Sign in to post your reply or Sign up for a free account.

Similar topics

5
13957
by: Chris | last post by:
Having been traumatised many years ago when using MFC/C++ to develop front-end code, I've religiously avoided GUI work since and stuck to back-end / server-side projects. So I'm a bit of a novice when it comes to designing Swing-based applications. I'm now having to do more and more GUI work to support my current projects, so I started...
6
6407
by: Joseph | last post by:
hi 1) i plan on having an awt canvas component (to draw graphs) on a JFrame with other swing components..will this be okay? i've read that swing and awt aren't compatible.. 2)Also, if i have a function which simply loops infinately that is part of a form, what happens when a form event (eg button click) occurs? will the event handler...
1
3363
by: Oleg Konovalov | last post by:
Hi, I am trying to implement an application administrative tool with interactive console (i.e. showing some info to the user and taking user input) using Swing and JDK 1.4. Up until now I have done info display part after selecting items from the menu (using JTextArea and append() ),
2
2155
by: Hank | last post by:
Please help me diagnose the following problem. I have a Swing program (running under 1.4.2_04) that uses JNI to get some hardware-level information and return that info as a csv String. The String returns and I am able to run .split() on it. At some point after that (this seems to vary), my program simply exits during the middle of...
3
4851
by: Sherrod Faulks | last post by:
I'm using Jython and in the python script I do: from javax import swing result = javax.swing.JOptionPane.showInputDialog(wC, cmd,"Prompt from " + client.serverName, JOptionPane.PLAIN_MESSAGE) wC is a JFrame, cmd is a String and client.serverName is a String. It won't show the JOptionPane, why?
2
2038
by: asj | last post by:
I have to honestly say I was flabbergasted by this report. Obviously, I have no problems asserting that Java is #1 in the enterprise (JEE), or in the mobile space (JME), and I do know it's made good strides on the desktop, with popular apps like Azureus, Limewire, and others leading the way. But never in a million years did I imagine this....
1
3148
by: bruce628 | last post by:
I want to use SWT tab compnent and make it be multiline,but I fail.please see the class TabFolderExample. Can aneone help me? import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDialog;
1
2773
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. When a radio button wass selected and "OK/Next" was clicked on, I was able to write some code inside the "ActionPerformed" method to verify that the...
6
51147
by: r035198x | last post by:
I have put together this article to give people starting Swing an awareness of issues that need to be considered when creating a Swing application. Most of the Swing tutorials that I have seen just help people display some nice widgets quickly without explaining some issues that need to be tackled when a full application needs to be developed....
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.