Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di zambo
    Registrato dal
    Jan 2001
    Messaggi
    15

    URGENTE!! - Java - Stampare file di testo.

    Salve a tutto il forum.
    Devo trovare il modo, usando java, di stampare un banalissimo file di testo su una stampante in rete.
    Ho già provato diversi modi tra cui l'utilizzo di Runtime con il comando DOS "PRINT" ma non è molto affidabile.
    Ho trovato su internet alcune classi di esempio per la stampa ma sono estremamente macchinosi e tutti sfruttano le classi per la grafica 2D.
    Sapete se esistono dei metodi meno laboriosi oppure della classi pronte per l'uso.

    Ringrazio chiunque possa aiutarmi.
    Zambo
    ---------------------------------------
    Se il tuo capo si ammazza per obbligarti a fare un lavoro che non ti compete, si paziente ... lascialo morire.
    ---------------------------------------

  2. #2
    potresti richiamare il notepad di windows:

    "notepad /p filedistampa.txt"

    ...magari è possibilen ascondere l' interfaccia che potrebbe darti fastidio!
    jabjoint

  3. #3
    Non conosco un modo per stampare file di testo, ma su "Java 2 tecniche avanzate" ho trovato questo codice che forse ti può essere d'aiuto (SDK 1.4):

    codice:
    /**
       @version 1.00 2001-08-08
       @author Cay Horstmann
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import java.util.*;
    import javax.print.*;
    import javax.swing.*;
    
    /**
       This program demonstrates the use of print services.
       The program lets you print a GIF image to any of the print
       services that support the GIF document flavor.
    */
    public class PrintServiceTest
    {
       public static void main(String[] args)
       {
          JFrame frame = new PrintServiceFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.show();
       }
    }
    
    /**
       This frame displays the image to be printed. It contains
       menus for opening an image file, printing, and selecting
       a print service.
    */
    class PrintServiceFrame extends JFrame
    {
       public PrintServiceFrame()
       {
          setTitle("PrintServiceTest");
          setSize(WIDTH, HEIGHT);
    
          // set up menu bar
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
    
          JMenu menu = new JMenu("File");
          menuBar.add(menu);
    
          JMenuItem openItem = new JMenuItem("Open");
          menu.add(openItem);
          openItem.addActionListener(new
             ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   openFile();
                }
             });
    
          JMenuItem printItem = new JMenuItem("Print");
          menu.add(printItem);
          printItem.addActionListener(new
             ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   printFile();
                }
             });
    
          JMenuItem exitItem = new JMenuItem("Exit");
          menu.add(exitItem);
          exitItem.addActionListener(new
             ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   System.exit(0);
                }
             });
    
          menu = new JMenu("Printer");
          menuBar.add(menu);
          DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
          addPrintServices(menu, flavor);
    
          // use a label to display the images
          label = new JLabel();
          Container contentPane = getContentPane();
          contentPane.add(label);
       }
    
       /**
          Adds print services to a menu
          @param menu the menu to which to add the services
          @param flavor the flavor that the services need to support
       */
       public void addPrintServices(JMenu menu, DocFlavor flavor)
       {      
          PrintService[] services 
             = PrintServiceLookup.lookupPrintServices(flavor, null);
          ButtonGroup group = new ButtonGroup();
          for (int i = 0; i < services.length; i++)
          {
             final PrintService service = services[i];
             JRadioButtonMenuItem item 
                = new JRadioButtonMenuItem(service.getName());
             menu.add(item);
             if (i == 0) 
             {
                item.setSelected(true);
                currentService = service;
             }
             group.add(item);
             item.addActionListener(new
                ActionListener()
                {
                   public void actionPerformed(ActionEvent event)
                   {
                      currentService = service;
                   }
                });
          }
       }
    
       /**
          Open a GIF file and display the image.
       */
       public void openFile()
       {
          // set up file chooser
          JFileChooser chooser = new JFileChooser();
          chooser.setCurrentDirectory(new File("."));
          
          // accept all files ending with .gif
          chooser.setFileFilter(new
             javax.swing.filechooser.FileFilter()
             {
                public boolean accept(File f)
                {
                   return f.getName().toLowerCase()
                      .endsWith(".gif")
                      || f.isDirectory();
                }
                public String getDescription()
                {
                   return "GIF Images";
                }
             });
    
          // show file chooser dialog
          int r = chooser.showOpenDialog(PrintServiceFrame.this);
          
          // if image file accepted, set it as icon of the label
          if(r == JFileChooser.APPROVE_OPTION)
          {
             fileName = chooser.getSelectedFile().getPath();
             label.setIcon(new ImageIcon(fileName));
          }
       }
    
       /**
          Print the current file using the current print service.
       */
       public void printFile()
       {
          try
          {
             if (fileName == null) return;
             if (currentService == null) return;
             FileInputStream in = new FileInputStream(fileName);
             DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;      
             Doc doc = new SimpleDoc(in, flavor, null);
             DocPrintJob job = currentService.createPrintJob();
             job.print(doc, null);      
          }
          catch (FileNotFoundException exception)
          {
             JOptionPane.showMessageDialog(this, exception);
          }
          catch (PrintException exception)
          {  
             JOptionPane.showMessageDialog(this, exception);
          }
       }
    
       private JLabel label;
       private String fileName;
       private PrintService currentService;
       private static final int WIDTH = 300;
       private static final int HEIGHT = 400;
    }
    È una classe per stampare un'immagine .gif, ma se sostituisci INPUT_STREAM.GIF con BYTE_ARRAY.TEXT_PLAIN_UTF_8 forse...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.