I am trying to figure out why setting the contents of the system clipboard won't work for me. I programmatically set the clipboard contents. When i use the output part of the code, it works. However, when i try copy/pasting in any text editor, it is blank.
hovercraft edit, code from github:
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws HeadlessException,
UnsupportedFlavorException, IOException {
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection("hi there"), null);
System.out.println(((String) Toolkit.getDefaultToolkit()
.getSystemClipboard().getData(DataFlavor.stringFlavor)));
}
}
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
public class tester{
public static void main(String[] args){
// from string to clipboard
StringSelection selection = new StringSelection("hi");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
}
}
This program does it. It will set the String "hi" to clipboard. You can change it to a variable.
Linux cut and paste is a bit weird these days, because there are at least two different ways of doing it. In short, sometimes it's better to just paste with the middle button, and other times it's better to control-v, and sometimes neither seems to work.
Running autocutsel as a background process seems to help.
http://www.nongnu.org/autocutsel/
Related
I am a beginner.
I want to use addTableModelListener in the followinc code, but I am not sure how to import in .Java file. I am not sure but it whould be something like import javax.swing.table;
tableModel.addTableModelListener(new TableModelListener(){
#Override
public void tableChanged(TableModelEvent tableModelEvent) {
if(table.isEditing())
String value = table.getValueAt(table.getSelectedRow(),3);
//do stuff with value
}
});
We can import addTableModelListener as below:
import javax.swing.event.TableModelListener;
For example, refer to the following link:
Reference Link
I am trying to tap on an Element on 1st screen[say an element named Views] and upon it's click a new screen opens and there I want to tap on an element named[Expandable Lists].
So on 2nd attempt I want to use Tap function to the operation for me. Tap() function is not working whereas using .click() the tap does work. Please have a look at the code that I've written till this step:
import java.net.MalformedURLException;
import java.time.Duration;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.interactions.touch.TouchActions.*;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
import static io.appium.java_client.touch.WaitOptions.waitOptions;
public class Gestures extends Parent {
public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
AndroidDriver<AndroidElement> driver = Capabilities();
driver.findElementByXPath("//android.widget.TextView[#text= 'Views']").click();
TouchActions t = new TouchActions(driver);
WebElement expandList= driver.findElementByXPath("//android.widget.TextView[#text='Expandable Lists']");
//t.tap(tapOptions().withElement(element(webElement)));
// for element we need to import it's library just like we did
//for the WebElement
t.singleTap(expandList);
t.perform();
TouchAction(driver).tap(tapOptions()
.withElement(element(expandList)))
.waitAction(waitOptions(Duration.ofMillis(250))).perform();
}
}
You can use click() instead of tap().
To make sure click() is working fine, you can check if your expected screen appear after the clicking the button.
I want to Access Clipboard data/text in my Java Program in Windows 10 system targeted program. Any code snippets or Class that is used to access the clipboard data?
This code snippet is for accessing and printing the clipboard data in Java:
import java.awt.datatransfer.*;
import java.awt.*;
/**
* Demo to access System Clipboard
*/
public class SystemClipboardAccess {
public static void main(String args[]) throws Exception {
// Create a Clipboard object using getSystemClipboard() method
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
// Get data stored in the clipboard that is in the form of a string (text)
System.out.println(c.getData(DataFlavor.stringFlavor));
}
}
Use Toolkit#getSystemClipboard:
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
public static String readClipboard() {
return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
I have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.
I'm a beginner in Java and I followed a tutorial to write this program (yes, I that much of a beginner) and it works perfectly when I run it on Eclipse. It also runs great on the computer I coded it on. However, if I send it to another computer (just the .jar file) and run it, it fails because it can't find the icon. Here is everything I've got. The icon I'm using is saved in the bin folder along with all the class files for the program. For privacy reasons, I replaced certain lines with "WORDS".
The tutorial I followed in two parts:
Part 1 - https://buckysroom.org/videos.php?cat=31&video=18027
Part 2 - https://buckysroom.org/videos.php?cat=31&video=18028
My main class (I called it apples cause the tutorial did).
import javax.swing.JFrame;
public class apples {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(1920,1080);
go.setVisible(true);
}
}
And now my second class, "Gui":
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton custom;
public Gui () {
super("WORDS");
setLayout(new FlowLayout());
Icon b = new ImageIcon(getClass().getResource("b.png"));
custom = new JButton(null, b);
custom.setToolTipText("WORDS");
add(custom);
HandlerClass handler = new HandlerClass();
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
}
}
}
Thank you so much for helping!
It's worth reading Loading Images Using getResource where it's explained in detail along with loading images from jar as well.
You can try any one based on image location.
// Read from same package
ImageIO.read(getClass().getResourceAsStream("b.png"));
// Read from src/images folder
ImageIO.read(getClass().getResource("/images/b.png"))
// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/b.png"))
Read more...