May be silly mistake .... please help me

Hi Friends,
I have jframe which contains a toggle button, when toggle button is pressed, I am trying to add a JText Field dynamically when user drags mouse.
I am getting nullpointer exception.
Can some one please tell me how can I solve this problem ?
Below is two classes of my code
1. Main (contains JFrame)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DragTest4{
    public static void main(String[] args) {
        drawRect dr = new drawRect();
class drawRect extends JFrame
             implements MouseListener, MouseMotionListener, ActionListener {
    Container c;
    Draggable3 d0;
    int startX;
    int startY;
    boolean drawRect = false;
    int compId = 0;
    JPanel jp;
    drawRect() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setSize(new Dimension(400, 400));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = screenSize.width/2 - getSize().width/2;
        int y = screenSize.height/2 - getSize().height/2;
        setLocation(x, y);
        Container c = getContentPane();
        JToggleButton jtb = new JToggleButton("Draw");
        jtb.setLayout(null);
        jtb.setBounds(10,10,80,20);
        jtb.addActionListener(this);
        c.add(jtb);
        d0 = new Draggable3();
        c.add(d0, null);
        c.setBackground(Color.white);
        c.setLayout(null);
        c.addMouseListener(this);
        c.addMouseMotionListener(this);
        setVisible(true);
    public void actionPerformed(ActionEvent e) {
        drawRect = !(drawRect);
        if(drawRect == true)
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
        else
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    public void mouseDragged(MouseEvent e) {
        if(drawRect == true) {
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            //.setBounds(startX, startY, e.getX() - startX, e.getY() - startY);
            System.out.println("Object Resized CompId:" + compId);
        } else {
    public void mouseMoved(MouseEvent e) {
    public void mouseEntered(MouseEvent e) {
    public void mouseExited(MouseEvent e) {
    public void mousePressed(MouseEvent e) {
        if(drawRect == true) {
            startX = e.getX();
            startY = e.getY();
            System.out.println("Object Created CompId:" + compId);
    public void mouseReleased(MouseEvent e) {
        if(drawRect == true) {
            System.out.println("Object Added CompId:" + compId);
            c.add(new Draggable3());  //Here nullpointer exception occurs !!
    public void mouseClicked(MouseEvent e) {
}2. modified JTextField (which will be added dynamically)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
class Draggable3
    extends JTextField
    implements MouseListener, MouseMotionListener {
  private final JTextField p;
  private LineBorder normalBorder = new LineBorder(Color.darkGray);
  private LineBorder selectedBorder = new LineBorder(Color.blue, 2);
  private boolean selected = false;
  final private int NORMAL_MOVE = 0;
  final private int LEFT_MOVE = 1;
  final private int RIGHT_MOVE = 2;
  final private int TOP_MOVE = 3;
  final private int BOTTOM_MOVE = 4;
  final private int LB_MOVE = 5;
  final private int LT_MOVE = 6;
  final private int RB_MOVE = 7;
  final private int RT_MOVE = 8;
  final private int DRAG_MOVE = 9;
  private int startDragPointX;
  private int startDragPointY;
  private int fieldResize = NORMAL_MOVE;
  public Draggable3() {
    setDoubleBuffered(true);
    p = this;
    setLayout(new BorderLayout());
    setBackground(Color.white);
    setBounds(40, 40, 50, 20);
    setBorder(normalBorder);
    addMouseListener(this);
    addMouseMotionListener(this);
  public void mouseClicked(MouseEvent e) {
    if (this.selected == true) {
      this.selected = false;
      this.setBorder(this.normalBorder);
    else {
      this.selected = true;
      this.setBorder(this.selectedBorder);
    mouseCursor(e);
  public void mouseDragged(MouseEvent e) {
    if (this.selected == true) {
      switch (fieldResize) {
        case LEFT_MOVE:
          fieldLeftStrech(e);
          break;
        case RIGHT_MOVE:
          fieldRightStrech(e);
          break;
        case BOTTOM_MOVE:
          fieldBottomStrech(e);
          break;
        case TOP_MOVE:
          fieldTopStrech(e);
          break;
        case LB_MOVE:
          fieldLeftStrech(e);
          fieldBottomStrech(e);
          break;
        case LT_MOVE:
          fieldLeftStrech(e);
          fieldTopStrech(e);
          break;
        case RB_MOVE:
          fieldRightStrech(e);
          fieldBottomStrech(e);
          break;
        case RT_MOVE:
          fieldRightStrech(e);
          fieldTopStrech(e);
          break;
        case DRAG_MOVE:
          p.setBounds(p.getX() - (startDragPointX - e.getX()),
                      p.getY() - (startDragPointY - e.getY()), p.getWidth(),
                      p.getHeight());
          break;
        default:
          break;
  private void fieldLeftStrech(MouseEvent e) {
    p.setBounds(p.getX() + e.getX(), p.getY(),
                p.getWidth() - e.getX(), p.getHeight());
  private void fieldRightStrech(MouseEvent e) {
    p.setBounds(p.getX(), p.getY(),
                e.getX(), p.getHeight());
  private void fieldBottomStrech(MouseEvent e) {
    p.setBounds(p.getX(), p.getY(),
                p.getWidth(), e.getY());
  private void fieldTopStrech(MouseEvent e) {
    p.setBounds(p.getX(), p.getY() + e.getY(),
                p.getWidth(), p.getHeight() - e.getY());
  public void mouseEntered(MouseEvent e) {
  public void mouseExited(MouseEvent e) {
  public void mousePressed(MouseEvent e) {
    startDragPointX = e.getX();
    startDragPointY = e.getY();
  public void mouseReleased(MouseEvent e) {
  public void mouseMoved(MouseEvent e) {
    mouseCursor(e);
  private void mouseCursor(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int fieldStartX = this.getX();
    int fieldStartY = this.getY();
    int fieldEndX = this.getWidth();
    int fieldEndY = this.getHeight();
    if (this.selected == true) {
      if ( (x <= 5) && (y >= fieldEndY - 5)) {
        fieldResize = LB_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
      else if ( (x <= 5) && (y <= 5)) {
        fieldResize = LT_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
      else if ( (x >= fieldEndX - 5) && (y >= fieldEndY - 5)) {
        fieldResize = RB_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
      else if ( (x >= fieldEndX - 5) && (y <= 5)) {
        fieldResize = RT_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
      else if (x <= 5) {
        fieldResize = LEFT_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
      else if (x >= fieldEndX - 5) {
        fieldResize = RIGHT_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
      else if (y <= 5) {
        fieldResize = TOP_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
      else if (y >= fieldEndY - 5) {
        fieldResize = BOTTOM_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
      else {
        fieldResize = DRAG_MOVE;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    else {
      fieldResize = NORMAL_MOVE;
      this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

Hello,
I think you are trying to do the same thing that I am trying to do...an editor? Could send me your source code?
I am trying to drag-drop java components like jtextfield, jbuttons,...
but I could not make it works. And create elements dynamically too.
My email:[email protected]
Thanks,
Luiz Fernando

Similar Messages

  • I inserted a SD card on a DVD drive for a mistake, please help me

    I inserted a SD card on a DVD drive for a mistake, please help me

    iTunes Store: Invalid, inactive, or illegible codes

  • Hi,my iphone 5 is not getting charged,but i can connect to itunes in usb.but when i put for charging it is is saying this accessory may not be supported,please help

    my iphone5 is not getting charged,but if i connect to my computer via usb i can use itunes and other all functions.normally if i connect it is charging battery also,but now it is not charging .it is saying this accessory may not be supported and the cable iam using orginal apple cable, alsoiam loosing my battery percentage.please help me

    Hello beenafromgbr,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at:
    Troubleshooting iPhone, iPad, and iPod touch accessories.
    http://support.apple.com/kb/ts2634
    Best of luck,
    Mario

  • About listener mistake,please help me!!

    I installed oracle10g on RHEL4 successfuly(ship.db.lnx32.cpio).But I get errors when I execute lsnrctl:
    [oracle@biodb1 product]$ lsnrctl
    LSNRCTL for Linux: Version 10.1.0.3.0 - Production on 09-MAR-2006 15:01:56
    Copyright (c) 1991, 2004, Oracle. All rights reserved.
    Message 850 not found; No message file for product=network, facility=NL
    I checked the $ORACLE_HOME is alright,and the user's permission is enough.
    I am not sure how to fix them,please help me,thank you!

    Typing sqlplus and getting a response may simply mean that you have the PATH set up correctly.
    The listener also needs to find things like the listener.ora file, and for that it wants to have the ORACLE_HOME and ORACLE_SID.
    When I have problems, the first thing I do in RedHat or SuSE is:
    set | grep -i ORA
    and make sure I have reasonable entries for ORACLE_SID, ORACLE_HOME and PATH
    To set everything properly, I use Andreas' suggestion and enter
    . oraenv ... to use the /usr/local/bin/oraenv routine (/user/local/bin is hardcoded in my PATH)
    As a matter of fact, the .profile or .bash_profile for all the userids that are supposed to access or manage oracle have the lines
    export ORACLE_SID=orcl # change orcl to the default SID you want to use
    ORAENV_ASK=NO
    . oraenv
    ORAENV_ASK=
    just to make sure the SID, HOME and PATH are set properly. You would be surprised how many problems this resolves.

  • I UNPAIRED MY remote by mistake please help me

    hey guys i just unpaired my apple remote by mistake i think by holding play/pause button and the menu button for like 6 seconds and now i cant re- pair my apple remote with my ATV2 , when i click something on the remote the led light on the atv2 flashes twice and nothing happens, please help me , how do i re pair my apple remote?
    Message was edited by: lamonsasa

    If you unpair the remote, the Apple TV will (or should) respond to any appropriate remote, so the problem isn't that you unpaired (assuming that you actually did unpair).
    I have had this happen a few times with my Apple TV 2. After power-cycling the Apple TV, it responds to my remote (and then I can re-pair it so that it won't respond to other remotes).

  • Need help badly. project file wont open. saying damaged file or may contain outdated elements. please help

    project file wont open. damaged file or contains outdated elements message. i believe its an XML error but i dont know how to fix it. please help

    As a happy user of the Matrox RT.X2 for many years, and later an equally happy user of a Matrox MXO2, I can say from experience that it is highly unlikely that this problem has anything to do with the Matrox hardware!
    Can you open a new blank project, and save it?
    If you cannot import your faulty project into a new project, can you import the sequence from your faulty project?
    Have you tried clearing the Media Cache, clearing your preferences, etc. - all among the techniques used to resurrect a Premiere Pro project that has gone bad???

  • I deleted all my sync info, and I need it back I deleted it today by mistake please help me here.

    # I formatted my desktop
    # I install Firefox
    # I tried the Firefox sync option so I can restore my stuff
    # And it's asked me for a username & password, So I used my username and password but I stopped when it asked me for a recovery key that I don't have so I create a new one.
    # That when I lost all my bookmarks passwords... etc
    please help me here I need my stuff back ASAP

    Sorry, when you create a new Sync Key your data is erased from the Sync server and there are no backups made to the Sync server.

  • HT1711 I just bought a stupid nicki minaj album that I didn't wan't to buy and It cost me 15 dollars . Now all my money is GONE and I was hoping is there was ANYWAY I could get my money back, please! It was a stupid mistake, please help

    Well the title says it all, I just bought a stupid nicki minaj album that I didn't wan't to buy and It cost me 15 dollars >.< Now all my money is GONE and I was hoping is there was ANYWAY I could get my money back, please! It was a stupid mistake, please help

    Well, if you havn't been abusing a previous stupid mistake, try here:
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • X interface may not be working, please help

    Can you please help to troubleshoot program execution.
    I execute from telnet window:
    ./dbisql -c "uid=dba;pwd=sql;eng=iqclient" -nogui
    and it crashes with messages:
    Exception in thread "main" java.lang.InternalError: Can't
    connect to X11 wi
    ndow server using '192.168.1.65:0.0' as the value of the
    DISPLAY variable.

    Are you actually running a X server on the machine from where you telneted from and did you give permission for the remote machine to use its X server? I'd recommend you just use MobaXterm instead of telnet as it has its own build in X server and DISPLAY forwarding and Xauthority permissions are all done for you automatically.

  • Big problem and very stupid mistake, please help me

    I was about to put leopard on my iBook g4 when on the read before installing thing it said if you want to start fresh delete your hard disk. I did that and the cd didn't work so now I'm stuck with nothing on my computer. Oh and by the way I don't understand why it didn't work, when I put the cd in and I restarted my computer it just made weird noises for a few hours and nothing happened. Thanks for helping!

    See if you can use the install disks that shipped with your iBook. If so, then again erase and try installing 10.5.
    As far as retrieving your data (if you have no backup) before you do another operation on that drive your best bet is Prosoft Data Rescue. There is a free trial to see if it can recover what you need. But don't do another operation on that drive until you've downloaded and run it. Also, a service that has been recommended but I cannot personally endorse is DriveSavers.
    -mj

  • Stupid mistake, please help

    i recently deleted quicktime to revert back to an older version and in doing so, i seem to have messed up my built in isight. it still works with imovie and photobooth but every time i try to open up video chat in ichat and skype, it says in ichat "camera is in use by another application" and in skype "no camera detected"
    anyone know how i can fix this problem?

    See if you can use the install disks that shipped with your iBook. If so, then again erase and try installing 10.5.
    As far as retrieving your data (if you have no backup) before you do another operation on that drive your best bet is Prosoft Data Rescue. There is a free trial to see if it can recover what you need. But don't do another operation on that drive until you've downloaded and run it. Also, a service that has been recommended but I cannot personally endorse is DriveSavers.
    -mj

  • One silly question, please help

    I am doing my assignment, but strange, my problem is ... the buttons doesn't work!
    here is the code :
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    public class addPanel extends JPanel implements ActionListener {
         private JLabel lblName, lblStreetNum, lblStreetName, lblSuburb, lblPostcode, lblTele, lblEmail, lblPackage;
         private JButton btnAdd, btnClear;
         private JRadioButton rbtnBasic, rbtnMovie, rbtnPlus;
         private JTextField txtName, txtStreetNum, txtStreetName, txtSuburb, txtTele, txtEmail, txtPostcode;
         private JTextArea display;
         private subscriberList subList;
         public addPanel() {
              subscriberList subList = new subscriberList("", "", "", "", "", "", "", "");
              setLayout(new BorderLayout());
              JPanel all = new JPanel(new FlowLayout());
              JLabel lblName = new JLabel("Name:");
              lblName.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblStreetNum = new JLabel("Street Number:");
              lblStreetNum.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblStreetName = new JLabel("Street Name:");
              lblStreetName.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblSuburb = new JLabel("Suburb:");
              lblSuburb.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblPostcode = new JLabel("Postcode:");
              lblPostcode.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblTele = new JLabel("Telephone:");
              lblTele.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblEmail = new JLabel("Email:");
              lblEmail.setPreferredSize(new java.awt.Dimension(100, 20));
              JLabel lblPackage = new JLabel("Package:");
              lblPackage.setPreferredSize(new java.awt.Dimension(100, 20));
              JTextField txtName = new JTextField("");
              txtName.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtStreetNum = new JTextField("");
              txtStreetNum.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtStreetName = new JTextField("");
              txtStreetName.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtSuburb = new JTextField("");
              txtSuburb.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtPostcode = new JTextField("");
              txtPostcode.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtTele = new JTextField("");
              txtTele.setPreferredSize(new java.awt.Dimension(300, 25));
              JTextField txtEmail = new JTextField("");
              txtEmail.setPreferredSize(new java.awt.Dimension(300, 25));
              JButton btnAdd = new JButton("Add");
              btnAdd.setPreferredSize(new java.awt.Dimension(90, 25));
              JButton btnClear = new JButton("Clear");
              btnClear.setPreferredSize(new java.awt.Dimension(90, 25));
              btnAdd.addActionListener(this);
              btnClear.addActionListener(this);
              JRadioButton rbtnBasic = new JRadioButton("Basic");
              rbtnBasic.setSelected(true);
              JRadioButton rbtnMovie = new JRadioButton("Movie Essentials");
              JRadioButton rbtnPlus = new JRadioButton("Entertainment Plus");
              ButtonGroup group = new ButtonGroup();
              group.add(rbtnBasic);
              group.add(rbtnMovie);
              group.add(rbtnPlus);
              JPanel rbtnPanel = new JPanel (new GridLayout(3,1));
              rbtnPanel.setPreferredSize(new java.awt.Dimension(300, 90));
              rbtnPanel.add(rbtnBasic); rbtnPanel.add(rbtnMovie); rbtnPanel.add(rbtnPlus);
              JTextArea display = new JTextArea( 6, 30);
              display.setPreferredSize(new java.awt.Dimension(300, 200));
              all.add(lblName); all.add(txtName);
              all.add(lblStreetNum); all.add(txtStreetNum);
              all.add(lblStreetName); all.add(txtStreetName);
              all.add(lblSuburb); all.add(txtSuburb);
              all.add(lblTele); all.add(txtTele);
              all.add(lblEmail); all.add(txtEmail);
              all.add(lblPostcode); all.add(txtPostcode);
              all.add(lblPackage); all.add(rbtnPanel);
              all.add(btnAdd); all.add(btnClear);
              all.add(display);
              add(all);
         public void actionPerformed (ActionEvent e) {
              String ac = e.getActionCommand();
              String packageType = "";
              if (rbtnBasic.isSelected()) { packageType = "Basic";}
              if (rbtnMovie.isSelected()) { packageType = "Movie Essentials";}
              if (rbtnPlus.isSelected()) { packageType = "Entertainment Plus";}
              // if "add" button is pressed
              if (ac == "Add") {
                   // check the user has enter the fields or not
                   if ( (txtName.getText().equals("")) ||
                   (txtStreetNum.getText().equals("")) ||
                   (txtStreetName.getText().equals("")) ||
                   (txtSuburb.getText().equals("")) ||
    (txtPostcode.getText().equals("")) ||
    (txtTele.getText().equals("")) ||
    (txtEmail.getText().equals("")) ) {
                        display.append("\nPlease fill in the fields in order to add a Subscriber!\n");
                   else {
                        subList.addSubscriber(txtName.getText() , txtStreetNum.getText(), txtStreetName.getText(), txtSuburb.getText(), txtPostcode.getText(), txtTele.getText(), txtEmail.getText(), packageType);
                        display.append("\nSubscriber successfully added!\n");
              if (ac == "Clear") {
                   txtName.setText(""); txtStreetNum.setText("");
                   txtStreetName.setText(""); txtSuburb.setText("");
                   txtTele.setText(""); txtEmail.setText(""); txtPostcode.setText("");
    the Add and Clear buttons don't work, can anyone tell me why?
    Thanks a lot!!

    You compare String references (pointers) when you should compare String content. Use equals or compareTo, like
    if (ac.equals("Clear")) {
    // or maybe this if ac can be null
    if ("Clear".equals(ac)) {

  • I am using 3.0.1 software in my iphone. may i eligible to update it and if yes how can i do the same? please help me.

    hello friends.
    i am using apple iphone model MB384LL version 3.0.1,am i able to update it with latest version,if yes how may i do that. please help me.
    thanks

    You will need to update through iTunes on your computer.

  • Iam getting the message firefox cannot be used coz i dont have usp10.dll.i use windows xp ..please help.

    dear folks
    After i downlaod and install firefox.. i get a message displayed ...as "this application has failed to start because usp10.dll was not found.Re-installing the application may fix this problem".Please help me fix this as i want to use firefox.I do have IE but with firefox it would be more comfortable.
    Thanks for the help rendered in advance
    Regards
    Sreenivaas

    I have a 5th generation 60GB iPod which I have been
    trying to update to software version 1.2 (from
    1.1.1.) but I keep getting the error message
    duplicated in the subject line of this post.
    I wouldn't mind to keep using V.1.1, except that I
    want to play a game I downçloaded from iTunes and
    which requires 1.2 to work.
    Any help?
    WK
      Windows XP  
    I have the exact same problem with mine only that it is 30GB and it has version 1.1.2 on it

  • Hello to everyone, I have a problem with my iPhone 5, the dynimic doesn't work... i didn't drop it and didn't contact with water... so should I do? please help me =(  P.S. may be I wrote with some mistakes, so I apologyze for it

    hello to everyone, I have a problem with my iPhone 5, the dynimic doesn't work... I didn't drop it and didn't contact with water...
    so what should I do? please help me =( 
    P.S. may be I wrote with some mistakes, so I apologyze for it

    Hello darkhanfromaktobe,
    Thank you for using Apple Support Communities.
    For more information, take a look at:
    iPhone: Microphone issues
    http://support.apple.com/kb/ts5183
    Have a nice day,
    Mario

Maybe you are looking for

  • List of unreleased POs with Net PO Price more than $10000

    Hi,    How can I check the list of unreleased POs having net price more than $10000. I tried using ME2N, but it gives result of the specific net price. I want to use range of net price. Thanks Suraj Edited by: Suraj Sharma on Jul 30, 2008 5:42 AM Edi

  • A3 UNABLE TO ORDER PRINTS

    I note from my earlier questions that NONE of the problems experienced with A3/10.6.3 etc have been resolved by Apple. I contacted Apple by phone and a very nice chap tells me they are looking at it. After the third time he told me I don't believe hi

  • IOS 8 problem in Spotlight

    In my ipad mini running newest ios 8, When iam slide down in homescreen, and then the display turn into grayscale. Why?

  • Send picture as sms

    hi to all, How can i send picture as sms in java.......i want to know its format ,size etc...i have jsms api......i dont know how to use them.......

  • BAPI_0050_CREATE How Use?

    Hi All, I'm using the BAPI_0050_CREATE and I don't obtaining to creat with the correct period.   CALL FUNCTION 'BAPI_0050_CREATE'     EXPORTING       header_data           = r_i_header       header_data_add    = r_i_header_add       testrun