Results 1 to 3 of 3
  1. #1
    rasen58 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default How to use variable in adapter class

    I have this simple code and in my main class, I have a JLabel called output, that I want to change the text of when there is a mouse pressed event. Right now, I just made an instance of the main class again, but is it ok to do that? The JappFrame class is a JApplet, so can you make an instance of it?
    XML Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class DragMouseAdapter extends MouseAdapter
    {
    	JappFrame a = new JappFrame();
    	public void mousePressed(MouseEvent e)
    	{
    		JComponent c = (JComponent)e.getSource();
    		TransferHandler handler = c.getTransferHandler();
    		handler.exportAsDrag(c, e, TransferHandler.COPY);
    		
    		JappFrame.output.setText("bye");
    		
    	}
    }
    
    public class JappFrame extends JApplet
    {
    	static JLabel output;
    	public static JLabel label1;
    	public void init()
    	{
    		ImageIcon icon1 = new ImageIcon("lightFlash.jpg");
    		ImageIcon icon2 = new ImageIcon("mask on earth.jpg");
    		JPanel panel = new JPanel(new GridLayout(2,1));
    		JPanel panel1 = new JPanel(new BorderLayout());
    		JPanel panel2 = new JPanel(new BorderLayout());
    		
    		output = new JLabel("Hello");
    		
    		panel1.setBackground(Color.red);
    		panel2.setBackground(Color.white);
    		
    		label1 = new JLabel(icon1);
    		JLabel label2 = new JLabel(icon2);
    		
    		MouseListener listener = new DragMouseAdapter();
    		label1.addMouseListener(listener);
    		label2.addMouseListener(listener);
    		
    		label1.setTransferHandler(new TransferHandler("icon"));
    		label2.setTransferHandler(new TransferHandler("icon"));
    		
    		panel1.add(label1);
    		panel2.add(label2);
    		panel.add(panel1);
    		panel.add(panel2);
    		panel.add(output);
    		
    		
    		setLayout(new GridLayout(1, 2));
    		setSize(600, 640);
    		add(panel);
    		//setVisible(true);
    	}
    	
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: How to use variable in adapter class

    Quote Originally Posted by rasen58 View Post
    I just made an instance of the main class again, but is it ok to do that? The JappFrame class is a JApplet, so can you make an instance of it?
    (double) No!
    First, why do you create an instance of that if the label is static anyway? But you don`t need both (static + the instance), just pass a reference to the label to the constructor (you have to create one) of your DragMouseAdapter !

  3. #3
    rasen58 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: How to use variable in adapter class

    Oh! thank you! That makes sense

Similar Threads

  1. Replies: 1
    Last Post: 06-27-2012, 02:55 PM
  2. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  3. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  4. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  5. Replies: 5
    Last Post: 08-07-2009, 12:54 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
  •