JButtons

Hi I'm wondering how you create JButtons outside the JFrame or the JPanel, just a normal class with a Jbutton That's what I want.

Thank you for helping...

Here's some Code:

publicclass Musikspelareextends Appletimplements ActionListener

{

JButton a,b;

AudioClip kairi;

String Url ="Triton.midi";

/*

public Musikspelare()

{

}

public void init()

{

}*/

publicvoid actionPerformed(ActionEvent evt)

{

if(evt.getSource()==a)

{

kairi.play();

}

if(evt.getSource()==b)

{

kairi.stop();

}

}

}

Am I supposed to create the JButtons in init(), in Musikspelare() or maybe somewhere else?

[1304 byte] By [Consideratea] at [2007-11-26 19:52:11]
# 1
JButtons can be created anywhere, but they need to be added to a Container parent, and to be seen that container parent needs to be added to a Frame or Applet and visualized either as an application or as an applet.
tjacobs01a at 2007-7-9 22:42:52 > top of Java-index,Java Essentials,Java Programming...
# 2

> JButtons can be created anywhere, but they need to be

> added to a Container parent, and to be seen that

> container parent needs to be added to a Frame or

> Applet and visualized either as an application or as

> an applet.

I have got a Frame but It's an other class how do I do?

Consideratea at 2007-7-9 22:42:52 > top of Java-index,Java Essentials,Java Programming...
# 3

> > JButtons can be created anywhere, but they need to

> be

> > added to a Container parent, and to be seen that

> > container parent needs to be added to a Frame or

> > Applet and visualized either as an application or

> as

> > an applet.

>

> I have got a Frame but It's an other class how do I

> do?

If you want to add the button to that Frame, why are you creating it in this Applet? If you want to add it to this Applet, then add it to the Applet in the init method.

hunter9000a at 2007-7-9 22:42:52 > top of Java-index,Java Essentials,Java Programming...
# 4
So if I add them to the Applet are the buttons able to show up in the Frame?And if I add them to the Frame is it possible for me to use them to change anything in this class?
Consideratea at 2007-7-9 22:42:52 > top of Java-index,Java Essentials,Java Programming...
# 5

> So if I add them to the Applet are the buttons able

> to show up in the Frame?

> And if I add them to the Frame is it possible for me

> to use them to change anything in this class?

I'm confused by what you're trying to accomplish. Do you want to have both an applet and a frame displayed? They're meant for different purposes, frames for desktop apps, and applets for web apps. If you just mean where in the code do you create the objects, then a good design will almost always have them in the applet or frame that's using them. You don't want to have gui elements mixed in with your other non-gui code. Does that answer your question?

hunter9000a at 2007-7-9 22:42:52 > top of Java-index,Java Essentials,Java Programming...
# 6

> > So if I add them to the Applet are the buttons

> able

> > to show up in the Frame?

> > And if I add them to the Frame is it possible for

> me

> > to use them to change anything in this class?

>

> I'm confused by what you're trying to accomplish. Do

> you want to have both an applet and a frame

> displayed? They're meant for different purposes,

> frames for desktop apps, and applets for web apps. If

> you just mean where in the code do you create the

> objects, then a good design will almost always have

> them in the applet or frame that's using them. You

> don't want to have gui elements mixed in with your

> other non-gui code. Does that answer your question?

Taking what Hunter said I would go a step further. Assuming that he's right about what you're after, I would create a JPanel with the buttons and the behavior for the buttons there in (Exactly how you code the behavior at this level is another topic) and I would then be able to add an instance of the panel to either an applet or a Swing JFrame with equal facility and fundamentally the same results. Bear in mind that an applet and a desktop app are very different and you'd have to allow for that in your button panel.

Hope this helps,

PS.

puckstopper31a at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 7

Ok I'll explain a bit closer...

This is the sructure of my program:

RectFrame:MainClass, JFrame

RectPanel: JPanel

Musikspelare: A class with nothing that uses RectPanel

So I'd like to add buttons to the Musikspelare Class but don't mind the Applet thing it has nothing to do with this, I'd like the buttons to show when I run RectFrame.

How do I create the buttons to show in RectFrame and then could be used in Musikspelare?

Consideratea at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 8

> Ok I'll explain a bit closer...

> This is the sructure of my program:

>

> RectFrame:MainClass, JFrame

> RectPanel: JPanel

> Musikspelare: A class with nothing that uses

> RectPanel

>

