Eric
Eric

Reputation: 51

How to move an image in a window?

enter image description hereI'm having trouble figuring out how to move and image to a different place in the window. I read about BorderLayout but I'm not sure how to implement it. I want to put the car above the text area, so would I use BorderLayout.NORTH in some way?

b3.addActionListener(new ActionListener() {
        /**
         * Displays the arraylist.
         */
        public void actionPerformed(ActionEvent e) {

            if (cars.size()>0){

                ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
                StringBuilder sb = new StringBuilder();


                for(int i=0; i < cars.size(); i++) {
                    sb.append("Car " + (i+1) + ": " + cars.get(i) + "\n");
                }

                Font font = new Font("Times New Roman", Font.PLAIN, 14);
                JTextArea textArea = new JTextArea(sb.toString());
                JScrollPane scrollPane = new JScrollPane(textArea); 
                textArea.setFont(font);
                textArea.setForeground(Color.BLACK);
                textArea.setLineWrap(true);
                textArea.setEditable(false);
                textArea.setWrapStyleWord(true); 
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.setPreferredSize(new Dimension( 100, 125 ));
                JOptionPane.showMessageDialog(null, scrollPane, "Inventory", JOptionPane.PLAIN_MESS![enter image description here][2]AGE, icon);
            }
            else {
                JOptionPane.showMessageDialog(null, "No cars in inventory", "Error", JOptionPane.ERROR_MESSAGE);
            }


        }

    });

Upvotes: 1

Views: 90

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You state:

I'm having trouble figuring out how to move and image to a different place in the window.

You will want to tell us the details of your problem. Where is your image now? What do you want your GUI to look like? Details matter.

I read about BorderLayout but I'm not sure how to implement it.

What confuses you about the tutorial?

I wasnt to put the car above the textarea so would I use BorderLayout.NORTH in some way?

Generally you use the BorderLayout constants such as BorderLayout.NORTH when adding components to a BorderLayout-using container. By "adding" I mean call the container's add(...) method whereby you pass into this method first the component to be added to the container and then the constant telling the BorderLayout layout manager where you want it to be added.

e.g.,

JPanel container = new JPanel(new BorderLayout());
JLabel label = new JLabel("North Label");
container.add(label, BorderLayout.NORTH);

But again for the details you need to read the tutorial. Links:

Upvotes: 3

Tyler Marshall
Tyler Marshall

Reputation: 314

The border layout feature is what you want to use IF you want to position the picture in different sections. You can have things like CENTER, NORTH, SOUTH, WEST, EAST, etc.

Here's how you would implement it:

setLayout(new BorderLayout());
JPanel southPanel = new JPanel();
southPanel.add(PICTUREGOESHERE);
add(southPanel, BorderLayout.SOUTH);

First, you set the layout to Border Layout. Them you create another JPanel. You add your component to the JPanel. You add your JPanel to the Main JFrame.

Upvotes: 2

Related Questions