Hi all, firstly thank you for reading, and hopefully helping me find an answer to this: currently ive got a layerpane, within which i need to add "x" amount of jlabels, where x is the number of times they select an option from a menu item, to further complicate the matter each label needs to be able to move independantly. currently i can get all the above working, apart from the dynamic part, each label is already defined and coded, but if the user then clicks too many times they still only get the number of labels that are coded, currently 1. i know the answer will be something really really simple, but after hours of googleing still cant seam to find an answer! thanks again!! ps, hope you can understand the above, couldn't think of a better way the write it! Have just added to code below, hopefully it makes sense since i've tried to edit it down from the 400+ lines to just those i think are needed! Code: package Dialogs; import java.awt.*; import javax.swing.*; import java.io.*; import java.awt.event.*; import java.awt.image.BufferedImage; public class EditorFrame extends EditorSkeletonFrame { private JLabel label3; private JLayeredPane layer; private int me1; private int me2; public EditorFrame() { layer = new javax.swing.JLayeredPane(); label3 = new javax.swing.JLabel(); label3.addMouseMotionListener(new java.awt.event.MouseMotionAdapter(){ public void mouseDragged(java.awt.event.MouseEvent evt){ label3MouseDragged(evt); } }); label3.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt){ label3MousePressed(evt); } }); //Add Layer to the Frame this.add(layer, BorderLayout.CENTER); JMenuBar menubar = new JMenuBar(); this.setJMenuBar(menubar); JMenu toolMenu = new JMenu("Tools"); toolMenu.setMnemonic(KeyEvent.VK_T); menubar.add(toolMenu); JMenu insertItem = new JMenu("Insert"); JMenuItem GItem = new JMenuItem("Access Point 802.11G"); toolMenu.add(insertItem); insertItem.add(GItem); EditorListener editorListener = new EditorListener(this); GItem.addActionListener(editorListener); } private void label3MouseDragged(java.awt.event.MouseEvent evt){ PointerInfo me = java.awt.MouseInfo.getPointerInfo(); int x = me.getLocation().x; int y = me.getLocation().y; x -= me1; y -= me2; label3.setLocation(x, y); } private void label3MousePressed(java.awt.event.MouseEvent evt){ int temp1 = label3.getX(); int temp2 = label3.getY(); PointerInfo me = java.awt.MouseInfo.getPointerInfo(); int x = me.getLocation().x; int y = me.getLocation().y; me1 = (x - temp1); me2 = (y - temp2); } @Override public void GItem(){ label3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Andrew\\Desktop\\circle1.png")); label3.setText("Modal Layer"); label3.setSize(label3.getPreferredSize()); label3.setVisible(true); label3.setLocation(100, 100); layer.add(label3, JLayeredPane.MODAL_LAYER); } } }
it depends when the x is selected for example, if it is selected before the layerpane is created, you can create an array of JLabels if the x is selected after the layerpane is created, you will probably need some Collection (e.g. ArrayList) of JLabels to add to whenever you create a new JLabel then whenever you paint/update your layerpane, just work through the array/collection making sure each is painting itself if you post your code, i'm sure I (or someone else) could be more helpful
that was my original plan, but i have no way of knowing how many the user will want to put on, so was hoping for some method of dynamically adding them ps. thanks for the reply!
A for loop like this perhaps? Code: for (int x = 0; x < args; ++x) { String label = "label"; label += Integer.parseInt(args); JLabel label = new JLabel(); } I'm not sure if it is possible to use a variable for naming object instances. Edit: You're welcome, judging by your code you're a far more advanced programmer than myself and i'm just bouncing ideas to you here so i'm not sure how much help i am/can be.
OK... so assuming that some other class will call some function in this class "addLabel" i suggest you have an ArrayList of labels heres some code alterations i would do (its not in full proper java..but you should get the jist) Code: //first change your mouse event functions to take a label and an event //(alternatively you could set up the below to use an index) private void labelMouseDragged(JLabel label, java.awt.event.MouseEvent evt){ //other stuff label.setLocation(x, y); } private void labelMousePressed(JLabel label, java.awt.event.MouseEvent evt){ int temp1 = label.getX(); int temp2 = label.getY(); //other stuff } //create a helper function to create a label private JLabel createLabel(){ JLabel label = new JLabel(); label.addMouseMotionListener(new java.awt.event.MouseMotionAdapter(){ public void mouseDragged(java.awt.event.MouseEvent evt){ labelMouseDragged(label, evt); } }); //and the other listener } //in the class you probably want to declare the ArrayList private ArrayList<JLabel> labels = new ArrayList<JLabel>(); //then you want a public function to actually add JLabels public void addLabelToFrame(){ JLabel newLabel = reateNewLabel(); labels.add( newLabel ) ; //maybe this is where you need to add it to the layer too layer.add(newLabel() ); //we need to tell swing to reorganise the layer (otherwise it might not show the label) layer.validate(); } //your constructor can then be altered to use the above... //im not sure what your GItem function is for... but that can easily be turned into a for loop as i mentioned depending on what things you are doing, it may be better to use another collection... or it may be better to use an indexes rather than actual JLabel objects Im not sure if the above will make sense, nor have I tried to run it... but let me know how you get on
Just an update: thanks to every reply, you have all helped: finally got the code to work with the following: Code: public void NItem(){ final JLabel label[] = new JLabel[10]; ImageIcon icon = new ImageIcon("F:\\JAVA\\Prog\\imgN.png"); for(final int i = 0; i < 10; i++){ label[i] = new JLabel(icon); layer.add(label[i], JLayeredPane.MODAL_LAYER); label[i].setSize(label[i].getPreferredSize()); label[i].setVisible(true); label[i].setLocation(0, 0); label[i].addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt){ labelMouseDragged(evt); } public void labelMouseDragged(MouseEvent evt) { PointerInfo me = java.awt.MouseInfo.getPointerInfo(); int x = me.getLocation().x; int y = me.getLocation().y; x -= me1; y -= me2; label[i].setLocation(x, y); } }); label[i].addMouseListener(new MouseAdapter() { public void mousePressed(MouseAdapter evt){ labelMousePressed(evt); } public void labelMousePressed(MouseAdapter evt) { int temp1 = label[i].getX(); int temp2 = label[i].getY(); PointerInfo me = java.awt.MouseInfo.getPointerInfo(); int x = me.getLocation().x; int y = me.getLocation().y; me1 = (x - temp1); me2 = (y - temp2); } }); } } although not the most efficent code it works, to a point, the new problem is that variable "i" needs to me made final (read somewhere its to do with a JAVA problem (not sure though), but then the "for" loop throws an error. the program works, but would be nice if it were with no errors, if someone can shed even a small glimmer of light it would be greatly recieved! Thanks!!
unless ive misunderstood the reason i needs to be final is because it's being accessed by your listeners/adapters (i.e. an anonymous inner class) the reason the for loop will error is simple, you set i to be 0, then at the end of the first loop it wants to increment i, but it cant because its final (i.e. constant) the way to get around this: for(int i = 0; i < 10; i++){ final int anotherI = i; //addListeners using 'anotherI' }