Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Absolute positioning + button action

Status
Not open for further replies.

mellthy

Programmer
Dec 6, 2001
17
CZ
Hi,

I'm a newbie in Java, please be tolerant :)

I got the simplest code possible (absolute positioning):

Code:
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;

public class AbsoluteLayoutDemo {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(null);

        JButton b1 = new JButton("one");
        JButton b2 = new JButton("two");
        JButton b3 = new JButton("three");

        pane.add(b1);
        pane.add(b2);
        pane.add(b3);

        Insets insets = pane.getInsets();
        Dimension size = b1.getPreferredSize();
        b1.setBounds(25 + insets.left, 5 + insets.top,
                     size.width, size.height);
        size = b2.getPreferredSize();
        b2.setBounds(55 + insets.left, 40 + insets.top,
                     size.width, size.height);
        size = b3.getPreferredSize();
        b3.setBounds(150 + insets.left, 15 + insets.top,
                     size.width + 50, size.height + 20);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("AbsoluteLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Size and display the window.
        Insets insets = frame.getInsets();
        frame.setSize(300 + insets.left + insets.right,
                      125 + insets.top + insets.bottom);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I'm not able to add ActionListeners to make the buttons do something.

Any hints?

Ganymed
 
At its simplest :

Code:
   b1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
         //Do something...
         System.out.println("Hello");
      }
   });

You'll need to import java.awt.event.ActionListener and java.awt.event.ActionEvent

Tim
 
Well, I put your suggested code after addComponentsToPane function, but it says that identifier was expected in the line with "*.addActionListener(new ActionListener()&{", "new" is underlined.

I imported java.awt.event.*

I tried to fix it but I can't find a solution.
 
I pasted that snippet straight out of an Eclipse project and it compiles/runs okay.
Have you checked that you've got all the curly and normal braces in the correct places?

The full method looks like this :-
Code:
public static void addComponentsToPane(Container pane) {
  pane.setLayout(null);

  JButton b1 = new JButton("one");
  JButton b2 = new JButton("two");
  JButton b3 = new JButton("three");

  pane.add(b1);
  pane.add(b2);
  pane.add(b3);

  Insets insets = pane.getInsets();
  Dimension size = b1.getPreferredSize();
  b1.setBounds(25 + insets.left, 5 + insets.top,
                     size.width, size.height);
  size = b2.getPreferredSize();
  b2.setBounds(55 + insets.left, 40 + insets.top,
                     size.width, size.height);
  size = b3.getPreferredSize();
  b3.setBounds(150 + insets.left, 15 + insets.top,
                     size.width + 50, size.height + 20);
        
  b1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      //Do something...
      System.out.println("Hello");
    });
  }
}
 
Strange, I got some errors reported at the end of the addActionListener function.

My complete code I use right now (red places are errors):

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

public class gui {

public static void addComponentsToPane(Container pane) {
  pane.setLayout(null);

  JButton b1 = new JButton("one");
  JButton b2 = new JButton("two");
  JButton b3 = new JButton("three");

  pane.add(b1);
  pane.add(b2);
  pane.add(b3);

  Insets insets = pane.getInsets();
  Dimension size = b1.getPreferredSize();
  b1.setBounds(25 + insets.left, 5 + insets.top,
                     size.width, size.height);
  size = b2.getPreferredSize();
  b2.setBounds(55 + insets.left, 40 + insets.top,
                     size.width, size.height);
  size = b3.getPreferredSize();
  b3.setBounds(150 + insets.left, 15 + insets.top,
                     size.width + 50, size.height + 20);

  b1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      //Do something...
      System.out.println("Hello");
    })[COLOR=red];[/color]
  [COLOR=red]}[/color]
}

public static void createAndShowGUI() {
  JFrame.setDefaultLookAndFeelDecorated(false);

  JFrame frame = new JFrame("morseovka");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  addComponentsToPane(frame.getContentPane());

  Insets insets = frame.getInsets();
  frame.setSize(800, 400);
  frame.setVisible(true);
}
}
 
It's going to be one of those days [dazed].
The first snippet of code I posted is the correct one as it appears in my test project. I somehow got the second code snippet mixed up?!
 
My fault, I should've checked that. Anyway, it's working ... finally :)

Thank you very much for your time and patience with my stupid questions.

Have a nice day

G.
 
No probs. Glad to help and glad it's working.

Tim
 
One more thing. I am not able to make JFileChooser to work in ActionListener.

Code:
package morseovka;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {

public static void addComponentsToPane(Container pane) {
  pane.setLayout(null);

  JButton b1 = new JButton("one");
  JButton b2 = new JButton("two");
  JButton b3 = new JButton("three");

  pane.add(b1);
  pane.add(b2);
  pane.add(b3);

  Insets insets = pane.getInsets();
  Dimension size = b1.getPreferredSize();
  b1.setBounds(25 + insets.left, 5 + insets.top,
                     size.width, size.height);
  size = b2.getPreferredSize();
  b2.setBounds(55 + insets.left, 40 + insets.top,
                     size.width, size.height);
  size = b3.getPreferredSize();
  b3.setBounds(150 + insets.left, 15 + insets.top,
                     size.width + 50, size.height + 20);

  final JFileChooser fc = new JFileChooser();

  b1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      int retval = fc.[COLOR=red]showOpenDialog[/color](test.[COLOR=red]this[/color]);
      if (retval == JFileChooser.APPROVE_OPTION) {
          File myFile = fc.getSelectedFile();
          String path = myFile.getPath();
          System.out.println(path);
      }
    }
  });
}

public static void createAndShowGUI() {
  JFrame.setDefaultLookAndFeelDecorated(false);

  JFrame frame = new JFrame("morseovka");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  addComponentsToPane(frame.getContentPane());

  Insets insets = frame.getInsets();
  frame.setSize(800, 400);
  frame.setVisible(true);
}
}

I think that I use pretty standard method. So I can't understand why there is a problem. Ahh, these newbies ...
 
This is because the showOpenDialog method is expecting a Component instance passed in its constructor. Your doing everything from static methods and the test class is never instantiated. Even if it was it can't be used since it doesn't subclass component.

You need to pass the frame instance in really, but you'll have to refactor your code somewhat.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top