Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    I have the following layout...

    Capture.jpg

    What I would like to do ideally is, when a user clicks on a JLabel in the scrolling panel on the bottom right, attach a different version of the image (one without the costs and name) to the mouse cursor. Then when the user clicks inside the giant floor plan panel, it places a copy of a JLabel using the swapped image each time they click. When another label is selected the same thing happens. When they push the selection button (Hand Icon) or press ESC the label is unselected.

    I don't have much experience at all with GUIs in general so it's a wonder I got this far. Has anyone ever worked on something like this? Any suggestions how to make it work?

    Here is the code I currently have working....

    // =================================================================
    //  Restaurant Builder - Interface
    //  CSC - 540
    //  10/22/13
    //  Kris Purdy, Chris Fletemier
    // =================================================================
     
    package layout.manager;
     
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
     
    public class Interface extends JFrame implements ActionListener
    {
        // Class variables
        private static final int SCREEN_WIDTH = 1366;
        private static final int SCREEN_HEIGHT = 700;
        private static final int FLOORPLAN_WIDTH = 1150;
        private static final int FLOORPLAN_HEIGHT = SCREEN_HEIGHT;
        private static final int BUTTONS_WIDTH = SCREEN_WIDTH - FLOORPLAN_WIDTH;
        private static final int BUTTONS_HEIGHT = 280;
        private static final int OBJECTLIST_WIDTH = BUTTONS_WIDTH;
        private static final int OBJECTLIST_HEIGHT = SCREEN_HEIGHT - BUTTONS_HEIGHT;
        private static final int BTN_WIDTH = 50;
        private static final int BTN_HEIGHT = 50;
        private static final int LABELS_WIDTH = 180;
        private static final int LABELS_HEIGHT = 50;
        private static final int OBJECT_WIDTH = 180;
        private static final int OBJECT_HEIGHT = 225;
     
        private final Floorplan floorplan;
        private Estimate estimate;
     
        private final ImageIcon leftRotateImg;
        private final ImageIcon rightRotateImg;
        private final ImageIcon upImg;
        private final ImageIcon downImg;
        private final ImageIcon leftImg;
        private final ImageIcon rightImg;
        private final ImageIcon costImg;
        private final ImageIcon deleteImg;
        private final ImageIcon selectionImg;
        private final ImageIcon clearLayoutImg;
        private final ImageIcon hostessPodiumImg;
        private final ImageIcon roundTableImg;
        private final ImageIcon squareTableImg;
        private final ImageIcon rectangularTableImg;
        private final ImageIcon tableForTwoImg;
        private final ImageIcon boothImg;
        private final ImageIcon buffettBarImg;
        private final ImageIcon toiletPaperDispenserImg;
        private final ImageIcon toiletImg;
        private final ImageIcon towelDispenserImg;
        private final ImageIcon wasteBasketImg;
        private final ImageIcon bathroomSinkImg;
        private final ImageIcon ovenImg;
        private final ImageIcon rangeImg;
        private final ImageIcon counterTopImg;
        private final ImageIcon fridgeImg;
        private final ImageIcon freezerImg;
        private final ImageIcon kitchenSinkImg;
        private final ImageIcon dishwasherImg;
        private final ImageIcon kitchenImg;
        private final ImageIcon diningroomImg;
        private final ImageIcon bathroomImg;  
     
        private final JButton leftRotateBtn;
        private final JButton rightRotateBtn;
        private final JButton upBtn;
        private final JButton downBtn;
        private final JButton leftBtn;
        private final JButton rightBtn;
        private final JButton costBtn;
        private final JButton deleteBtn;
        private final JButton selectBtn;
        private final JButton clearLayoutBtn;
     
        private final JPanel buttons;
        private final JPanel objectlist;
        private final JPanel whitespace;
     
        private final JLabel kitchenLbl;
        private final JLabel diningroomLbl;
        private final JLabel bathroomLbl;
     
        private final RLabel hostessPodiumLbl;
        private final RLabel roundTableLbl;
        private final RLabel squareTableLbl;
        private final RLabel rectangularTableLbl;
        private final RLabel tableForTwoLbl;
        private final RLabel boothLbl;
        private final RLabel buffettBarLbl;
        private final RLabel toiletPaperDispenserLbl;
        private final RLabel toiletLbl;
        private final RLabel towelDispenserLbl;
        private final RLabel wasteBasketLbl;
        private final RLabel bathroomSinkLbl;
        private final RLabel ovenLbl;
        private final RLabel rangeLbl;
        private final RLabel counterTopLbl;
        private final RLabel fridgeLbl;
        private final RLabel freezerLbl;
        private final RLabel kitchenSinkLbl;
        private final RLabel dishwasherLbl;    
     
        private final JLabel recttabLbl;  // Can remove later
     
        private final JScrollPane scroll;
        private final RestaurantObject recttab;
        private ArrayList<RestaurantObject> list;
     
        // Default Constructor
        public Interface() throws IOException
        {
        //---------------------------------------------------------------MAIN WINDOW
     
            // Create and set up the main window
            setTitle("Restaurant Builder");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(null);
     
        //------------------------------------------------------------MOUSE LISTENER
     
            MouseListener ml = new MouseListener() {
                @Override
                public void mouseReleased(MouseEvent e) 
                {
                    // Empty
                }
     
                @Override
                public void mousePressed(MouseEvent e) 
                {
                    // Empty
                }
     
                @Override
                public void mouseExited(MouseEvent e) 
                {
                    // Empty
                }
     
                @Override
                public void mouseEntered(MouseEvent e) 
                {
                    // Empty
                }
     
                @Override
                public void mouseClicked(MouseEvent e) 
                {
                    removeFocus();
                    ((RLabel)e.getSource()).setBorder(BorderFactory.createLineBorder(Color.black, 3));
                    ((RLabel)e.getSource()).setFocus(true);
                }
            };
     
        //-----------------------------------------------------------FLOORPLAN PANEL
     
            // Create the floorplan panel
            floorplan = new Floorplan("resources/floorplanPic.png",
                    FLOORPLAN_WIDTH, FLOORPLAN_HEIGHT);
            floorplan.setBounds(0, 0, FLOORPLAN_WIDTH, FLOORPLAN_HEIGHT);
            floorplan.setLayout(null);
     
        // ---------------------------------------------------------------------TEST
     
            recttab = new RestaurantObject("RectangularTable");
     
            recttabLbl = new JLabel();
            ImageIcon testImg = new ImageIcon(recttab.getImgPath());
            recttabLbl.setIcon(testImg);
            recttabLbl.setBounds(500, 200, recttab.getImgWidth(), recttab.getImgHeight());
            recttabLbl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            //recttabLbl.setFocusable(true);
            //recttabLbl.setRequestFocusEnabled(true);
            //recttabLbl.grabFocus();
            floorplan.add(recttabLbl);
     
            ComponentMover cm = new ComponentMover();
            cm.registerComponent(recttabLbl);
            recttabLbl.addMouseListener(ml);
     
    //        recttabLbl.addMouseListener(new MouseAdapter(){
    //        @Override
    //        public void mousePressed(MouseEvent arg0) {
    //            recttabLbl.setBorder(BorderFactory.createLineBorder(Color.black));
    //        }
    //        @Override
    //        public void mouseReleased(MouseEvent arg0) {
    //            recttabLbl.setBorder(null);
    //        }
    //        });
     
     
        // -----------------------------------------------------------------END TEST
     
        //-------------------------------------------------------------BUTTONS PANEL
     
            // Create the buttons panel
            buttons = new JPanel();
            buttons.setLayout(null);
            buttons.setBackground(Color.LIGHT_GRAY);
            buttons.setBounds(FLOORPLAN_WIDTH, 0, BUTTONS_WIDTH, BUTTONS_HEIGHT);
     
            // Create images for the buttons
            leftRotateImg = new ImageIcon("RotateLeft.png");
            rightRotateImg = new ImageIcon("RotateRight.png");
            upImg = new ImageIcon("Up.png");
            downImg = new ImageIcon("Down.png");
            leftImg = new ImageIcon("Left.png");
            rightImg = new ImageIcon("Right.png");
            costImg = new ImageIcon("Cost.png");
            deleteImg = new ImageIcon("Delete.png");
            selectionImg = new ImageIcon("Selection.png");
            clearLayoutImg = new ImageIcon("ClearLayout.png");
     
            // Create the buttons
            leftRotateBtn = new JButton(leftRotateImg);
            leftRotateBtn.setToolTipText("Rotate to the left.");
            leftRotateBtn.addActionListener(this);
            rightRotateBtn = new JButton(rightRotateImg);
            rightRotateBtn.setToolTipText("Rotate to the Right.");
            rightRotateBtn.addActionListener(this);
            upBtn = new JButton(upImg);
            upBtn.setToolTipText("Move the object up.");
            upBtn.addActionListener(this);
            downBtn = new JButton(downImg);
            downBtn.setToolTipText("Move the object down.");
            downBtn.addActionListener(this);
            leftBtn = new JButton(leftImg);
            leftBtn.setToolTipText("Move the object to the left.");
            leftBtn.addActionListener(this);
            rightBtn = new JButton(rightImg);
            rightBtn.setToolTipText("Move the object to the right.");
            rightBtn.addActionListener(this);
            costBtn = new JButton(costImg);
            costBtn.setToolTipText("Show the current costs for the materials "
                    + "and labor.");
            costBtn.addActionListener(this);
            deleteBtn = new JButton(deleteImg);
            deleteBtn.setToolTipText("Delete the object.");
            deleteBtn.addActionListener(this);
            selectBtn = new JButton(selectionImg);
            selectBtn.setToolTipText("Deselects and changes to the select cursor.");
            selectBtn.addActionListener(this);
            clearLayoutBtn = new JButton(clearLayoutImg);
            clearLayoutBtn.setToolTipText("Removes all objects from the layout.");
            clearLayoutBtn.addActionListener(this);
     
            // Set the location and size of the buttons
            leftRotateBtn.setBounds(25, 25, BTN_WIDTH, BTN_HEIGHT);
            upBtn.setBounds(85, 25, BTN_WIDTH, BTN_HEIGHT);
            rightRotateBtn.setBounds(145, 25, BTN_WIDTH, BTN_HEIGHT);
            leftBtn.setBounds(25, 85, BTN_WIDTH, BTN_HEIGHT);
            costBtn.setBounds(85, 85, BTN_WIDTH, BTN_HEIGHT);
            rightBtn.setBounds(145, 85, BTN_WIDTH, BTN_HEIGHT);
            selectBtn.setBounds(25, 145, BTN_WIDTH, BTN_HEIGHT);
            downBtn.setBounds(85, 145, BTN_WIDTH, BTN_HEIGHT);
            deleteBtn.setBounds(145, 145, BTN_WIDTH, BTN_HEIGHT);
            clearLayoutBtn.setBounds(25, 205, BTN_WIDTH * 3 + 20, BTN_HEIGHT);
     
            // Add the buttons to the panel
            buttons.add(leftRotateBtn);
            buttons.add(upBtn);
            buttons.add(rightRotateBtn);
            buttons.add(leftBtn);
            buttons.add(costBtn);
            buttons.add(rightBtn);
            buttons.add(selectBtn);
            buttons.add(downBtn);
            buttons.add(deleteBtn);
            buttons.add(clearLayoutBtn);
     
        //----------------------------------------------------------OBJECTLIST PANEL
     
            // Create objectlist panel
            objectlist = new JPanel();
            objectlist.setBackground(Color.WHITE);
            objectlist.setLayout(new BoxLayout(objectlist, BoxLayout.Y_AXIS));
     
            // Create labels to store images of the objects
            hostessPodiumLbl = new RLabel();
            hostessPodiumLbl.addMouseListener(ml);        
            hostessPodiumLbl.setName("hostessPodiumLbl");
            roundTableLbl = new RLabel();
            roundTableLbl.addMouseListener(ml);
            roundTableLbl.setName("roundTableLbl");
            squareTableLbl = new RLabel();
            squareTableLbl.addMouseListener(ml);
            squareTableLbl.setName("squareTableLbl");
            rectangularTableLbl = new RLabel();
            rectangularTableLbl.addMouseListener(ml);
            rectangularTableLbl.setName("rectangularTableLbl");
            tableForTwoLbl = new RLabel();
            tableForTwoLbl.addMouseListener(ml);
            tableForTwoLbl.setName("tableForTwoLbl");
            boothLbl = new RLabel();
            boothLbl.addMouseListener(ml);
            boothLbl.setName("boothLbl");
            buffettBarLbl = new RLabel();
            buffettBarLbl.addMouseListener(ml);
            buffettBarLbl.setName("buffettBarLbl");
            toiletPaperDispenserLbl = new RLabel();
            toiletPaperDispenserLbl.addMouseListener(ml);
            toiletPaperDispenserLbl.setName("toiletPaperDispenserLbl");
            toiletLbl = new RLabel();
            toiletLbl.addMouseListener(ml);
            toiletLbl.setName("toiletLbl");
            towelDispenserLbl = new RLabel();
            towelDispenserLbl.addMouseListener(ml);
            towelDispenserLbl.setName("towelDispenserLbl");
            wasteBasketLbl = new RLabel();
            wasteBasketLbl.addMouseListener(ml);
            wasteBasketLbl.setName("wasteBasketLbl");
            bathroomSinkLbl = new RLabel();
            bathroomSinkLbl.addMouseListener(ml);
            bathroomSinkLbl.setName("bathroomSinkLbl");
            ovenLbl = new RLabel();
            ovenLbl.addMouseListener(ml);
            ovenLbl.setName("ovenLbl");
            rangeLbl = new RLabel();
            rangeLbl.addMouseListener(ml);
            rangeLbl.setName("rangeLbl");
            counterTopLbl = new RLabel();
            counterTopLbl.addMouseListener(ml);
            counterTopLbl.setName("counterTopLbl");
            fridgeLbl = new RLabel();
            fridgeLbl.addMouseListener(ml);
            fridgeLbl.setName("fridgeLbl");
            freezerLbl = new RLabel();
            freezerLbl.addMouseListener(ml);
            freezerLbl.setName("freezerLbl");
            kitchenSinkLbl = new RLabel();
            kitchenSinkLbl.addMouseListener(ml);
            kitchenSinkLbl.setName("kitchenSinkLbl");
            dishwasherLbl = new RLabel();
            dishwasherLbl.addMouseListener(ml);
            dishwasherLbl.setName("dishwasherLbl");
     
            kitchenLbl = new JLabel();
            diningroomLbl = new JLabel();
            bathroomLbl = new JLabel();
     
            // Load the images for the objects
            hostessPodiumImg = new ImageIcon("scrlHostessPodium.png");
            roundTableImg = new ImageIcon("scrlRoundTable.png");
            squareTableImg = new ImageIcon("scrlSquareTable.png");
            rectangularTableImg = new ImageIcon("scrlRectangularTable.png");
            tableForTwoImg = new ImageIcon("scrlTableForTwo.png");
            boothImg = new ImageIcon("scrlBooth.png");
            buffettBarImg = new ImageIcon("scrlBuffettBar.png");
            toiletPaperDispenserImg = new ImageIcon("scrlToiletPaper.png");
            toiletImg = new ImageIcon("scrlToilet.png");
            towelDispenserImg = new ImageIcon("scrlTowelDispenser.png");
            wasteBasketImg = new ImageIcon("scrlWasteBasket.png");
            bathroomSinkImg = new ImageIcon("scrlBathroomSink.png");
            ovenImg = new ImageIcon("scrlOven.png");
            rangeImg = new ImageIcon("scrlRange.png");
            counterTopImg = new ImageIcon("scrlCountertop.png");
            fridgeImg = new ImageIcon("scrlFridge.png");
            freezerImg = new ImageIcon("scrlFreezer.png");
            kitchenSinkImg = new ImageIcon("scrlKitchenSink.png");
            dishwasherImg = new ImageIcon("scrlDishwasher.png");
            kitchenImg = new ImageIcon("Kitchen.png");
            diningroomImg = new ImageIcon("DiningRoom.png");
            bathroomImg = new ImageIcon("Bathroom.png");
     
            // Attach the images to the labels
            hostessPodiumLbl.setIcon(hostessPodiumImg);
            roundTableLbl.setIcon(roundTableImg);
            squareTableLbl.setIcon(squareTableImg);
            rectangularTableLbl.setIcon(rectangularTableImg);
            tableForTwoLbl.setIcon(tableForTwoImg);
            boothLbl.setIcon(boothImg);
            buffettBarLbl.setIcon(buffettBarImg);
            toiletPaperDispenserLbl.setIcon(toiletPaperDispenserImg);
            toiletLbl.setIcon(toiletImg);
            towelDispenserLbl.setIcon(towelDispenserImg);
            wasteBasketLbl.setIcon(wasteBasketImg);
            bathroomSinkLbl.setIcon(bathroomSinkImg);
            ovenLbl.setIcon(ovenImg);
            rangeLbl.setIcon(rangeImg);
            counterTopLbl.setIcon(counterTopImg);
            fridgeLbl.setIcon(fridgeImg);
            freezerLbl.setIcon(freezerImg);
            kitchenSinkLbl.setIcon(kitchenSinkImg);
            dishwasherLbl.setIcon(dishwasherImg);
     
            kitchenLbl.setIcon(kitchenImg);
            diningroomLbl.setIcon(diningroomImg);
            bathroomLbl.setIcon(bathroomImg);
     
            // Add all the images to the panel
            objectlist.add(diningroomLbl);
            objectlist.add(hostessPodiumLbl);
            objectlist.add(roundTableLbl);
            objectlist.add(squareTableLbl);
            objectlist.add(rectangularTableLbl);
            objectlist.add(boothLbl);
            objectlist.add(bathroomLbl);
            objectlist.add(toiletPaperDispenserLbl);
            objectlist.add(toiletLbl);
            objectlist.add(towelDispenserLbl);
            objectlist.add(wasteBasketLbl);
            objectlist.add(bathroomSinkLbl);
            objectlist.add(kitchenLbl);
            objectlist.add(ovenLbl);
            objectlist.add(rangeLbl);
            objectlist.add(counterTopLbl);
            objectlist.add(fridgeLbl);
            objectlist.add(freezerLbl);
            objectlist.add(kitchenSinkLbl);
            objectlist.add(dishwasherLbl);
     
            // Create a scroll bar for the objectlist panel
            scroll = new JScrollPane(objectlist);
            scroll.setBackground(Color.WHITE);
            scroll.setBorder(null);
            scroll.getVerticalScrollBar().setUnitIncrement(16);
            scroll.setBounds(FLOORPLAN_WIDTH + 25, BUTTONS_HEIGHT, 
                    OBJECTLIST_WIDTH - 25, OBJECTLIST_HEIGHT);
     
        //----------------------------------------------------------WHITESPACE PANEL 
     
            // Create a panel to add some white space
            whitespace = new JPanel();
            whitespace.setBounds(FLOORPLAN_WIDTH, BUTTONS_HEIGHT, 25, OBJECTLIST_HEIGHT);
            whitespace.setBackground(Color.WHITE);
     
        //---------------------------------------------------------------MAIN WINDOW
     
            // Display the main window
            add(floorplan);
            add(buttons);
            add(scroll);
            add(whitespace);
            setExtendedState(Frame.MAXIMIZED_BOTH);  
            setVisible(true); 
            setResizable(false);
        }
     
        //-----------------------------------------------------------ACTION LISTENER
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            int x, y, imgW, imgH;
            x = recttab.getX();
            y = recttab.getY();
            imgW = recttab.getImgWidth();
            imgH = recttab.getImgHeight();
     
            if (e.getSource() == leftRotateBtn)
            {
                System.out.println("leftRotateBtn");
            }
            else if (e.getSource() == upBtn)
            {
                if (y > 5)
                {
                    y -= 5;
                    recttab.setY(y);
                    recttabLbl.setBounds(x, y, imgW, imgH);
                }
     
            }
            else if (e.getSource() == rightRotateBtn)
            {
                System.out.println("rightRotateBtn");
            }
            else if (e.getSource() == leftBtn)
            {
                if (x > 5)
                {
                    x -= 5;
                    recttab.setX(x);
                    recttabLbl.setBounds(x, y, imgW, imgH);
                }
            }
            else if (e.getSource() == costBtn)
            {
                estimate = new Estimate();
            }
            else if (e.getSource() == rightBtn)
            {
                if (x < (FLOORPLAN_WIDTH - recttab.getImgWidth() - 5))
                {
                    x += 5;
                    recttab.setX(x);
                    recttabLbl.setBounds(x, y, imgW, imgH);
                }
            }
            else if (e.getSource() == selectBtn)
            {
                removeFocus();
            }
            else if (e.getSource() == downBtn)
            {
                if (y < (SCREEN_HEIGHT - recttab.getImgHeight() - 5))
                {
                    y += 5;
                    recttab.setY(y);
                    recttabLbl.setBounds(x, y, imgW, imgH);
                }
            }
            else if (e.getSource() == deleteBtn)
            {
                System.out.println("deleteBtn");
            }
            else if (e.getSource() == clearLayoutBtn)
            {
                System.out.println("clearLayoutBtn");
            }    
        }
     
        // New label class to allow lables to have focus
        private class RLabel extends JLabel
        {
            private boolean focus;
            private String name;
     
            // Does the object have focus
            @Override
            public boolean hasFocus()
            {
                return this.focus;
            }
     
            // Set the focus
            public void setFocus(boolean f)
            {
                this.focus = f;
            }
     
            // Get the labels name
            @Override
            public String getName()
            {
                return this.name;
            }
     
            // Set the labels name
            @Override
            public void setName(String n)
            {
                this.name = n;
            }
        }
     
        // Removes focus from all labels
        private void removeFocus()
        {
            hostessPodiumLbl.setBorder(null);
            hostessPodiumLbl.setFocus(false);
            roundTableLbl.setBorder(null);
            roundTableLbl.setFocus(false);
            squareTableLbl.setBorder(null);
            squareTableLbl.setFocus(false);
            rectangularTableLbl.setBorder(null);
            rectangularTableLbl.setFocus(false);
            tableForTwoLbl.setBorder(null);
            tableForTwoLbl.setFocus(false);
            boothLbl.setBorder(null);
            boothLbl.setFocus(false);
            buffettBarLbl.setBorder(null);
            buffettBarLbl.setFocus(false);
            toiletPaperDispenserLbl.setBorder(null);
            toiletPaperDispenserLbl.setFocus(false);
            toiletLbl.setBorder(null);
            toiletLbl.setFocus(false);
            towelDispenserLbl.setBorder(null);
            towelDispenserLbl.setFocus(false);
            wasteBasketLbl.setBorder(null);
            wasteBasketLbl.setFocus(false);
            bathroomSinkLbl.setBorder(null);
            bathroomSinkLbl.setFocus(false);
            ovenLbl.setBorder(null);
            ovenLbl.setFocus(false);
            rangeLbl.setBorder(null);
            rangeLbl.setFocus(false);
            counterTopLbl.setBorder(null);
            counterTopLbl.setFocus(false);
            fridgeLbl.setBorder(null);
            fridgeLbl.setFocus(false);
            freezerLbl.setBorder(null);
            freezerLbl.setFocus(false);
            kitchenSinkLbl.setBorder(null);
            kitchenSinkLbl.setFocus(false);
            dishwasherLbl.setBorder(null);
            dishwasherLbl.setFocus(false);
     
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    I only glanced at your code, because there's a lot of it, and I primarily wanted to see if it was hand built. I don't see the typical fingerprints of a graphical GUI Builder, so I'll assume it's hand built by someone for now.

    You've described what needs to be done, and what you've described seems easy enough to program, especially considering what has been done so far, so I'm confused. What's the question? What part of what you've described don't you know how to do?

    Let's start with one thing you'd like to do, and that one thing should be at the top of the chain of events you've described. Describe the one thing you want help with, ask a specific question about it, and point to the piece of code you've already tried to modify to make that one thing happen. Describe what is or isn't happening and post any errors received.

    There's no way we'll be able to run this project to duplicate your results, because too many external resources are required, so you're going to have to either post code we can run that duplicates the problem (not likely) or be very descriptive of what you're seeing and need help with.

    Other thoughts: I wouldn't name a class Interface. What you've posted may be a reasonable form for a small group project or discussing in a forum, but I wouldn't want to modify or maintain a mass of code this large. The single ActionListener and MouseListener are going to become difficult to manage. I haven't completely thought it through, but I'm not sure how you'll get the single MouseListener declared as local to the Interface constructor to work. As indicated by the @Override errors, there are already problems with the design, and the MouseListener design may be the first thing that needs fixing in order for you to move forward.

    Whether we help or not, good luck with your project!

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    Thanks for the reply. I'll try and address all of your concerns. First of all, this is a class project that myself and a partner are working on that is due in about a week. We have had about 3 semesters of Java and none of it was about GUIs. We were not even taught how to use the debugger. The university forces us to learn this stuff on our own, so naturally this is going to produce terrible programming habits as you can tell. I have to google search what I need and use bits and pieces of code that I don't fully understand or code that may do what I need right then, but doesn't function properly overall with the rest of the code. So as far as the college goes, we are encouraged to seek outside help as the professor doesn't know how to do this herself.

    I coded this all by hand. We were not allowed to use a GUI builder for it (otherwise I would have did it in VB, yes we are forced to use Java). This is an Interface class, but they never teach you a line of code, which defeats the purpose of taking it to learn to build interfaces. There are other class files but I didn't include their code. Any "bad programming" you see in the code is a direct result of the college's "teach yourself" approach. I honestly don't have a clue what I am doing half the time. It's essentially like giving a child a pile of blocks of different shapes. I find one that looks like it might fit, place it in the stack and if it doesn't fall down then I repeat the process.

    As far as the naming goes, what is wrong with the name Interface? If you think that code is long, you should have seen our groups last project. I didn't code it, but it was thousands of lines long just for a 25 button point of sale. I don't see how you can avoid lengthy code when you are working with a GUI. There are so many elements that do different things that I don't think you can condense them down any further. On this, at least I tried to separate my classes into different files. The last guy in the group put almost all the classes in the same file.

    Right now this is where we stand. We had to modify a bunch of code, but we found on another forum a way of copying a label every time you click. It uses a MouseAdapter which wreaked a bit of havoc on our code and I had to comment out all the MouseListener stuff as well as a lot of other things because I don't know how these things work together. Apparently it didn't like my RLabel extension of the JLabel so I had to change everything back to JLabels. I also lost functionality of the labels having a border drawn on them when selected since that was a part of the MouseListener. What is working though is when you click on the first label and then click in the floorplan, it will copy that label every time. (Although it is placing the label below all other labels if they are stacked for some reason.) We also added some previous code we found to make the items drag-able within the panel.

    Here is a small bit of code that does part of the copying...

        public void deepCopy(JLabel label)throws Exception
        {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(label);
            oos.close();
        }
        public void pasteLabel(int x, int y)throws Exception
        {
            if (clickCount == 1)
            {
                bins = new ByteArrayInputStream(baos.toByteArray());
                ObjectInputStream oins = new ObjectInputStream(bins);
                JLabel obj = (JLabel)oins.readObject();
                ComponentMover cm = new ComponentMover();
                cm.registerComponent(obj);
                floorplan.add(obj);
                obj.setBounds(x,y,obj.getWidth(),obj.getHeight());
                //jlist.add(obj);   Will not add anything to list for some reason
                //System.out.println(jlist.toString());
            }
        }

    The first problem is we need to swap the image that it is copying from the label to a similar but different one. Some objects are small, others large, so when I created the image in that photo, I blew up some items, shrunk others, and then added the Name and Costs. When we copy the image, I need it to be swapped out for the real one that I have saved.

    My thoughts were to create ImageIcons for all the extra images we have, then initialize these with the file locations. Once we have all of them created, then I could add some code in right after where it sets the objects bounds. Maybe something like obj.setIcon( name here ). The only problem with that is I would need even more, as you put it, lengthy code to determine which label was selected, then match the appropriate image. Normally I would just extend the JLabel and add a field to store the second imageIcon and then I can easily reference it, but this would break the copying code as it doesn't like my custom RLabels.

    Another issue I ran into was in that code above, when I tried to create an ArrayList<JLabel> jlist and add the objs to it, it wouldn't add anything. I placed that System.out line there to see if there was anything in the array but there is nothing printing when I add a label. I also ran the debugger and when it gets past that line, I checked the jlist array and there was nothing being stored in it.

    I really need each JLabel to create an instance of our ResturantObject class so that we can keep track of the costs of items being placed. But in order to do that I would need a name from each JLabel and I think when it copies them, they have just null names.

    So to sum up this post, I really don't know how to approach things correctly because I don't have the knowledge or background, but are being graded as if I did regardless of that. I have a sense of how I want it to function, but I just don't know how to make it do it.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    Where to begin . . . ?

    Thanks for the background, but let's stick to the task at hand which is to finish this project. Everyone has a life story, but it isn't always relevant or useful.

    As I tried to communicate earlier, let's work one thing at a time. If I were to pick one thing from above, it would be:
    The first problem is we need to swap the image that it is copying from the label to a similar but different one. Some objects are small, others large, so when I created the image in that photo, I blew up some items, shrunk others, and then added the Name and Costs. When we copy the image, I need it to be swapped out for the real one that I have saved.
    I don't completely understand all of that, but I'll focus on the first sentence. To that I would suggest you create a new JLabel in the desired size - if it must change - and associate with it the required image of the appropriate size. As this approach seems obvious to me and within the grasp of someone who wrote the code you've posted, I must be missing something.

    I'm also seeing 2 other items to attack:
    Another issue I ran into was in that code above, when I tried to create an ArrayList<JLabel> jlist and add the objs to it, it wouldn't add anything.
    Show the code that demonstrates this problem. And then:
    I really need each JLabel to create an instance of our ResturantObject class so that we can keep track of the costs of items being placed. But in order to do that I would need a name from each JLabel and I think when it copies them, they have just null names.
    It's easy enough to assign a specific identifier to each JLabel. If you review the JLabel API, you'll see setName()/getName() that can be used to identify each JLabel.

    I honestly don't know if you'll find someone with the time and desire to help through this or will be able to stick with you until done, but don't give up.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    I honestly don't know if you'll find someone with the time and desire to help through this or will be able to stick with you until done, but don't give up.
    I will agree with that. For me, it's not a quick fix type of problem. I am notifying the department head of the college and see what his suggestions are as almost anyone not directly involved in the project will most likely not want to take the time to get involved. But on the other hand, I think once we can get the labels to copy properly and tell which one has focus, then we can get all the rest of the program working ourselves.

    So to try and clarify that first part. Here are two sample images I have.

    scrlToiletPaper.pngToiletPaper.png

    As you can see from the two files, they are not the same size or the exact same image. One has the text in it while the other doesn't. I could get around the text part by just splitting the image in two have having the text portion as a label itself that is not copyable. But the issue with the different sizes then becomes the problem.

    So I think I can do what you are suggesting by creating all the JLabels with the actual image as I did in the image from the first post. That table is just a sample JLabel I made to test with.

    I guess my confusion comes in with how to take in the label selected and based on that, choose the correct new label. I see this line here ...

    (JLabel)oins.readObject()
    ... and I assume that I can use that in some way to tell which label was selected. But I don't understand what this Byte Array Input/Output stream is, much less what it is doing. I also don't know what information is being stored in it or if I can even use it to access the label name clicked on.

    If I get that working, I assume that I would just create a select case structure to match a copy of a premade label (with correct image) with the one that was selected?

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    Just a quick update. We have the copy thing working now. I also am working on the swap image functionality. I can do it with obj.setIcon( ). Once I get that working it will just be a matter of getting the objects stored in an array with or either as RestaurantObject objects.

    After that creating the code for the buttons should be fairly easy. If I run into any other smaller issues I will post them back here. Thanks for the help!

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    Ok I am including a zip copy of all my code and images. You can just open the project in Netbeans. (I assume it will work in Eclipse or other IDEs.)

    http://www.filedropper.com/layoutmanager_1

    Everything works correctly now as I wanted it and we have an array that stores counters for all the items added. The problem I currently have is: When a user clicks on an object in the floor plan, they can drag it around just fine, but they can't select it. In my mouseclick event it should be getting a border drawn around it to show that it's been selected but it doesn't. I assume this is because it has been registered as a component mover object with that class and it's overriding the mouse click event.

    What is the best solution around this issue to fix the problem? I need to have the object have a border around it to show it's selected and it needs to grab focus so that the user can then use the buttons to manipulate the object.

    EDIT: Ok I moved the MyMouseAdapter and added that to the object I was creating each time. I can now click on the objects but because of the way I am doing my removeBorder method, it's not removing the border from the objects in the floor plan. It's also not changing focus to the object because I can still keep clicking to make copies of the last label selected from the scroll bar, even though it's not highlighted anymore.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Need Help With Copying a JLabel From One Panel To Another While Swapping Images

    In my mouse click event I now set a String variable to

    prevFocus = evt.getComponent().getName();
    This will store the current label that is clicked on so next time an item is clicked it will be the item with the previous focus. I then want to do something like...

    prevFocus.setBorder(null);
    ... but since this is a String variable I can't call the dot setBorder() method since it's not a component. How can I take the string stored in the variable and then call the method to remove it's border?

Similar Threads

  1. swapping integers OOP
    By iamgonge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2013, 06:19 AM
  2. Copying a File
    By ubiByte in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 19th, 2012, 06:55 PM
  3. swapping nodes in a linked list
    By ueg1990 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 10th, 2012, 02:37 PM
  4. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  5. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM