How to get the text in a Text Area to be saved to a text file when a button is clicked?
I have implemented the GUI and when the btnContinue button is clicked I want the content of txtOrder to be saved to a text file in notepad.
I'm not a pro programmer in Java so pls correct me if I'm wrong, but I would try this:
FileWriter fw = null;
try
{
fw = new FileWriter("Output.txt",true);
fw.write(txtOrder.getText())
//textArea.write(fw);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (fw!=null)
{
fw.close();
}
}
Related
Screenshot using PrintScreenand save it in work doc. I tried below but not working. i need a complete code to take screenshot if i press printscreen button and save it in word doc . i need to give doc name dynamically in cmd. until i stop he program, it have to take screen shot and save it in same doc.
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, AWTException {
try {
// Set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// Create a KeyEventDispatcher to listen to keyboard events
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_PRINTSCREEN) {
// Take a screenshot when the print screen button is pressed
try {
// Create a Robot object
Robot robot = new Robot();
// Get the size of the screen
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
// Capture the screen image
BufferedImage screenImage = robot.createScreenCapture(screenRect);
// Get the current date and time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String fileName = "screenshot_" + dateFormat.format(new Date()) + ".png";
// Save the screenshot to a file
ImageIO.write(screenImage, "png", new File(fileName));
// Create a Word document and add the screenshot
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
// Create a run in the paragraph
XWPFRun run = paragraph.createRun();
// Add the screenshot to the run
FileInputStream stream = new FileInputStream(fileName);
run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, fileName, Units.toEMU(screenImage.getWidth()), Units.toEMU(screenImage.getHeight()));
// Close the stream
stream.close();
// Save the Word document
FileOutputStream out = new FileOutputStream("screenshot_" + dateFormat.format(new Date()) + ".docx");
document.write(out);
out.close();
// Show a message indicating that the screenshot was saved
JOptionPane.showMessageDialog(null, "Screenshot saved in screenshot.docx", "Information", JOptionPane.INFORMATION_MESSAGE);
return false;
}
catch(IOException | AWTException | InvalidFormatException exc){
exc.printStackTrace();}
return false;
}
return false;
}
});
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Desktop desktop = Desktop.getDesktop();
File sitetxt = new File(System.getProperty("user.dir") + File.separator + "site.txt");
MenuItem settings = new MenuItem("Settings");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
desktop.open(sitetxt);
} catch (IOException e1) {
System.out.println("site.txt not found.");
e1.printStackTrace();
}
}
});
popup.add(settings);
trayIcon.setPopupMenu(popup);
I'm trying to open a text file when the user clicks "Settings" from the right click menu of the system tray icon. The default text editor is set to Notepad. There is another menu item called "Exit" but it works fine.
i am new to eclipse plugin development. i created a plugin which loads a unicode saved text file from the folder & displays it on a dialog & also setting it for the Label. If it is, in an English Language, everything is working fine. But if i try to load any other language text, it is displaying empty. How can i get through this.
Here is some code on how i am displaying it on dialog:
Shell shell = Display.getCurrent().getActiveShell();
// Loading the file by creating the assets folder & Temp.txt file inside
// Eclipse Plugin
URL url = FileLocator.find(bundle, new Path("assets/Temp.txt"), null);
URL fileUrl = null;
try {
fileUrl = FileLocator.toFileURL(url);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
File file = new File(fileUrl.getPath());
String Message = "";
try {
if (file.exists()) {
BufferedReader in = new BufferedReader(
new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
if (str.contains(LanguageSelected)) {
System.out.println(str);
Message = Message + "\n" + str;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Creating the dialog
Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
...
// Adding Label to dialog
Label LabelMessage = new Label(dialog, SWT.LEFT);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 6;
// Adding Message to the dialog
LabelMessage.setLayoutData(data);
LabelMessage.setBackground(new Color(null, 255, 255, 255)); // White
LabelMessage.setText(Message);
In your launch configuration, make sure you are setting the -nl parameter to the language you want to display. See http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html for information.
I'm making a little program that when a jButton is clicked, it opens the downloads folder from the file explorer. I have the button set up, I'm just having troubles getting the folder open.
Here is my code.
private void downloadsActionPerformed(java.awt.event.ActionEvent evt) {
String download = System.getenv("downloads");
File downloadsDir = new File(download);
try {
Desktop.getDesktop().open(downloadsDir);
} catch (IOException ex) {
Logger.getLogger(GettingFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
JPanel with 3 JButton and I need only two of them to be captured...
public static void grabScreenShot(JPanel panel) {
BufferedImage image = (BufferedImage) panel.createImage(
panel.getSize().width, panel.getSize().height);
panel.paint(image.getGraphics());
File file = null;
file = new File("Customers");
if (!file.exists()) {
file.mkdir();
}
try {
file = new File("Customers" + File.separator
+ String.valueOf(System.currentTimeMillis()));
ImageIO.write(image, "png", file);
System.out.println("Image was created");
} catch (IOException e) {
System.out.println("Had trouble writing the image.");
e.printStackTrace();
}
}
How to avoid unnecessary components to be captured.?
You can try to override paintComponent() of the buttons and introduce a flag needPaint. The flag is true by default.
if (needPaint) {
super.paintComponent(g);
}
In your grabScreenShot() set the flag to false for the button to be hidden and reset it back after panel.paint(image.getGraphics()); call