JAVA CLASSES

Tabbed pane and applets

Page 1 of 1

1 Replies - 1248 Views - Last Post: 28 April 2010 - 09:41 PM Rate Topic: -----

#1 Guest_DEE*


Reputation:

JAVA CLASSES

Posted 28 April 2010 - 03:23 PM

Can i open a class from another class? Im trying to have a tabbed pane that can have different classes in each pane. The code for the tabbed pane and the applet i want to add are both given. i would like to add the applet to the tabbed pane.. is there a way i can do that? PLEASE HELP! :)

FOR EXAMPLE:
In this tabbed pane, i want to add a painting/drawing applet!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JComponent;

public class PROJECT{

  private JTabbedPane tabbedPane;
  private JFrame dlframe;

      public PROJECT(){

        dlframe = new JFrame("A Java Project");

        dlframe.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
             System.exit(0);
           }
         });

         tabbedPane = new JTabbedPane(SwingConstants.LEFT);
         tabbedPane.setBackground(Color.pink);
         tabbedPane.setForeground(Color.white);

         AddTabbedPaneItems();

         buildMenu();

         dlframe.getContentPane().add(tabbedPane);

         dlframe.pack();
         dlframe.setSize(800, 400);
         dlframe.setBackground(Color.pink);
         dlframe.setVisible(true);
         }

         private void AddTabbedPaneItems(){


           tabbedPane.addTab("Welcome",null,new Panel1(),"Welcome");

           tabbedPane.addTab("Buttons",null,new Panel2(),"Example of tabbed pane with buttons");

           tabbedPane.addTab("Check Box",null,new Panel3(),"Example of tabbed pane with check box");

           tabbedPane.addTab("Combo Box",null,new Panel4(),"Example of tabbed pane with combo box");

           tabbedPane.addTab("JTextField",null,new Panel5(),"Example of tabbed pane with Jtextfield");

           tabbedPane.addTab("Slider",null,new Panel6(),"Example of tabbed pane with slider");
	   }


         private void buildMenu(){JMenuBar mb = new JMenuBar();
			 JMenu menu = new JMenu("File");
			 JMenuItem item = new JMenuItem("Exit");
			 item.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
                System.exit(0);
             }
             });

             menu.add(item);
             mb.add(menu);
             dlframe.setJMenuBar(mb);
             }



     public static void main(String[] args){
      PROJECT dl = new PROJECT();
      }
}

 class Panel1 extends JPanel{
		  Panel1(){
			  JLabel lbl = new JLabel("A simple project to show an application that uses tabbed panes and widgets such as labels, buttons, checkboxes, etc.");
			  add(lbl);
			  JLabel lbl1 = new JLabel("CLICK ON A DIFFERENT TABBED PANE");
			  add(lbl1);
		  }

		  }

class Panel2 extends JPanel implements ActionListener{
		  JLabel lbl2;
		  JButton b1;
		  JButton b2;
		  JButton b3;
		  JButton b4;
		  JLabel lbl3;

		  Panel2(){
			  lbl2 = new JLabel("Choose a City!");
			  add(lbl2);

			  b1 = new JButton("New York");
			  add(b1);
			  b1.addActionListener(this);
			  b2 = new JButton("London");
			  add(b2);
			  b2.addActionListener(this);
			  b3 = new JButton("Hong Kong");
			  add(b3);
			  b3.addActionListener(this);
			  b4 = new JButton("Tokyo");
			  add(b4);
			  b4.addActionListener(this);

			  lbl3 = new JLabel("TEXT");
			  add(lbl3);
		  }

		   public void actionPerformed(ActionEvent evt){
		  			  		 if (evt.getSource() == b1){
		  			  		 lbl3.setText("NEWYORK");
		  			  		 }

		  	          }
}

