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.

Page 3 of 5 FirstFirst 12345 LastLast
Results 51 to 75 of 120

Thread: javadisplay images

  1. #51
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: javadisplay images

    Quote Originally Posted by stresstedout View Post
    are you able to show me a little example? also what is inital pack??? and where does this code go??
    Ok, try the following example (note: it's a bit compact/contrived, just to be short. Don't do this in "real" apps!).

    import java.awt.*;
    import javax.swing.*;
     
    public class GridLayoutTest {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("GridLayout test");
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     
                    ImageIcon icon = new ImageIcon("icon.jpg");
     
                    JPanel gridPanel = new JPanel(new GridLayout(2, 2, 6, 6));
                    gridPanel.setBackground(Color.YELLOW);
                    for (int i = 0; i < 4; i++) {
                        gridPanel.add(new JLabel(icon));
                    }
     
                    frame.getContentPane().add(gridPanel, BorderLayout.CENTER);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }
    Note: icon.gif must be in the current directory. Change it to what you want.

    Try to enlarge the frame. You will see that the gridPanel (highlighted in yellow) always fill the entire content pane. And the space between images can be large, depending on the frame size.

    Now, instead of:

    frame.getContentPane().add(gridPanel, BorderLayout.CENTER);

    put:

    JPanel flowPanel = new JPanel();
    flowPanel.add(gridPanel);
    frame.getContentPane().add(flowPanel, BorderLayout.CENTER);


    JPanel has, by default, a FlowLayout. FlowLayout respects the "preferred" size of components. See yourself the difference!
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  2. #52
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    please can someone help me

  3. #53
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    How can i get the image to go under the button
    Is that your question? Can you describe what that means? The posted image in #54 shows 4 columns of images in a rectangle with a grey rectangle beneath it. Is that what you want to change? What do you want to change it to?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    Please post the code that creates the GUI shown in the image in post#55

    What do you mean by "on top"? Is an object "on top" if its image is closer to the top of the window?

    xxx
    yyy
    Here xxx is "on top" of yyy
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    while (result.next()) {
    byte[] image = null;
    image = result.getBytes("image");

    Image img = Toolkit.getDefaultToolkit().createImage(image);
    img = img.getScaledInstance(100,100, Image.SCALE_SMOOTH);
    JButton button1 = new JButton();


    ImageIcon icon = new ImageIcon(img);

    getContentPane().add(new JPanel(), BorderLayout.SOUTH);

    button1.setPreferredSize(new Dimension(10, 10));
    JLabel lPhoto = new JLabel();
    lPhoto.setIcon(icon);
    add(lPhoto);
    add(button1);

    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block

    e.printStackTrace();

    }
    setVisible(true);
    }

    --- Update ---

    image underneath and button on top, so basically so when you click the button you can see the image and then u click another button and the image under in shown, if it matches both images are shown and you can carry on with the game if they dont match the button reappears on the image..

    does it make sense?

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    What does "on top" mean? Did you see my questions in post#56?

    Do you mean that in any location on the grid there is either an image shown or a button is shown?

    When posting code: Please wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    What layout manager does the code use?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    sorry i forgot about the code,
    Last edited by stresstedout; March 4th, 2014 at 01:54 AM.

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    By "on top" do you mean that in any location on the grid there is either an image shown or a button is shown? Never both.


    The posted code needs formatting. Nested statements need to be indented. Too many statements are starting in the first column. See post#51 for an example.

    BTW main is NOT a good name for a class. It is confused with the method: main()
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    no there is a bottom layer which has a images and top layer has buttons.
    the buttons are used to cover the image... do u get it now?

    i shall change the name of class and sort the code



    Quote Originally Posted by Norm View Post
    By "on top" do you mean that in any location on the grid there is either an image shown or a button is shown? Never both.


    The posted code needs formatting. Nested statements need to be indented. Too many statements are starting in the first column.

    BTW main is NOT a good name for a class. It is confused with the method: main()

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    there is a bottom layer which has a images and top layer has buttons.
    Would it look like this on the window. BBB for buttons, IMG for images:
    BBB1 BBB2 BBB3 ...
    IMG1 IMG2 IMG3 ...

    There are two rows of objects show in the window: the top row shows buttons, the bottom row shows images.

    Or would it be like this:
    XXX XXX XXX ...
    XXX XXX XXX ...

    Where XXX is a location on the window where either an image is shown or a button is shown.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    i am trying to make what is shown in this video...
    https://www.youtube.com/watch?v=50bFmi6Eopw
    so when the button is clicked the images of the animals are shown, so thats what i mean by image under and button on top.

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    Sorry, I don't go to other sites.

    If it is not one of the two choices I asked about, I don't understand what you are asking.

    What is wrong with this explanation?
    would it be like this:
    XXX XXX XXX ...
    XXX XXX XXX ...

    Where XXX is a location on the window where either an image is shown or a button is shown.

    There is no top or beneath. There is a region on the window where either an image is shown or a button is shown.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    Did you read my last post? What is wrong with the description I posted there?

    look at this.. we have buttons and on top we have images..
    In the image in post#65 I see 5 rows with 4 columns of rectangles all containing images. Where are the buttons in that image?


    Make a small simple program that uses an image file from the hard drive and creates a window that shows what you are talking about. Don't use a database .
    If you don't understand my answer, don't ignore it, ask a question.

  14. #64
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: javadisplay images

    Quote Originally Posted by stresstedout View Post
    how would you get the images to be under the buttons instead of then being assigned to be on top. maybe this will help with what im askn help for.
    In my answer #42 I told you one thing and I can repeat here.

    If you want that every cell (of GridLayout) contains a JLabel and a JButton, you have to create, for each cell, a new JPanel with a layout manager that disposes label and button as you desire.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  15. #65
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    i tried that it didnt work so i changed it. oh man

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: javadisplay images

    Quote Originally Posted by stresstedout View Post
    i tried that it didnt work so i changed it. oh man
    So you want the button above and image (label) below?

    JPanel cellPanel = new JPanel(new BorderLayout());
    cellPanel.add(yourButton, BorderLayout.NORTH);
    cellPanel.add(yourLabel, BorderLayout.CENTER);
    // add cellPanel into panel with GridLayout

    All this for each cell in GridLayout.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  18. #68
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    [QUOTE=andbin;138438]So you want the button above and image (label) below?
    Last edited by stresstedout; March 4th, 2014 at 01:54 AM.

  19. #69
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    How have you tried using the code provided by andbin? You need to try and when you have problems, post the code and the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    Please edit the post and properly format the code. Nested statements need to be indented. Too many statements are starting in the first column. See post#51 for an example.

    For testing the creation of the GUI, forget about using a database and read the image from a disk file:
              ImageIcon icon = new ImageIcon("path to image here");
    If you don't understand my answer, don't ignore it, ask a question.

  21. #71
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    Quote Originally Posted by Norm View Post
    Please edit the post and properly format the code. Nested statements need to be indented. Too many statements are starting in the first column. See post#51 for an example.

    For testing the creation of the GUI, forget about using a database and read the image from a disk file:
              ImageIcon icon = new ImageIcon("path to image here");
    i need to use the database can i add in after?

    REGARDLESS of me using the database or the disk file i still cant seem to get it all in place

  22. #72
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: javadisplay images

    You don't need the database to work on the GUI problem you are having. For testing you can use a single image read from the disk.

    Later when the GUI is working, you can put in the code to read from the database.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #73
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    [QUOTE=andbin;138438]So you want the button above and image (label) below?
    Last edited by stresstedout; March 4th, 2014 at 01:55 AM.

  24. #74
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: javadisplay images

    Quote Originally Posted by stresstedout View Post
    ITS STILL PUTTING THEM BESIDE EACH OTHER
    LY FORMATTED I WILL DO
    Last edited by stresstedout; March 4th, 2014 at 01:55 AM.

  25. #75
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: javadisplay images

    Quote Originally Posted by stresstedout View Post
    I KNOW ITS NOT PROPERLY FORMATTED I WILL DO ONCE I GET THE BUTTONS ON TOP OF THOSE LABELS. PLEASE HELP ME
    First, I am very disappointed by the fact that I continue to give my help and you don't understand.

    I have told you to create, for each cell in GridLayout, a new JPanel that contains JButton and JLabel (laid out with BorderLayout) and put that panel (not button/label) into the GridLayout. I have also posted a valid code.
    And this is not what you have done in your last code!

    This is my last advice:

    ......
    while (result.next()) {
        ........
        JButton button = ...........
        JLabel lPhoto = ...........
        ........
     
        JPanel cellPanel = new JPanel(new BorderLayout());
        cellPanel.add(button, BorderLayout.NORTH);
        cellPanel.add(lPhoto, BorderLayout.CENTER);
        yourContainerWithGridLayout.add(cellPanel);
        ........
    }

    Where yourContainerWithGridLayout is the container that has the GridLayout. I don't known now (to be honest I am only confused by all your posted code ....) what is your container with the GridLayout. You must to know, at least, this.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Page 3 of 5 FirstFirst 12345 LastLast

Similar Threads

  1. No images being displayed
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2013, 02:57 PM
  2. Images
    By Hoshi in forum Java Theory & Questions
    Replies: 1
    Last Post: September 6th, 2012, 10:54 PM
  3. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM
  4. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM

Tags for this Thread