5 Replies - 411 Views - Last Post: 31 July 2013 - 02:46 AM Rate Topic: -----

#1 AndreeU17  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 141
  • Joined: 29-June 13

Trying to understand Java swing Better!

Posted 30 July 2013 - 09:06 PM

Hi im looking into java swing, trying to better understand the concept behind GUI (Graphical User Interface).
im having a bit of an issue in understanding why we use this and whats the purpose of it.
public tooltip() {
        
        initUI();
    }
private void initUI(){

}



I can seem to understand the purpose of it in this simple swing example of tooltips (Hovering aid).
/*
 * A tool tip is the tip when hovering over something specific
 */
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class tooltip extends JFrame {

    public tooltip() {
        
        initUI();
    }

    private void initUI() {

        JPanel panel = new JPanel();
        getContentPane().add(panel);

        panel.setLayout(null); //This is what creates the tool tip only for panel
        panel.setToolTipText("A Panel container"); //Panel = background of window

        JButton btn = new JButton("Button"); // Button Object 
        btn.setBounds(100, 60, 100, 30); // Button positioning
        btn.setToolTipText("A Button component"); //What will pop out on the hover
        // ^ what give the hover of the button above
        
        panel.add(btn);

        setTitle("Tooltip");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            	tooltip ex = new tooltip();
                ex.setVisible(true);
            }
        });
    }
}



Now besides that i have another question, what is a better way of understanding and using swing? Must i memories everything or is it always best to have a small journal were all methods are name in case i need a specific method. I feel like most programmers here simply program non-stop because of their huge vast knowledge in the language, or am i wrong?
ANY help is appreciated thank you :D

-Jonathan

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand Java swing Better!

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4356
  • View blog
  • Posts: 12,175
  • Joined: 18-April 07

Re: Trying to understand Java swing Better!

Posted 30 July 2013 - 09:26 PM

Well first we all don't use that first example you have there. That is just someone who is factoring out what goes in the constructor (the method named the same as the class) and put it into another function. They might be doing this to keep the constructor simple and clean. The idea would be that the constructor may call the initUI() and then call some other functions to do some other things. Of course they don't do anything else in your example. But that setup is not some standard or anything. Many people put the init type of code right in the constructor.

As for the best way to understand swing is just to dive in and try to do some things. Read some material on the subject and experiment. That is the name of the game to learning programming. Read up on how things work, try out some examples, see what works and doesn't and that way you get the idea of why things are done the way they are and at the same time you learn all about the error messages you might get.

Unless you deal with a language day in and day out and have thousands of hours of experience in a specific language, most programmers have to stop and look up a function from time to time. I don't think anyone expects you to know thousands of functions and all their signatures. That is why they invented intellisense and autocompletion features.

You just have to put in the time and practice to learn. You can read about riding a bike but until you get on a bike, crash a few times and keep practicing you will never learn.

And last thing... get it in your head right now, you are ALWAYS learning. If you are not, you are stale and outdated. I have over 16 years as a programmer and I am still learning things every day.

http://docs.oracle.c...torial/uiswing/

:)

This post has been edited by Martyr2: 30 July 2013 - 09:29 PM

Was This Post Helpful? 1
  • +
  • -

#3 AndreeU17  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 141
  • Joined: 29-June 13

Re: Trying to understand Java swing Better!

Posted 30 July 2013 - 09:40 PM

View PostMartyr2, on 30 July 2013 - 10:26 PM, said:

Well first we all don't use that first example you have there. That is just someone who is factoring out what goes in the constructor (the method named the same as the class) and put it into another function. They might be doing this to keep the constructor simple and clean. The idea would be that the constructor may call the initUI() and then call some other functions to do some other things. Of course they don't do anything else in your example. But that setup is not some standard or anything. Many people put the init type of code right in the constructor.

As for the best way to understand swing is just to dive in and try to do some things. Read some material on the subject and experiment. That is the name of the game to learning programming. Read up on how things work, try out some examples, see what works and doesn't and that way you get the idea of why things are done the way they are and at the same time you learn all about the error messages you might get.

Unless you deal with a language day in and day out and have thousands of hours of experience in a specific language, most programmers have to stop and look up a function from time to time. I don't think anyone expects you to know thousands of functions and all their signatures. That is why they invented intellisense and autocompletion features.

You just have to put in the time and practice to learn. You can read about riding a bike but until you get on a bike, crash a few times and keep practicing you will never learn.

And last thing... get it in your head right now, you are ALWAYS learning. If you are not, you are stale and outdated. I have over 16 years as a programmer and I am still learning things every day.

http://docs.oracle.c...torial/uiswing/

:)/>


WoW you just inspired me :D such great words and help i really appreciate it. Yes im glad even professional have to pick up a cheat sheet every now and then. Also i will dig into learning more about swing etc. So you say that i dont really need to use initUI()? thank you
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1405
  • View blog
  • Posts: 3,120
  • Joined: 05-April 11

Re: Trying to understand Java swing Better!

Posted 30 July 2013 - 10:59 PM

Use the documentation to look up classes and methods
I can of course remember the commonly used classes and methods, but I am using the docs every day :)
It doesn't take much time to look up a method when you are used to doing it
Was This Post Helpful? 0
  • +
  • -

#5 AndreeU17  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 141
  • Joined: 29-June 13

Re: Trying to understand Java swing Better!

Posted 30 July 2013 - 11:01 PM

View PostCasiOo, on 30 July 2013 - 11:59 PM, said:

Use the documentation to look up classes and methods
I can of course remember the commonly used classes and methods, but I am using the docs every day :)/>
It doesn't take much time to look up a method when you are used to doing it


WOW seemingly outstanding :D thank you for the tip man i just checked out the site and it has everything (Well looks like it does lol).
thank You :D

-Jonathan
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2041
  • View blog
  • Posts: 4,223
  • Joined: 11-December 07

Re: Trying to understand Java swing Better!

Posted 31 July 2013 - 02:46 AM

People have different ways of organising their code. It looks like your instructor's ideas aren't too far from mine. Here is a demo with comments explaining how I like to organise my Swing code.

(Don't worry if you don't understand every single line. Anonymous classes, for example, are something you might like to revisit later.)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SwingCodeDemo extends JFrame {

	// A really short main just to kick things off.
	// Usually I put this in its own very short class.
	public static void main(String[] args) {
		new SwingCodeDemo().setVisible(true);
	}

	// Gui elements go here
	// but only if they need to be accessed by other methods
	private JTextField userText;
	private JButton display;

	public SwingCodeDemo() {
		super("Just a swing demo.");

		// Instantiate all the fields:
		userText = new JTextField(50);
		display = new JButton("Display...");

		// Keep things tidy by setting up the layout and events in different
		// methods:
		setUpLayout();
		setUpEvents();
	}

	private void setUpLayout() {
		// I'll declare a lot of components here because
		// they are only for decoration or layout purposes.

		// This panel uses a BorderLayout to keep my button
		// to the right, and a border to add a little spacing.
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.add(display, BorderLayout.EAST);
		buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 50));

		JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(userText, BorderLayout.CENTER);
		mainPanel.add(buttonPanel, BorderLayout.SOUTH);

		add(mainPanel);
		pack();
		setLocation(100, 100);
		// Some people would add setVisible(true) here. I think that's bad
		// manners.
		// It should be up to the class that uses it when and if to display a
		// frame.
	}

	private void setUpEvents() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Event listeners are anonymous classes.
		// I keep them very short so they don't get confusing.
		display.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showUserTextInDialog();
			}
		});
	}

	// Putting the actual event code in another method makes my class more
	// flexible.
	// I can call it from listeners attached to other components.
	// I can call it from test code.
	//
	// In this case, I don't need any info from the ActionEvent but
	// if I did, I could pass that to this method.
	private void showUserTextInDialog() {
		String message = userText.getText();
		JOptionPane.showMessageDialog(this, message);
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1