> So I'd like to add buttons to the Musikspelare Class

> but don't mind the Applet thing it has nothing to do

> with this, I'd like the buttons to show when I run

> RectFrame.

>

> How do I create the buttons to show in RectFrame and

> then could be used in Musikspelare?

1) Are you trying to display both a Frame and an Applet at the same time?

2) If the Frame class will display and use the buttons, then the Frame class should hold references to them and create them. A Frame and Applet shouldn't really even be used at the same time (for any reason I can think of), and they definitely should have their own components.

hunter9000a at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 9

I don't use the Applet really it's there for me so I can see what's happening in that certain class, once agian don't mind the applet.

Now we start over I have got these three classes:

RectFrame

RectPanel

Musikspelare

I have a JFrame in RectFrame, a JPanel in RectPanel and I'd like the buttons in Musikspelare to do what I showed in the code above(actionPerformed(ActionEvent)). Is it possible for me to somehow create the Buttons in RectFrame and still use them in Musikspelare?

That is my question now.

Consideratea at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 10

Hi I see many have watched this thread but few have replied, is it because I'm not clear in what I want or is it just because it's a hard problem.

If you don't think I'm clear enough it's because I'm not sure what I want to do with this all I want is buttons that will show in my JFrame and they will be able to do this:

public void actionPerformed(ActionEvent evt)

{

if(evt.getSource()==a)

{

kairi.play();

}

if(evt.getSource()==b)

{

kairi.stop();

}

}

That's all I'm asking for so please don't go into the spesifics

Consideratea at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 11

I can see now that this is not really what you want ... I played with it a bit while the last post was like ... #6 or so ... anyway, perhaps the code might assist a bit:

// <applet code="Musikspelare.class" width="200" height="100"></applet>

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

import javax.swing.JApplet;

import java.net.URL;

public class Musikspelare extends JApplet

{

AudioClip kairi;

String Url = "Triton.midi";

TempPanel tempPanel;

public void init()

{

tempPanel = new TempPanel() {

public void actionPerformed(ActionEvent evt)

{

if(evt.getSource()==tempPanel.getA())

{

play();

}

if(evt.getSource()==tempPanel.getB())

{

stop();

}

}

};

getContentPane().add(tempPanel);

}

public void play() {

System.out.println("Play clip here");

}

public void stop() {

System.out.println("Stop Playing clip here");

}

private class TempPanel extends JPanel

implements ActionListener {

JButton a,b;

public TempPanel() {

a = new JButton("a");

b = new JButton("b");

a.addActionListener(this); // this is the JPanel that implements ActionListener

b.addActionListener(this);

add(a);

add(b);

setSize(75,75);

}

public JButton getA() {

return( a );

}

public JButton getB() {

return( b );

}

public void actionPerformed(ActionEvent evt) {

}

}

}

abillconsla at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 12
Hi, this program works great if I'm using an applet...That's the problem I'm not, I'm using a JFrame.Is there any way to write this code to a Frame instead?Thanks for helping
Consideratea at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 13
I haven't figured out how to make these buttons in a JFrame yet, I really need your help.//Considerate
Consideratea at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 14
> I haven't figured out how to make these buttons in a> JFrame yet, I really need your help. http://java.sun.com/docs/books/tutorial/uiswing/components/button.htmlThis link is right at the top of the API documentation for JButton.
DrClapa at 2007-7-9 22:42:53 > top of Java-index,Java Essentials,Java Programming...
# 15

One way:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ActionExample implements Runnable {

public void run() {

MusicBox musicBox = new MusicBox();

JToolBar tb = new JToolBar();

tb.add(musicBox.play);

tb.add(musicBox.stop);

JFrame f = new JFrame("ActionExample");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(tb, BorderLayout.NORTH);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new ActionExample());

}

}

class MusicBox {

Action play = new AbstractAction("play") {

private static final long serialVersionUID = 0;

public void actionPerformed(ActionEvent evt) {

System.out.println("play");

setEnabled(false);

stop.setEnabled(true);

}

};

Action stop = new AbstractAction("stop") {

private static final long serialVersionUID = 0;

public void actionPerformed(ActionEvent evt) {

System.out.println("stop");

setEnabled(false);

play.setEnabled(true);

}

};

public MusicBox() {

stop.setEnabled(false);

}

}

DrLaszloJamfa at 2007-7-21 17:44:57 > top of Java-index,Java Essentials,Java Programming...