Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By KevinWorkman
  • 1 Post By JosAH
  • 1 Post By SurfMan

Thread: Some suggestions for making a GUI

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Some suggestions for making a GUI

    Note:- I might not be a good thing to ask this question and you might tell me that I should read up on Java Swing and get the concepts but I am not in a state of doing so at the moment, and I need to do this task, so I decided to ask anyway. I'll greatly appreciate any help/guideline.

    I need to make a GUI. I don't have any experience with Java Swing and I am in a real hurry. So I am posting here to request some suggestions about how can I achieve what I need. I need to be told what components should I use. What important methods might be useful for me to achieve it, what layout should I use etc.

    1. There are five buttons at the bottom, and four buttons on the top. I want to add pictures to them such that they appear just like icons, without any text or any border etc. (That is it should not look like a button, it should just be an icon, and with no text associated with it)

    2. There is a label saying logo, I want to add an image to it, and also a listener. I hope we can add listeners to labels, for mouse click events.

    The listener should open a small window which displays some information.

    3. I want an image on the background. The background image should adjust its size according to the size of the window(no matter whatever is the size of the image is) when (1) it is placed on the frame/panel/label/whatever (2) when the window is resized (maximized etc.).

    Some suggestions for making a GUI-untitled.png

    Thank you so very much in advance.

    ____________________________________
    EDIT:- In the following image, you can see a thin line making a square around the image (of the icon of the recycle bin), I call it a border of the button. I do not want this border to appear around the buttons. I just want the image to be visible. The button under it should not be visible.

    Some suggestions for making a GUI-untitw.png
    Last edited by Zarah; 04-28-2014 at 09:10 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Some suggestions for making a GUI

    Remember that you can nest layouts. To me this looks like a BorderLayout in your JFrame. Both the NORTH and SOUTH positions of the BorderLayout hold a JPanel. Both of those JPanels has a horizontal BoxLayout.

    For the background, you should look into custom painting. Start here: http://docs.oracle.com/javase/tutori...wing/painting/

    Everything else you asked is in the basic tutorials for JButton, JLabel, and MouseListener.
    Zarah likes this.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Some suggestions for making a GUI

    For the top row you can use a JToolBar and popuate it with your buttons. A JLabel can't notify any ActionListeners so your logo needs to be a JButton as well. Your content pane should have a BorderLayout and its south part should have another JPanel for the JButtons near the bottom of the window; that other JPanel should have a FlowLayout for the JButtons to neatly line up.The center part of the content pane should contain your image.

    kind regards,

    Jos
    Zarah likes this.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Some suggestions for making a GUI

    In general, when creating a more complex GUI, you need to use several JPanels that have all sorts of layouts. BorderLayout, GridLayout are frequently used. For those that have more time and experience, GridBagLayout is very useful. It can be complex, but in the end, with some experience, you learn to work it quickly. But I would focus on BorderLayout, GridLayout, maybe FlowLayout can do good stuff for you as well. (I am not really up to speed with all the built-in layouts, since I use MigLayout for everything, which is a separate library... Shamless plug: MiG Layout Java Layout Manager for Swing and SWT)

    Usually you should sketch a GUI on paper and try to identify components that belong together. See if you can find a combination of JPanels and layouts that realize this.

    Maybe for that image button, a JLabel with an ImageIcon, add a MouseListener, maybe set the cursor to Cursor.HAND: myJLabel.setCursor(Cursor.getPredefinedCursor(Curs or.HAND_CURSOR));
    Zarah likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Some suggestions for making a GUI

    For the top row you can use a JToolBar and popuate it with your buttons.
    Will the toolbar become transparent so that it is just the pictures on the buttons which are visible, and nothing from the toolbar's grey background is visible?

    that other JPanel should have a FlowLayout for the JButtons to neatly line up.
    Shall I be able to align them in the centre of the panel? I don't want them to start from the left and leave space on the right.

    A JLabel can't notify any ActionListeners so your logo needs to be a JButton as well.
    I was just thinking I should make all the buttons rather labels so that when I add a picture, its border is not visible. I am adding a picture to the question to clarify what I mean by the border of the button (which I do not want to see).

  6. #6
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Some suggestions for making a GUI

    In general, when creating a more complex GUI, you need to use several JPanels that have all sorts of layouts.
    Is it possible to add panels to panels. Like add a panel to the background, then add a background image on it, then add another transparent panel and add the components on it, so that the components don't go under the image, and the image stays intact and separated on from the components

    a JLabel with an ImageIcon, add a MouseListener, maybe set the cursor to Cursor.HAND: myJLabel.setCursor(Cursor.getPredefinedCursor(Curs or.HAND_CURSOR));
    I will have to search a bit to understand this.

    Thank you for telling about the library, I think I can not think of using it for now because I don't even know enough about the built in layout managers.
    Last edited by Zarah; 04-28-2014 at 09:22 PM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Some suggestions for making a GUI

    Quote Originally Posted by Zarah View Post
    Is it possible to add panels to panels. Like add a panel to the background, then add a background image on it, then add another transparent panel and add the components on it, so that the components don't go under the image, and the image stays intact and separated on from the components
    For that image you should follow KevinWorkman's advice in reply #2: custom painting.

    Quote Originally Posted by Zarah View Post
    I will have to search a bit to understand this.
    Normally you have a JButton with an ActionListener that responds to your clicks. But with your requirements, the JButton will have all kinds of decorations you don't need. So you could replace it with a JLabel which doesn't have these decorations. Unfortunately, JLabel doesn't have a way to attach an ActionListener that listens to clicks and all. So a general purpose MouseListener can be used that listens to the click event. In order to let the user know this label can be clicked, they need feedback, like a cursor that changes shape.

    Here's a rough example:
    Java Code:
    ImageIcon icon = null;
    try {
        icon = new ImageIcon(ImageIO.read(new URL("http://img3.wikia.nocookie.net/__cb20090227184041/java/images/5/55/Java_Duke.gif")));
    }
    catch (IOException e) {
        e.printStackTrace();
        //something went wrong loading the image. Fix this!
    }
    
    JLabel lab = new JLabel(icon);
    lab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    lab.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JOptionPane.showMessageDialog(f, "You clicked me!");
        }
    });
    (My example uses a MouseAdapter, that is a class that implements the MouseListener interface (and others), that lets you pick a method to override, instead of implementing *all* the interface methods.)

    Quote Originally Posted by Zarah View Post
    Thank you for telling about the library, I think I can not think of using it for now because I don't even know enough about the built in layout managers.
    You are right. Get comfortable with Swing and it's layouts, then you can start looking into libraries. It's useless to start with MigLayout if you totally lack the concept of layoutmanagers in the first place.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Some suggestions for making a GUI

    Quote Originally Posted by SurfMan View Post
    Normally you have a JButton with an ActionListener that responds to your clicks. But with your requirements, the JButton will have all kinds of decorations you don't need.
    So remove them; see the setBorderPainted(false) and setContentAreaFilled(false) method calls; there is no need to hack a JLabel for this ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Any Suggestions???
    By Coulterce in forum Forum Lobby
    Replies: 1
    Last Post: 06-29-2012, 06:38 PM
  2. Suggestions for practice
    By shrimathi in forum New To Java
    Replies: 2
    Last Post: 06-08-2011, 07:03 AM
  3. [suggestions] minesweeper
    By temyong in forum New To Java
    Replies: 7
    Last Post: 12-30-2010, 03:10 AM
  4. Looking for suggestions
    By tonino in forum New To Java
    Replies: 5
    Last Post: 09-26-2010, 07:17 PM
  5. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •