Head First Java, 2nd Edition

Author: Kathy Sierra, Bert Bates
All Stack Overflow 26
This Year Stack Overflow 3
This Month Stack Overflow 1


by anonymous   2017-08-20

Head First Java is a great place to start.

Head First Java

by anonymous   2017-08-20

Your question is how to pass information from one JFrame to another, and this can be done as simply as having one class call a method of the other class. That you haven't done this, and that you've only posted a skeleton program, one with components but with no logic suggests to me that you are still very much a beginner Java programmer, and so my main suggestion is that first and foremost you strive to learn to code, and in particular learn about object oriented principles and how they relate to Java. Without these rudiments under your belt, we can give you code and pointers, but it won't help you much. I suggest that you go to the Java Tutorials and start there, but also that you get a decent book or two on the subject such as Bruce Eckel's Thinking in Java, and/or Head First Java.

As for your actual code I suggest that you not create classes that extend JFrame since that locks you into a JFrame, and again as per my comment above, your tool window should be a non-modal JDialog not a JFrame. If you gear your code towards creating JPanels, then you can place them into JFrames, JDialogs, other JPanels, etc... wherever needed, and so this gives you a lot more flexibility.

The main difficulty in the situation of your program is not passing information from one window to another, one object to another, really, but rather when to do so, since the program is event driven. Myself, I like to use PropertyChangeListeners for this, basically using an observer interface that is already part of the Swing GUI structure. For example in the code below I create two main JPanels, one is displayed within a JFrame, the other within a non-modal JDialog, and I pass button press information (the actionCommand String of the button) to the JTextArea in the main GUI via a PropertyChangeListener:

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class Foo3 {
    private static void createAndShowGui() {
        final MainPanel1 mainPanel1 = new MainPanel1();
        final ToolPanel1 toolPanel1 = new ToolPanel1();

        JFrame frame = new JFrame("Foo3");

        JDialog dialog = new JDialog(frame, "Toolbar", ModalityType.MODELESS);

        toolPanel1.addPropertyChangeListener(ToolPanel1.ACTION_COMMAND, new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                mainPanel1.appendActionCommand((String) evt.getNewValue());

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

class MainPanel1 extends JPanel {
    private JTextArea actionCommandArea = new JTextArea(30, 50);
    private JScrollPane scrollPane = new JScrollPane(actionCommandArea);

    public MainPanel1() {

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);

    public void appendActionCommand(String text) {
        actionCommandArea.append(text + "\n");

class ToolPanel1 extends JPanel {
    public static final String ACTION_COMMAND = "action command";
    public static final String[] BTN_TEXTS  = {
        "Select Element",
        "Insert Image",
        "Insert Text",
        "Insert Hyperlink",
        "Change Page Background",
        "Insert Textbox",
        "Insert Radio Button", 
        "Insert Checkbox",
        "Insert Horizontal Rule",
        "Insert Button",
        "Insert Drop-Down List",
        "Insert List",
        "Add Script"
    private String actionCommand = "";

    public ToolPanel1() {
        int rows = 0; // variable number of rows
        int cols = 2; // 2 columns
        int hgap = 5;
        int vgap = hgap;
        setLayout(new GridLayout(rows, cols, hgap, vgap));
        setBorder(BorderFactory.createEmptyBorder(hgap, hgap, hgap, hgap));

        for (String btnText : BTN_TEXTS) {
            add(new JButton(new ButtonAction(btnText)));

    public String getActionCommand() {
        return actionCommand;

    private class ButtonAction extends AbstractAction {
        public ButtonAction(String name) {

        public void actionPerformed(ActionEvent e) {
            String oldValue = "";
            String newValue = e.getActionCommand();
            actionCommand = newValue;
            ToolPanel1.this.firePropertyChange(ACTION_COMMAND, oldValue, newValue);

A more robust design would be to use a Model-View-Controller type design, but this is a bit more advanced, and you'll need to get some more code experience under your belt before using this, I think. also check out these links to similar questions/answers.

by anonymous   2017-08-20

In future you should post these sorts of questions on the Programmers Q&A site.

If you want to learn best practices your best bet is to learn from multiple sources. That way you see the content from multiple perspectives and that is the best way to gain your own perspective on the subject. You sound pretty keen on videos although to be honest I wouldn't recommend them. You can learn much more from written articles, tutorials and books like Head First Java and the official Java Tutorial etc. with examples and exercises. You cannot learn programming without doing it.

by anonymous   2017-08-20

A few things stood out to me as your goals:

  1. You want formal training on how to program (this is independent of a language)
  2. You want to learn how to develop apps for a mobile device
  3. Your boss is on board with goals 1 and 2, but wants to see the best return on her investment.

I think the easiest way for you to meet all of these goals is to start learning how to program with Java. Java is often used in introductory computer science courses, so you should be able to learn the language and programming concepts in parallel. Once you have that foundation, you will be able to start learning Android development, since Android applications are built with the Java language.

You can start off by reading some of the resources from Oracle: http://download.oracle.com/javase/tutorial/java/concepts/index.html

And there are plenty of good intro books too:

There is nothing wrong with starting off by learning Python. Once you have a good grasp of programming fundamentals, you can learn new languages relatively quickly. However, from your boss's perspective, there is more value in paying for you to learn a language for mobile development and programming at the same time.

by Bill the Lizard   2017-08-20

Head First Java is the best book for learning Java. It's an introduction to object oriented programming, as well as Java, so as an experienced programmer you might be better served by Thinking in Java.

by Fortyrunner   2017-08-20

You really need a copy of Head First Java

by shuman1981   2017-08-19


Yeah, it was a good quick read (one week at the beach) and the exercises were not mundane. It's great for visual learners as well if you're into that sort of thing.

by minotaurohomunculus   2017-08-19

Head First Java. It's the easiest book on Java to read that I've found.


by AlphaOmegaTubbster   2017-08-19

Here are a few helpful resources to help you out.

Firstly, you probably need a beginners grasp on Java. For that, I would highly recommend: https://toptalkedbooks.com/amzn/0596009208

You do not need to go through the entire book, But it would be more helpful to you.

Secondly, I highly recommend this android book: https://toptalkedbooks.com/amzn/0321804333

They literally walk you step-by-step.

However, if you do not feel you can teach yourself programming there is always this option: http://appinventor.mit.edu/explore/ I haven't personally messed around with it but it doesn't require any programming experience.

Here is a free online class that starts tomorrow if you have the time. https://www.coursera.org/course/androidpart1

or this one that is already finished but you can still access the material. https://www.coursera.org/course/androidapps101

You could also go at your own pace through it.

Here is also a udemy course that also teaches you java. I would get it now before the price goes back up to 200 bucks. https://www.udemy.com/android-lollipop-complete-development-course/?dtcode=hfFhrtG2ans8

I haven't personally taken it, but a friend of mine has and he loves it.

Basically, just start reading and learning. The big nerd ranch book that I listed has some really great beginner apps that teach you the basics.

Persistence is the key. Don't give up, fight through the pain. Google like crazy.It's worth it, trust me.