class Panel3 extends JPanel{
 		  Panel3(){
			  Panel3 object = new Panel3();
			  

 		  }
	  }

 /* class Panel4 extends JPanel{
 		  Panel4(){
			  JLabel lbl4 = new JLabel("Make a selection from the list");
			  add(lbl4);
			  JComboBox jcb = new JComboBox();
			  jcb.addItem("Choice 1");
			  jcb.addItem("Choice 2");
			  jcb.addItem("Choice 3");
			  add(jcb);
 		  }
	  }

 class Panel5 extends JPanel{
 		  Panel5(){
			  JTextField jtf = new JTextField("Write here", 30);
			  add(jtf);
			  String text1 = jtf.getText();
			  JLabel text = new JLabel(text1);
			  add(text);

 		  }
	  }

 class Panel6 extends JPanel{
 		  Panel6(){
			  JSlider slider = new JSlider();
			  slider.setBorder(BorderFactory.createTitledBorder  ("JSlider with Tick Marks & Labels"));
			  slider.setMajorTickSpacing(20);
			  slider.setMinorTickSpacing(5);
			  slider.setPaintTicks(true);
			  slider.setPaintLabels(true);
			  add(slider);
 		  }
 		  */




I want to add in one of the tabs this class,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter extends JApplet {

   JComboBox colorChoice;
   JComboBox figureChoice;

   public void init() {

      setBackground(Color.gray);
      getContentPane().setBackground(Color.gray);

      Display canvas = new Display();
      getContentPane().add(canvas,BorderLayout.CENTER);

      JPanel buttonBar = new JPanel();
      buttonBar.setBackground(Color.gray);
      getContentPane().add(buttonBar, BorderLayout.SOUTH);

      JPanel choiceBar = new JPanel();
      choiceBar.setBackground(Color.gray);
      getContentPane().add(choiceBar, BorderLayout.NORTH);

      JButton fill = new JButton("Set Background");
      fill.addActionListener(canvas);
      buttonBar.add(fill);

      JButton clear = new JButton("Clear");
      clear.addActionListener(canvas);
      buttonBar.add(clear);

      colorChoice = new JComboBox();
      colorChoice.addItem("Black");
      colorChoice.addItem("Red");
      colorChoice.addItem("Green");
      colorChoice.addItem("Blue");
      colorChoice.addItem("Cyan");
      colorChoice.addItem("Magenta");
      colorChoice.addItem("Yellow");
      colorChoice.addItem("White");
      colorChoice.setBackground(Color.white);
      choiceBar.add(colorChoice);

      figureChoice = new JComboBox();
      figureChoice.addItem("Curve");
      figureChoice.addItem("Straight Line");
      figureChoice.addItem("Rectangle");
      figureChoice.addItem("Oval");
      figureChoice.addItem("RoundRect");
      figureChoice.addItem("Filled Rectangle");
      figureChoice.addItem("Filled Oval");
      figureChoice.addItem("Filled RoundRect");
      figureChoice.setBackground(Color.white);
      choiceBar.add(figureChoice);

   }

   private class Display extends JPanel
              implements MouseListener, MouseMotionListener, ActionListener {

      private final static int BLACK = 0;
      private final static int RED = 1;
      private final static int GREEN = 2;
      private final static int BLUE = 3;
      private final static int CYAN = 4;
      private final static int MAGENTA = 5;
      private final static int YELLOW = 6;
      private final static int WHITE = 7;

      private final static int CURVE = 0;
      private final static int LINE = 1;
      private final static int RECT = 2;
      private final static int OVAL = 3;
      private final static int ROUNDRECT = 4;
      private final static int FILLED_RECT = 5;
      private final static int FILLED_OVAL = 6;
      private final static int FILLED_ROUNDRECT = 7;

      Image OSI;

      int widthOfOSI, heightOfOSI;

      private int mouseX, mouseY;   // The location of the mouse.

      private int prevX, prevY;     // The previous location of the mouse.

      private int startX, startY;   // The starting position of the mouse.
                                    // (Not used for drawing curves.)

      private boolean dragging;     // This is set to true when the user is drawing.

      private int figure;    // What type of figure is being drawn.  This is
                             //    specified by the figureChoice menu.

      private Graphics dragGraphics;  // A graphics context for the off-screen image,
                                      // to be used while a drag is in progress.

      private Color dragColor;  // The color that is used for the figure that is
                                // being drawn.

      Display() {
         addMouseListener(this);
         addMouseMotionListener(this);
         setBackground(Color.white);
      }
      private void drawFigure(Graphics g, int shape, int x1, int y1, int x2, int y2) {
         if (shape == LINE) {
            g.drawLine(x1,y1,x2,y2);
            return;
         }
         int x, y;
         int w, h;
         if (x1 >= x2) {
            x = x2;
            w = x1 - x2;
         }
         else {
            x = x1;
            w = x2 - x1;
         }
         if (y1 >= y2) {
            y = y2;
            h = y1 - y2;
         }
         else {
            y = y1;
            h = y2 - y1;
         }
         switch (shape) {
            case RECT:
               g.drawRect(x, y, w, h);
               break;
            case OVAL:
               g.drawOval(x, y, w, h);
               break;
            case ROUNDRECT:
               g.drawRoundRect(x, y, w, h, 20, 20);
               break;
            case FILLED_RECT:
               g.fillRect(x, y, w, h);
               break;
            case FILLED_OVAL:
               g.fillOval(x, y, w, h);
               break;
            case FILLED_ROUNDRECT:
               g.fillRoundRect(x, y, w, h, 20, 20);
               break;
         }
      }

      private void repaintRect(int x1, int y1, int x2, int y2) {
            // Call repaint on a rectangle that contains the points (x1,y1)
            // and (x2,y2).  (Add a 1-pixel border along right and bottom
            // edges to allow for the pen overhang when drawing a line.)
         int x, y;  // top left corner of rectangle that contains the figure
         int w, h;  // width and height of rectangle that contains the figure
         if (x2 >= x1) {
            x = x1;
            w = x2 - x1;
         }
         else {
            x = x2;
            w = x1 - x2;
         }
         if (y2 >= y1) {
            y = y1;
            h = y2 - y1;
         }
         else {                y = y2;
            h = y1 - y2;
         }
         repaint(x,y,w+1,h+1);
      }


      private void checkOSI() {
           // This method is responsible for creating the off-screen image.
           // It should be called before using the OSI.  It will make a new OSI if
           // the size of the panel changes.
         if (OSI == null || widthOfOSI != getSize().width || heightOfOSI != getSize().height) {
                // Create the OSI, or make a new one if panel size has changed.
            OSI = null;  // (If OSI already exists, this frees up the memory.)
            OSI = createImage(getSize().width, getSize().height);
            widthOfOSI = getSize().width;
            heightOfOSI = getSize().height;
            Graphics OSG = OSI.getGraphics();  // Graphics context for drawing to OSI.
            OSG.setColor(getBackground());
            OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
            OSG.dispose();
         }
      }


      public void paintComponent(Graphics g) {
           // Copy the off-screen image to the screen,
           // after checking to make sure it exists.  Then,
           // if a shape other than CURVE is being drawn,
           // draw it on top of the image from the OSI.
         checkOSI();
         g.drawImage(OSI, 0, 0, this);
         if (dragging && figure != CURVE) {
            g.setColor(dragColor);
            drawFigure(g,figure,startX,startY,mouseX,mouseY);
         }
      }


      public void actionPerformed(ActionEvent evt) {
              // Respond when the user clicks on a button.  The
              // command must be either "Clear" or "Set Background".
         String command = evt.getActionCommand();
         checkOSI();
         if (command.equals("Set Background")) {
                // Set background color before clearing.
                // Change the selected color so it is different
                // from the background color.
            setBackground(getCurrentColor());
            if (colorChoice.getSelectedIndex() == BLACK)
               colorChoice.setSelectedIndex(WHITE);
            else
               colorChoice.setSelectedIndex(BLACK);
         }
         Graphics g = OSI.getGraphics();
         g.setColor(getBackground());
         g.fillRect(0,0,getSize().width,getSize().height);
         g.dispose();
         repaint();
      }


      private Color getCurrentColor() {
               // Check the colorChoice menu to find the currently
               // selected color, and return the appropriate color
               // object.
         int currentColor = colorChoice.getSelectedIndex();
         switch (currentColor) {
            case BLACK:
               return Color.black;
            case RED:
               return Color.red;
            case GREEN:
               return Color.green;
            case BLUE:
               return Color.blue;
            case CYAN:
               return Color.cyan;
            case MAGENTA:
               return Color.magenta;
            case YELLOW:
               return Color.yellow;
            default:
               return Color.white;
         }
      }


      public void mousePressed(MouseEvent evt) {
              // This is called when the user presses the mouse on the
              // panel.  This begins a draw operation in which the user
              // sketches a curve or draws a shape.  (Note that curves
              // are handled differently from other shapes.  For CURVE,
              // a new segment of the curve is drawn each time the user
              // moves the mouse.  For the other shapes, a "rubber band
              // cursor" is used.  That is, the figure is drawn between
              // the starting point and the current mouse location.)

         if (dragging == true)  // Ignore mouse presses that occur
             return;            //    when user is already drawing a curve.
                                //    (This can happen if the user presses
                                //    two mouse buttons at the same time.)

         prevX = startX = evt.getX();  // Save mouse coordinates.
         prevY = startY = evt.getY();

         figure = figureChoice.getSelectedIndex();
         dragColor = getCurrentColor();
         dragGraphics = OSI.getGraphics();
         dragGraphics.setColor(dragColor);

         dragging = true;  // Start drawing.

      } // end mousePressed()


      public void mouseReleased(MouseEvent evt) {
              // Called whenever the user releases the mouse button.
              // If the user was drawing a shape, we make the shape
              // permanent by drawing it to the off-screen image.
          if (dragging == false)
             return;  // Nothing to do because the user isn't drawing.
          dragging = false;
          mouseX = evt.getX();
          mouseY = evt.getY();
          if (figure == CURVE) {
                 // A CURVE is drawn as a series of LINEs
              drawFigure(dragGraphics,LINE,prevX,prevY,mouseX,mouseY);
              repaintRect(prevX,prevY,mouseX,mouseY);
          }
          else if (figure == LINE) {
             repaintRect(startX,startY,prevX,prevY);
             if (mouseX != startX || mouseY != startY) {
                   // Draw the line only if it has non-zero length.
                drawFigure(dragGraphics,figure,startX,startY,mouseX,mouseY);
                repaintRect(startX,startY,mouseX,mouseY);
             }
          }
          else {
             repaintRect(startX,startY,prevX,prevY);
             if (mouseX != startX && mouseY != startY) {
                   // Draw the shape only if both its height
                   // and width are both non-zero.
                drawFigure(dragGraphics,figure,startX,startY,mouseX,mouseY);
                repaintRect(startX,startY,mouseX,mouseY);
             }
          }
          dragGraphics.dispose();
          dragGraphics = null;
      }


      public void mouseDragged(MouseEvent evt) {
               // Called whenever the user moves the mouse while a mouse button
               // is down.  If the user is drawing a curve, draw a segment of
               // the curve on the off-screen image, and repaint the part
               // of the panel that contains the new line segment.  Otherwise,
               // just call repaint and let paintComponent() draw the shape on
               // top of the picture in the off-screen image.

          if (dragging == false)
             return;  // Nothing to do because the user isn't drawing.

          mouseX = evt.getX();   // x-coordinate of mouse.
          mouseY = evt.getY();   // y=coordinate of mouse.

          if (figure == CURVE) {
                 // A CURVE is drawn as a series of LINEs.
             drawFigure(dragGraphics,LINE,prevX,prevY,mouseX,mouseY);
             repaintRect(prevX,prevY,mouseX,mouseY);
          }
          else {
                // Repaint two rectangles:  The one that contains the previous
                // version of the figure, and the one that will contain the
                // new version.  The first repaint is necessary to restore
                // the picture from the off-screen image in that rectangle.
             repaintRect(startX,startY,prevX,prevY);
             repaintRect(startX,startY,mouseX,mouseY);
          }

          prevX = mouseX;  // Save coords for the next call to mouseDragged or mouseReleased.
          prevY = mouseY;

      } // end mouseDragged.


      public void mouseEntered(MouseEvent evt) { }   // Some empty routines.
      public void mouseExited(MouseEvent evt) { }    //    (Required by the MouseListener
      public void mouseClicked(MouseEvent evt) { }   //    and MouseMotionListener
      public void mouseMoved(MouseEvent evt) { }     //    interfaces).


   } // end nested class Display


} // end class SimplePaint3


how can i do it? PLEASEEE HELP ASAP :) thankss

Is This A Good Question/Topic? 0

Replies To: JAVA CLASSES

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JAVA CLASSES

Posted 28 April 2010 - 09:41 PM

You can add any JComponent (if not simply Component) to a JTabbedPane
so what is your problem >
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1