473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Swing Cut info

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.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javax.swing.*;

public class MenuCheck
{

static class MenuActionListener implements ActionListener
{
public void actionPerformed(ActionEvent actionEvent)
{
System.out.println("Selected:" + actionEvent.getActionCommand());

}
}

public static void main(String[] args)
{

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

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame aframe = new JFrame("LogTrack");
aframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

aframe.setSize(800, 600);
aframe.setVisible(true);



ActionListener menuListener = new MenuActionListener();


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

//Set up the File menu.
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);

//Set up the Open menu item for File menu.
JMenuItem OpenMenuItem = new JMenuItem("Open");
OpenMenuItem.addActionListener(menuListener);
OpenMenuItem.setMnemonic(KeyEvent.VK_O);
OpenMenuItem.setAccelerator(KeyStroke.getKeyStroke (
KeyEvent.VK_O, ActionEvent.ALT_MASK));
OpenMenuItem.setActionCommand("open");

fileMenu.add(OpenMenuItem);

//Set up the Save menu item for File menu.
JMenuItem SaveMenuItem = new JMenuItem("Save");
SaveMenuItem.setMnemonic(KeyEvent.VK_S);
SaveMenuItem.setAccelerator(KeyStroke.getKeyStroke (
KeyEvent.VK_S, ActionEvent.ALT_MASK));
SaveMenuItem.setActionCommand("save");
SaveMenuItem.addActionListener(menuListener);
fileMenu.add(SaveMenuItem);

//Set up the Quit/Exit menu item for Log menu.
JMenuItem QuitMenuItem = new JMenuItem("Quit");
QuitMenuItem.setMnemonic(KeyEvent.VK_Q);
QuitMenuItem.setAccelerator(KeyStroke.getKeyStroke (
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
QuitMenuItem.setActionCommand("quit");
QuitMenuItem.addActionListener(menuListener);
fileMenu.add(QuitMenuItem);

JMenu tileMenu = new JMenu("Tiling Frame"); tileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(tileMenu);

//Setup Copy and Paste

JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
menuBar.add(editMenu);

JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T);
cutMenuItem.addActionListener(menuListener);
KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X");
editMenu.add(cutMenuItem);

JMenuItem copyMenuItem = new JMenuItem ("Copy", KeyEvent.VK_C);
copyMenuItem.addActionListener(menuListener);
KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke("control C");
copyMenuItem.setAccelerator(ctrlCKeyStroke);
editMenu.add(copyMenuItem);

JMenuItem pasteMenuItem = new JMenuItem("Paste", KeyEvent.VK_P);
pasteMenuItem.addActionListener(menuListener);
KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V");
pasteMenuItem.setAccelerator(ctrlVKeyStroke);
pasteMenuItem.setEnabled(false);
editMenu.add(pasteMenuItem);
aframe.setJMenuBar(menuBar);
aframe.setVisible(true);

JTextArea text = new JTextArea();
getContentPane().add(text);
Clipboard clipbd = getToolkit().getSystemClipboard();

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

class CutMenutItem implements ActionListener {
public void actionPerformed(ActionEvent e) {
String selection = text.getSelectedText();
if (selection == null)
return;
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
text.replaceRange("", text.getSelectionStart(), text
.getSelectionEnd());
}
}

class PastemenuItem implements ActionListener {
public void actionPerformed(ActionEvent e) {
Transferable clipData = clipbd.getContents(CutAndPaste.this);
try {
String clipString = (String) clipData
.getTransferData(DataFlavor.stringFlavor);
text.replaceRange(clipString, text.getSelectionStart(), text
.getSelectionEnd());
} catch (Exception ex) {
System.err.println("Not String flavor");
}
}
}





}
};

EventQueue.invokeLater(runner);
}
}

thanks.
Sep 17 '06 #1
0 1887

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

Similar topics

5
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...
6
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...
1
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...
2
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...
3
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) ...
2
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...
1
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...
1
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. ...
6
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.