Results 1 to 3 of 3
  1. #1
    mConfused is offline Member
    Join Date
    Apr 2010
    Location
    Manchester
    Posts
    5
    Rep Power
    0

    Default Printing multiple user entries.

    Hi there guys. Was hoping someone could give me some ideas on how to work this segment of code.

    I am basically trying to print 2 user inputs. 1 is a textbox and the other is a jcombobox.

    Java Code:
    import java.awt.print.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    
    public class PrintingTest extends javax.swing.JFrame implements Printable{
    
        JPanel mainPanel = new JPanel();
        String text = "Write something here :D";
        JTextField txtTest = new JTextField(text, 20);
        String[] choiceFormat = {"Open Format", "Importance Format", "Dichotomous Format", "Choice Format"};
        JComboBox jcbChoice = new JComboBox(choiceFormat);
    
        /** Creates new form PrintingTest */
        public PrintingTest() {
            initComponents();
            displayPanel();
        }
    
    public void displayPanel()
        {
            JButton btnTest = new JButton("Test !");
    
            mainPanel.add(txtTest);
            mainPanel.add(jcbChoice);
            mainPanel.add(btnTest);
    
            btnTest.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnTestActionPerformed(evt);
                }
            });
    
            this.add(mainPanel);
    
            mainPanel.setSize(400,200);
            mainPanel.setBackground(Color.YELLOW);
        }    
        
        private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {
            JTextField txtChoice = new         JTextField(jcbChoice.getSelectedItem().toString());
            MessageFormat question = createFormat(txtTest);
            MessageFormat choice = createFormat(txtChoice);
    
            PrintingTask task = new PrintingTask(question, choice);
            task.execute();
        }
    
    private class PrintingTask extends SwingWorker<Object, Object> {
            private final MessageFormat questionFormat;
            private final MessageFormat choiceFormat;
            private volatile boolean complete = false;
            
            public PrintingTask(MessageFormat question, MessageFormat choice){
                this.questionFormat = question;
                this.choiceFormat = choice;
            }
            
            @Override
            protected Object doInBackground() {
                try {
                    complete = mainPanel.print(questionFormat, choiceFormat,
                            true, null, null, null);
                } catch (PrinterException ex) {
                   JOptionPane.showMessageDialog(null, "Unable to print + Error : "
                            + ex);
                } catch (SecurityException ex) {
                    JOptionPane.showMessageDialog(null, "Unable to access selected printer ! ");
                }
                return null;
            }
            
            @Override
            protected void done() {
                JOptionPane.showMessageDialog(null, "Printing Done !");
            }
        }
        
        private MessageFormat createFormat(JTextField source) {
            String text2 = source.getText();
            if (text2 != null && text2.length() > 0) {
                try {
                    return new MessageFormat(text2);
                } catch (IllegalArgumentException e) {
                    JOptionPane.showMessageDialog(null,"Sorry, this format is invalid.");
                }
            }
            return null;
        }
    
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new PrintingTest().setVisible(true);
                }
            });
        }
    The following website is what I am trying to do in a similar fashion but in a smaller scale. I would really appreciate any help.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Is it a server client or what?

  3. #3
    mConfused is offline Member
    Join Date
    Apr 2010
    Location
    Manchester
    Posts
    5
    Rep Power
    0

    Default

    No this is not a client server. Just a basic user input program but trying to use the print method. I cant seem to get it right and each tutorial found online has their own style which is not working for mine :(

Similar Threads

  1. Ask user multiple Yes/No Questions
    By jlafber in forum AWT / Swing
    Replies: 1
    Last Post: 03-30-2010, 11:55 AM
  2. Replies: 1
    Last Post: 10-24-2009, 02:18 AM
  3. Multiple line user input
    By jointhelabel in forum New To Java
    Replies: 3
    Last Post: 10-26-2008, 02:41 AM
  4. Replies: 0
    Last Post: 02-12-2008, 05:05 PM
  5. Printing Swing components in multiple pages
    By Rajeswari in forum AWT / Swing
    Replies: 0
    Last Post: 07-27-2007, 05:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •