Need help to print Jpanel (with components) in java

View previous topic View next topic Go down

Java Need help to print Jpanel (with components) in java

Post by BIT0201-farhan on Fri Feb 25, 2011 10:08 pm

I'm stuck with this problem for couple of days:?: .Though i've found probable solution,but don't know why the code is conspiring(t think so,though the problem is mine Crying or Very sad ) against me.I'm posting the source code here.Would anybody please help?It's urgent.
thanks in advance.

Code:

package diabetics;

import java.awt.Color;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

public class InfoPanel extends JPanel {
   /**
    *
    */
   private static final long serialVersionUID = 1L;

   JLabel idLabel, dateLabel, nameLabel, ageLabel, heightLabel, ftLabel,
         inchLabel, sexLabel, weightLabel, kgLabel;

   JLabel headerLabel;

   JLabel testNameLabel, pgrLabel, cusLabel;
   JLabel resultLabel, unitLabel, molLabel, nil1Label, nil2Label, nvalueLabel,
         rangelJLabel;
   JLabel commentLabel, chklJLabel, doctorlJLabel;
   JComboBox cusBox, commentBox;
   JTextField idField, dateField, nameField, weightField, pgrField;
   JSpinner ageSpinner, ftSpinner, inchSpinner;
   JRadioButton maleButton, femaleButton;

   public InfoPanel() {
      // infoPanel = new JPanel();
      setSize(510, 700);
      setBackground(Color.white);
      setVisible(true);

      setLayout(null);

      idLabel = new JLabel("Patient's ID ");
      idLabel.setBounds(10, 10, 90, 25);
      add(idLabel);

      idField = new JTextField();
      idField.setBounds(105, 10, 70, 25);
      add(idField);

      dateLabel = new JLabel("Date");
      dateLabel.setBounds(275, 10, 55, 25);
      add(dateLabel);

      dateField = new JTextField();
      dateField.setBounds(335, 10, 90, 25);
      add(dateField);

      nameLabel = new JLabel("Patient's Name");
      nameLabel.setBounds(10, 70, 120, 25);
      add(nameLabel);

      nameField = new JTextField();
      nameField.setBounds(140, 70, 180, 25);
      add(nameField);

      ageLabel = new JLabel("Age");
      ageLabel.setBounds(355, 70, 35, 25);
      add(ageLabel);

      ageSpinner = new JSpinner(new SpinnerNumberModel(35, 5, 90, 1));
      ageSpinner.setBounds(395, 70, 55, 25);
      add(ageSpinner);

      heightLabel = new JLabel("Height");
      heightLabel.setBounds(10, 100, 60, 25);
      add(heightLabel);

      ftSpinner = new JSpinner(new SpinnerNumberModel(5, 1, 8, 1));
      ftSpinner.setBounds(75, 100, 60, 25);
      add(ftSpinner);

      ftLabel = new JLabel("ft");
      ftLabel.setBounds(140, 100, 20, 25);
      add(ftLabel);

      inchSpinner = new JSpinner(new SpinnerNumberModel(6, 0, 12, 1));
      inchSpinner.setBounds(165, 100, 60, 25);
      add(inchSpinner);

      inchLabel = new JLabel("inch.");
      inchLabel.setBounds(230, 100, 35, 25);
      add(inchLabel);

      sexLabel = new JLabel("Sex");
      sexLabel.setBounds(275, 100, 35, 25);
      add(sexLabel);

      ButtonGroup bg = new ButtonGroup();

      maleButton = new JRadioButton("Male");
      maleButton.setBounds(315, 100, 70, 25);
      add(maleButton);
      maleButton.setSelected(true);

      femaleButton = new JRadioButton("Female");
      femaleButton.setBounds(390, 100, 85, 25);
      add(femaleButton);
      bg.add(maleButton);
      bg.add(femaleButton);

      weightLabel = new JLabel("Weight");
      weightLabel.setBounds(10, 130, 75, 25);
      add(weightLabel);

      weightField = new JTextField();
      weightField.setBounds(95, 130, 50, 25);
      add(weightField);

      kgLabel = new JLabel("Kg's");
      kgLabel.setBounds(150, 130, 30, 25);
      add(kgLabel);

      headerLabel = new JLabel("Test Sample");
      headerLabel.setBounds(220, 200, 80, 50);
      add(headerLabel);

      testNameLabel = new JLabel("Test Name");
      testNameLabel.setBounds(10, 260, 200, 25);
      add(testNameLabel);

      resultLabel = new JLabel("Result");
      resultLabel.setBounds(220, 260, 80, 25);
      add(resultLabel);

      unitLabel = new JLabel("Unit");
      unitLabel.setBounds(330, 260, 75, 25);
      add(unitLabel);

      nvalueLabel = new JLabel("Normal Range");
      nvalueLabel.setBounds(410, 260, 105, 25);
      add(nvalueLabel);

      pgrLabel = new JLabel("Plasma Glucose(Random)");
      pgrLabel.setBounds(5, 290, 200, 25);
      add(pgrLabel);

      pgrField = new JTextField();
      pgrField.setBounds(210, 290, 60, 25);
      add(pgrField);

      molLabel = new JLabel("mMol/L");
      molLabel.setBounds(320, 290, 80, 25);
      add(molLabel);

      nvalueLabel = new JLabel("7.8 to 11.1");
      nvalueLabel.setBounds(410, 290, 105, 25);
      add(nvalueLabel);

      cusLabel = new JLabel("CUS");
      cusLabel.setBounds(5, 320, 200, 25);
      add(cusLabel);

      cusBox = new JComboBox();
      cusBox.setBounds(210, 320, 70, 25);
      add(cusBox);

      cusBox.addItem("Nil");
      cusBox.addItem("+");
      cusBox.addItem("++");
      cusBox.addItem("+++");

      nil1Label = new JLabel("Nil");
      nil1Label.setBounds(330, 320, 70, 25);
      add(nil1Label);

      nil2Label = new JLabel("Nil");
      nil2Label.setBounds(420, 320, 70, 25);
      add(nil2Label);

      commentLabel = new JLabel("Comments");
      commentLabel.setBounds(10, 390, 75, 25);
      add(commentLabel);

      commentBox = new JComboBox();
      commentBox.setBounds(90, 390, 150, 25);
      add(commentBox);
      commentBox.addItem("No diabetics");
      commentBox.addItem("Yes, diabetics exists");

      chklJLabel = new JLabel("Checked by:");
      chklJLabel.setBounds(10, 500, 120, 25);
      add(chklJLabel);

      doctorlJLabel = new JLabel("Doctor's Name");
      doctorlJLabel.setBounds(315, 500, 120, 25);
      add(doctorlJLabel);
   }

}

Code:


package diabetics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.UIManager;

public class UIDesign extends JFrame implements ActionListener {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   public UIDesign() {

      setLayout(null);

      try {
         UIManager
               .setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
      } catch (Exception e) {
         e.printStackTrace();
      }

      setTitle("DiaBe Check");
      setSize(650, 700);
      setLocationRelativeTo(null);
      setResizable(false);

      InfoPanel inf = new InfoPanel();
      add(inf);
      ButtonPanel butt = new ButtonPanel();
      add(butt);

   }

   @Override
   public void actionPerformed(ActionEvent e) {

   }

}

Code:


package diabetics;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel implements ActionListener {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   JButton newButton, saveButton, searchButton, printButton, exitButton;

   public ButtonPanel() {
      // TODO Auto-generated constructor stub
      setBounds(515, 150, 135, 700);

      newButton = new JButton("New Page");
      newButton.setBounds(525, 150, 100, 40);
      newButton.addActionListener(this);
      add(newButton);

      saveButton = new JButton("Save");
      saveButton.setBounds(525, 200, 100, 40);
      add(saveButton);
      saveButton.addActionListener(this);

      searchButton = new JButton("Search");
      searchButton.setBounds(525, 250, 100, 40);
      add(searchButton);
      searchButton.addActionListener(this);

      printButton = new JButton("Print");
      printButton.setBounds(525, 300, 100, 40);
      add(printButton);
      printButton.addActionListener(this);

      exitButton = new JButton("Exit");
      exitButton.setBounds(525, 350, 100, 40);
      add(exitButton);
      exitButton.addActionListener(this);
      setBackground(Color.lightGray);

   }

   @Override
   public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub

      if (e.getActionCommand() == "New Page") {
         UIDesign newUI = new UIDesign();
         newUI.setVisible(true);
      } else if (e.getActionCommand() == "Print") {
         //InfoPanel i =new InfoPanel();
         

         print(getGraphics());

      } else if (e.getActionCommand() == "Exit") {
         System.exit(0);
      }

   }

}

Code:


package diabetics;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
//import com.sun.java.swing.*; //Use this swing import
//line for release before Swing 1.1 and Java 2
import javax.swing.*;

public class printpanel extends JPanel implements ActionListener, Printable {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   public printpanel() {
      setBackground(Color.white);
      InfoPanel b = new InfoPanel();
      System.err.println("paic");
      b.setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {
      PrinterJob printJob = PrinterJob.getPrinterJob();
      printJob.setPrintable(this);
      try {
         printJob.print();
      } catch (Exception PrintException) {
      }
   }

   public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
      if (pi >= 1) {
         return Printable.NO_SUCH_PAGE;
      }

      g.translate(600, 700);
      Font f = new Font("Monospaced", Font.PLAIN, 12);
      System.err.println("paic2");
      g.setFont(f);
      paint(g);
      return Printable.PAGE_EXISTS;
   }

}

Code:



package diabetics;

public class MainClass {
   public static void main(String[] args) {
      UIDesign ui = new UIDesign();
      

      ui.setVisible(true);

   }
}
avatar
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

View user profile

Back to top Go down

Java Re: Need help to print Jpanel (with components) in java

Post by BIT0101-rayhan on Sat Feb 26, 2011 1:36 am

Well, I am not sure about the part of the code that is creating problems for you, would you please mention what you are trying to do and where you are being stuck
avatar
BIT0101-rayhan
Release Candidate
Release Candidate

Posts : 107
Points : 177

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum