Java GUI help

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Why does this work.
Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame {
	JPanel mypanel = new JPanel();
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	FlowLayout layout = new FlowLayout();
	
	public SolarSystemGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		label.setText("Solar System controller:");
		label2.setText("Author: ");
		mypanel.add(label);
		mypanel.add(label2);
		mypanel.setLayout(layout);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(500, 300);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
}

But this not?

Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame {
	JPanel mypanel = new JPanel();
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	FlowLayout layout = new FlowLayout();
	

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		label.setText("Solar System controller:");
		label2.setText("Author: ");
		mypanel.add(label);
		mypanel.add(label2);
		mypanel.setLayout(layout);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(500, 300);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	
}

I can't understand it. To me they seem exactly the same.
 
Last edited:
Because all that code isn't within a method.

try
Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame {
  JPanel mypanel = new JPanel();
  JLabel label = new JLabel();
  JLabel label2 = new JLabel();
  FlowLayout layout = new FlowLayout();
	
    public SolarSystemGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		label.setText("Solar System controller:");
		label2.setText("Author: ");
		mypanel.add(label);
		mypanel.add(label2);
		mypanel.setLayout(layout);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(500, 300);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
  }
	
}

Oops, I see I've just given you your original code, I probably should've read the thread before replying.

The reason it doesn't build is because it breaks Java's grammar, you can define and initialize variables like you have (though they're initialized at construction time), however it won't let you call non static methods like setDefaultCloseOperation(int blah) from a static context.
 
Last edited:
Thanks.

If I want to add a block of text to a panel is there a better way than using multiple labels? JTextArea formats it right, but the background is white, I want it to look like a label does ie blend into the background, not like a text field where you can enter text on a white background.
 
You can call setOpaque(false) and setBackground(null) on your text area, you'd also have to make it non editable and hide the caret from within it, there is nothing wrong with using a number of labels though.
 
You can also make use of HTML tags in most components, including JLabel so you could span text over multiple lines easily.

Code:
myJLabel.setText("<html>Hello <br> World!</html>");
 
You can also make use of HTML tags in most components, including JLabel so you could span text over multiple lines easily.

Code:
myJLabel.setText("<html>Hello <br> World!</html>");

That's much more elegant. :) I have to wonder why the people who created java don't allow you to just use \n.
 
Last edited:
Can't get this to work,

Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame implements ActionListener {
	JPanel mypanel = new JPanel();
	JPanel mypanel2 = new JPanel();
	JLabel mylabel = new JLabel();
	BorderLayout layout = new BorderLayout();
	JButton button = new JButton();
	JButton button2 = new JButton();
	
	public SolarSystemGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		mylabel.setText("<html> Solar System Controller v1.0 <br> Author: ***** </html>");
		button.setText("Add planet");
		button2.setText("Find Planet");
		mypanel.setLayout(layout);
		mypanel2.add(button);
		mypanel2.add(button2);
		mypanel.add("South", mypanel2);
		mypanel.add("North", mylabel);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(350, 200);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {}
}

Says that it can't find ActionEvent or ActionListener.
 
Last edited:
They're in a different package, you need to import java.awt.event.*; as well. Importing x.* doesn't recurse down through those packages.
 
Energize are you using an IDE for your Java programming? Modern IDEs such as Eclipse and NetBeans will pick up these errors immediatly and most of the time suggest corretive action to resolve the issue.

This is Eclipse:

correct1.png


correct2.png
 
Last edited:
I'm just learning Java at the moment too and Netbeans does in fact pick that error straight away. I've also found it beneficial to important individual packages as opposed to the whole package.
 
Energize are you using an IDE for your Java programming? Modern IDEs such as Eclipse and NetBeans will pick up these errors immediatly and most of the time suggest corretive action to resolve the issue.

I do normally, but I have no idea how to add classes that I don't have the src code for into a project.
 
Ok, so based on looking at what netbeans does I've got this;

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class SolarSystemGUI extends JFrame implements ActionListener {
	JPanel mypanel = new JPanel();
	JPanel mypanel2 = new JPanel();
	JLabel mylabel = new JLabel();
	BorderLayout layout = new BorderLayout();
	JButton button = new JButton();
	JButton button2 = new JButton();
	
	public SolarSystemGUI() {
		button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                buttonActionPerformed(e);
            }
        });
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		mylabel.setText("<html> Solar System Controller v1.0 <br> Author:  </html>");
		button.setText("Add planet");
		button2.setText("Find Planet");
		mypanel.setLayout(layout);
		mypanel2.add(button);
		mypanel2.add(button2);
		mypanel.add("South", mypanel2);
		mypanel.add("North", mylabel);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(350, 200);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
	
	}
	
	public void buttonActionPerformed(ActionEvent e) {
		System.out.println("test");
	}
}

However I'm assuming there's a much better way to do it than that? The lecture notes mention using .getSource(); and an if statement, but if I try to make the button press to go the actionPerformed method I get a ton of runtime errors.
 
Change it to button.addActionListener(this); then your actionPerformed(ActionEvent e) method will be called.

Are your lecture notes suggesting something like
Code:
public class blah implements ActionListener {
  private JButton mButton1;
  private JButton mButton2;

  public blah(){
    mButton1 = new JButton("1");
    mButton2 = new JButton("2");
  
    mButton1.addActionListener(this);
    mButton2.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e){
    Object Source = e.getSource();
    if(Source == mButton1){
      //button 1 pressed
    }else if(Source == mButton2){
      //button 2 pressed
    }else{
      //impossible to hit here in this case
    }
  }
}

It must be said however when I write listeners I generally make them private inner classes or anonymous inner classes, I don't know why but I don't like having a 'main' class as a listener, just a class specifically for it, if that makes sense.
 
Change it to button.addActionListener(this); then your actionPerformed(ActionEvent e) method will be called.

Are your lecture notes suggesting something like
Code:
public class blah implements ActionListener {
  private JButton mButton1;
  private JButton mButton2;

  public blah(){
    mButton1 = new JButton("1");
    mButton2 = new JButton("2");
  
    mButton1.addActionListener(this);
    mButton2.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e){
    Object Source = e.getSource();
    if(Source == mButton1){
      //button 1 pressed
    }else if(Source == mButton2){
      //button 2 pressed
    }else{
      //impossible to hit here in this case
    }
  }
}

Yes thanks, that's it. :)

I'm just catching up on the java work because the lectures start too early on the monday to go to. Unless I want to be stuck at uni for 11 hours anyway. :eek: Luckily the Java Course has finished for this year, so there's nothing more to learn.
 
Last edited:
How can I add an actionListener to a component automatically upon creation? I have an array which adds pictures to a JButton like this,
Code:
for (int x = 0; x < 12; x++) {
	content.add(new JButton(new ImageIcon("bart" + x + ".jpg")));
}
 
How about this:

Code:
for (int x = 0; x < 12; x++) {
	JButton jb = new JButton(new ImageIcon("bart" + x + ".jpg"));
	jb.AddActionListener(this);
	content.add(jb);
}
 
That kind of code is textbook stuff you'd find when looking to build your first calculator - you haven't really helped yourself given you couldn't sacrifice going to a morning lecture :p
 
Back
Top Bottom