473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with typing on JPanel

8
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its added back in, i simply cannot type! HELP!!!!



Codes are as follow:


Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.lang.StringBuffer;
  5.  
  6.  
  7. public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, KeyListener
  8. {
  9.  
  10.     private Point oldPoint, currentPoint;
  11.     private String s;
  12.      private FontMetrics fm;
  13.      private JButton circBtn, rectBtn, lineBtn, eraseBtn, clearBtn, sendBtn;
  14.  
  15.     JPanel drawingArea = new JPanel();     
  16.     JPanel jp = new JPanel();     
  17.     TextField message = new TextField("Draw away! Click anywhere on canvas to type");
  18.      int option;
  19.  
  20.     int startx, starty;  // The location of the mouse when the dragging started.
  21.     int mousex, mousey;    // The location of the mouse during dragging.
  22.  
  23.     public void init() {
  24.  
  25.     //initialize the "old" point
  26.     oldPoint = new Point(0,0);
  27.  
  28.     this.setSize(new Dimension(600, 500));
  29.  
  30. }        
  31.  
  32.  
  33.  
  34. public static void main(String[] args) {
  35. new JPanels();
  36.  
  37.  
  38. }
  39.  
  40.  
  41.  
  42.  
  43. public JPanels() {
  44.  
  45.  
  46.     super("Welcome to Whiteboard!");
  47.  
  48.     Container content = getContentPane();
  49.     content.setBackground(Color.lightGray);
  50.  
  51.          circBtn = new JButton("Circle"); 
  52.         rectBtn = new JButton("Rectangle"); 
  53.         lineBtn = new JButton("Line"); 
  54.         eraseBtn = new JButton("Eraser"); 
  55.         clearBtn = new JButton("Clear"); 
  56.         sendBtn = new JButton("Send"); 
  57.  
  58.          jp.setLayout(new GridLayout(0,6));
  59.  
  60.          jp.add(circBtn,0);
  61.          jp.add(rectBtn,1);
  62.          jp.add(lineBtn,2);
  63.          jp.add(eraseBtn,3);
  64.          jp.add(clearBtn,4);
  65.          jp.add(sendBtn,5);
  66.  
  67.  
  68.     message.setBackground(Color.gray);
  69.     message.setEditable(false);
  70.  
  71.     drawingArea.setPreferredSize(new Dimension(500, 400));
  72.     drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
  73.     drawingArea.setBackground(Color.white);
  74.  
  75.  
  76.      content.add(jp, BorderLayout.NORTH);
  77.     content.add(drawingArea, BorderLayout.CENTER);
  78.     content.add(message, BorderLayout.SOUTH);
  79.  
  80.     pack();
  81.  
  82.         this.setVisible(true);
  83.         this.setResizable(true);
  84.  
  85.         drawingArea.addMouseListener(this);
  86.         drawingArea.addMouseMotionListener(this);
  87.  
  88.  
  89.         //ActionPerformed methods for the buttons on the side panel
  90.  
  91.          circBtn.setToolTipText("Draw a Circle"); 
  92.          circBtn.addActionListener(new ActionListener() { 
  93.              public void actionPerformed(ActionEvent e) 
  94.              { 
  95.                  circBtn_actionPerformed(e); 
  96.              } 
  97.  
  98.          }); 
  99.  
  100.          rectBtn.setToolTipText("Draw a Rectangle"); 
  101.          rectBtn.addActionListener(new ActionListener() { 
  102.              public void actionPerformed(ActionEvent e) 
  103.              { 
  104.                  rectBtn_actionPerformed(e); 
  105.              } 
  106.  
  107.          }); 
  108.  
  109.          lineBtn.setToolTipText("Draw a Line"); 
  110.          lineBtn.addActionListener(new ActionListener() { 
  111.              public void actionPerformed(ActionEvent e) 
  112.              { 
  113.                  lineBtn_actionPerformed(e); 
  114.              } 
  115.  
  116.          }); 
  117.  
  118.          eraseBtn.setToolTipText("Eraser"); 
  119.          eraseBtn.addActionListener(new ActionListener() { 
  120.              public void actionPerformed(ActionEvent e) 
  121.              { 
  122.                  eraseBtn_actionPerformed(e); 
  123.              } 
  124.  
  125.          }); 
  126.  
  127.          clearBtn.setToolTipText("Clear Canvas"); 
  128.          clearBtn.addActionListener(new ActionListener() { 
  129.              public void actionPerformed(ActionEvent e) 
  130.              { 
  131.                  clearBtn_actionPerformed(e); 
  132.              } 
  133.  
  134.          }); 
  135.  
  136.          sendBtn.setToolTipText("Send drawing to friend"); 
  137.          sendBtn.addActionListener(new ActionListener() { 
  138.              public void actionPerformed(ActionEvent e) 
  139.              { 
  140.                  sendBtn_actionPerformed(e); 
  141.              } 
  142.  
  143.          }); 
  144.  
  145.  
  146.  
  147. }
  148.  
  149.     private void circBtn_actionPerformed(ActionEvent e) 
  150.          { 
  151.              System.out.println("This is the Circle Button test"); 
  152.              // TODO: Add any handling code here 
  153.               option = 1;
  154.          } 
  155.  
  156.      private void rectBtn_actionPerformed(ActionEvent e) 
  157.          { 
  158.              System.out.println("This is the Rect Button test"); 
  159.              // TODO: Add any handling code here 
  160.               option = 2;
  161.          } 
  162.  
  163.      private void lineBtn_actionPerformed(ActionEvent e) 
  164.          { 
  165.              System.out.println("This is the Line Button test"); 
  166.              // TODO: Add any handling code here 
  167.               option = 3;
  168.          } 
  169.  
  170.      private void eraseBtn_actionPerformed(ActionEvent e) 
  171.          { 
  172.              System.out.println("This is the Erase Button test"); 
  173.              // TODO: Add any handling code here 
  174.               option = 4;
  175.          } 
  176.  
  177.      private void clearBtn_actionPerformed(ActionEvent e) 
  178.          { 
  179.              System.out.println("This is the Clear Button test"); 
  180.              // TODO: Add any handling code here 
  181.              drawingArea.repaint();
  182.              message.setText("Canvas cleared!");
  183.  
  184.          } 
  185.  
  186.      private void sendBtn_actionPerformed(ActionEvent e) 
  187.          { 
  188.              System.out.println("This is the Send Button test"); 
  189.              // TODO: Add any handling code here 
  190.  
  191.          } 
  192.  
  193.     //when the mouse is pressed, a point is formed
  194.     public void mousePressed(MouseEvent e){
  195.  
  196.         startx = mousex = e.getX();  // Save coords of mouse position.
  197.         starty = mousey = e.getY();
  198.  
  199.         oldPoint = e.getPoint();
  200.  
  201.         String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");    
  202.         System.out.println(msg);
  203.          message.setText(msg);
  204.  
  205.         s = "";
  206.  
  207.     }
  208.  
  209.     //mouseDragged will do the line drawing
  210.     public void mouseDragged(MouseEvent e){
  211.  
  212.         mousex = e.getX();
  213.         mousey = e.getY();
  214.         currentPoint = e.getPoint();
  215.  
  216.         String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
  217.         System.out.println(msg);
  218.         message.setText(msg);
  219.  
  220.          if(option == 1){
  221.  
  222.      //drawingArea.getGraphics().drawOval(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  223.         Graphics g = drawingArea.getGraphics();
  224.  
  225.         drawOval(g);    
  226.  
  227.  
  228.          }
  229.  
  230.         else if (option == 2){
  231.     //    drawingArea.getGraphics().drawRect(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  232.         Graphics g = drawingArea.getGraphics();
  233.         drawRect(g);    
  234.  
  235.         }         
  236.  
  237.          else if (option == 3){
  238.         drawingArea.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  239.  
  240.         } 
  241.  
  242.         else if (option == 4){
  243.  
  244.         Graphics g = drawingArea.getGraphics();
  245.         g.setColor(Color.white);
  246.         g.fillOval(oldPoint.x,oldPoint.y,20,20);
  247.  
  248.         }
  249.  
  250.         oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
  251.     }
  252.  
  253.  
  254.     public boolean keyDown(Event e, int key) {
  255.  
  256.         String s = String.valueOf((char)key);
  257.  
  258.         drawingArea.getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  259.         System.out.println("this is s: ");
  260.  
  261.            Font font = new Font("Arial", Font.BOLD, 12);
  262.         fm = getFontMetrics(font);
  263.  
  264.     return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  265. }
  266.  
  267.  
  268.     protected boolean record(int x, int y) {
  269.     oldPoint.x = x;
  270.     oldPoint.y = y;
  271.     return(true);
  272.  
  273.     }
  274.  
  275.     public void paint(Graphics g)
  276.     {
  277.  
  278.     } 
  279.  
  280.  
  281.     void drawRect(Graphics g) {
  282.  
  283.                int x, y;  // Top left corner of the rectangle.
  284.                int w, h;  // Width and height of the rectangle.
  285.                    // x,y,w,h must be computed from the coordinates
  286.                    // of the two corner points.
  287.                if (mousex > startx) {
  288.                   x = startx;
  289.                   w = mousex - startx;
  290.                }
  291.                else {
  292.                   x = mousex;
  293.                   w = startx - mousex;
  294.                }
  295.                if (mousey > starty) {
  296.                   y = starty;
  297.                   h = mousey - starty;
  298.                }
  299.                else {
  300.                   y = mousey;
  301.                   h = starty - mousey;
  302.                }
  303.                g.setColor(Color.white);
  304.                g.fillRect(x, y, w, h);
  305.                g.setColor(Color.black);
  306.                g.drawRect(x, y, w-1, h-1);
  307.             }
  308.        // end drawRect()
  309.  
  310.        void drawOval(Graphics g) {
  311.  
  312.                int x, y;  // Top left corner of the rectangle.
  313.                int w, h;  // Width and height of the rectangle.
  314.                    // x,y,w,h must be computed from the coordinates
  315.                    // of the two corner points.
  316.                if (mousex > startx) {
  317.                   x = startx;
  318.                   w = mousex - startx;
  319.                }
  320.                else {
  321.                   x = mousex;
  322.                   w = startx - mousex;
  323.                }
  324.                if (mousey > starty) {
  325.                   y = starty;
  326.                   h = mousey - starty;
  327.                }
  328.                else {
  329.                   y = mousey;
  330.                   h = starty - mousey;
  331.                }
  332.                g.setColor(Color.white);
  333.                g.fillOval(x, y, w, h);
  334.                g.setColor(Color.black);
  335.                g.drawOval(x, y, w-1, h-1);
  336.             }
  337.        // end drawRect()
  338.  
  339.  
  340.     //abstract methods promised by the MouseListener and MouseMotionListener interfaces
  341.     public void mouseEntered(MouseEvent e){}
  342.     public void mouseExited(MouseEvent e){}
  343.     public void mouseReleased(MouseEvent e){}
  344.     public void mouseClicked(MouseEvent e){}
  345.     public void mouseMoved(MouseEvent e){}
  346.     public void keyReleased(KeyEvent e){}
  347.     public void keyPressed(KeyEvent e){}
  348.  
  349.  
  350. }
Jun 17 '07 #1
1 2120
r035198x
13,262 8TB
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its added back in, i simply cannot type! HELP!!!!



Codes are as follow:


1.) You forgot the code tags
2.) There exists in Java a switch statement.
3.) What are you trying to achieve with this program? Typing is only possible on components that support typing.
Jun 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: pnolan | last post by:
Hello there, I'm brand new to Java and have. I'm taking my 2nd Java class at school and I'm pretty lost at this point. The main problem I'm having right now is I cannot get my code to execute....
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
1
by: Neverhood | last post by:
Hi folks, I'm trying to make a small game about being a drug dealer, with a 10x8 grid JPanel. On top of that i have another 10x8 grid JPanel as glassPane for the player icon. I have all the...
5
by: lost1 | last post by:
I am having trouble adding a result set to a scroll pane. no matter what I do using getText I get an error. Can someon give me a clue? import java.util.*; import java.sql.*; import java.awt.*;...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
1
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.