• Post Reply Bookmark Topic Watch Topic
  • New Topic

java jbutton  RSS feed

 
Jeff Barker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a college student and trying to use layOut to put one jbutton inside another jbutton. Below is the part of the code I am struggling with.
I filled in blank one with < new FlowLayout() >
The second Blank "whole line" < outer.add(inner); >
The third blank I put < outer >
Can someone help me. I don't get it!!


frame = new JFrame("A button in a button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
outer = new JButton("Outer");
outer.setBackground(Color.black);
outer.setForeground(Color.white);
outer.addActionListener(new OuterListener());
outer.setLayout(_______________________);
inner = new JButton("Inner");
inner.setBackground(Color.red);
inner.addActionListener(new InnerListener());
_________.add(_________);
frame.getContentPane().add(__________, BorderLayout.CENTER);
message = new JLabel("Click a button...");
message.setHorizontalAlignment(JLabel.CENTER);
frame.getContentPane().add(message, BorderLayout.SOUTH);
frame.setVisible(true);
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can add a button to a button.

it wouldn't be too hard to make a jpg image of your 'inner' button,
then add that to your 'outer' button as the icon.

its also not that hard to separate the actions of the two.
 
Vasu Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi you can add Buttons to ToggleButton, below is the sample code.

Regards,


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

public class Button1 extends JFrame {

public static void main(String args[]) {
new Button1();
}

Button1() {//constructor
JToggleButton power = new JToggleButton();//container
power.setLayout(new FlowLayout());
JButton am = new JButton("AM");//another button
JButton fm = new JButton("FM");//another button

power.add(am);//Put button in container button
power.add(new JLabel("Power"));//Put label in button
power.add(fm);//Put button in container button

getContentPane().add(power);//pub button in frame

String plafClassName = //set look and feel
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try{
UIManager.setLookAndFeel(plafClassName);

}catch(Exception ex){
System.out.println(ex);
}
SwingUtilities.updateComponentTreeUI(this);

getContentPane().setLayout(new FlowLayout());
setSize(300,100);
setTitle("Buttons in Buttons");
setVisible(true);

// Inner WindowListener class to terminate program
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}});//end WindowListener

}//end constructor

}//end class
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone needs to ask the ovbious question here: why? Why do you want to put one button inside another? How do you want it to behave?
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good guestion. What is the application?
roba
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!