aspose file tools*
The moose likes Java in General and the fly likes JCheckBox? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JCheckBox?" Watch "JCheckBox?" New topic
Author

JCheckBox?

Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52
Hi
My question is: How can I add class JCheckBox into my class Frame1?
It should be only one class...Frame1
Thanks
------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class CheckBox{
public static void main(String[] args){
Frame frame= new Frame("Checkbox");
Checkbox check=new Checkbox("Welcome");
Checkbox check1=new Checkbox("Hello");
frame.add(check);
frame.add(check1);
frame.setLayout(new FlowLayout());
frame.setSize(300,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}

---------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
* @author Lenny
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
this.setVisible(true);
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 400));
setTitle("AddMemberFrame");

jLabel1.setText("Crn.: ");
jLabel1.setBounds(new Rectangle(45, 76, 50, 15));
jTextField1.setBounds(new Rectangle(114, 76, 200, 21));

jLabel2.setText("Surname: ");
jLabel2.setBounds(new Rectangle(45, 180, 60, 15));
jTextField2.setBounds(new Rectangle(114, 180, 200, 21));

jLabel3.setText("Name: ");
jLabel3.setBounds(new Rectangle(45, 127, 61, 15));
jTextField3.setBounds(new Rectangle(114, 127, 200, 21));

jLabel4.setText("Address: ");
jLabel4.setBounds(new Rectangle(45, 233, 60, 15));
jTextField4.setBounds(new Rectangle(114, 233, 200, 21));

jButton1.setBounds(new Rectangle(124, 300, 73, 21));
jButton1.setText("Save");
jButton2.setBounds(new Rectangle(224, 300, 73, 21));
jButton2.setText("Cancel");

contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jTextField2);
contentPane.add(jLabel2);
contentPane.add(jTextField3);
contentPane.add(jLabel3);
contentPane.add(jLabel4);
contentPane.add(jTextField4);
contentPane.add(jButton1);
contentPane.add(jButton2);

}
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JTextField jTextField4 = new JTextField();

//JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
//JPanel contentPane;

}
[ May 08, 2008: Message edited by: Leni Kravitskki ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Welcome to the Ranch.
Your post might have been better on the Swing forum.

Why are you throwing an Exception from your jbInit method? I think that is unnecessary.
Don't use flowLayout on a Frame; the deafault of BorderLayout is probably better.
Also probably a bad idea to use null layout. For your purposes I suspect GridLayout would be the easiest to use.
Why are you using AWT classes in the BheckBox class and Swing classes in the other class?

What you are doing is adding a Checkbox to a Frame in your 1st class, but that class makes no reference at all to the Frame1 class.
If you are creating the Checkboxes in that class (preferable to use JCheckbox), then pass them to the Frame1 class in the Frame1 constructor, then they will be available for you to add. Probably easier still to crete them in the Frame1 class, however.

Hope that has enabled you to get your app working better.
Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52
...then pass them to the Frame1 class in the Frame1 constructor, then they will be available for you to add....

I don't now how? What I should writte int the constructor?
This is a new class I want to add into frame1. ;-)

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class CheckBox1 {
public static void main(String args[]) {
JFrame frame = new JFrame("Add activity form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 2));

JCheckBox check = new JCheckBox("Active");
check.setBorderPaintedFlat(true);
panel.add(check);

check = new JCheckBox("Passive");
panel.add(check);
check = new JCheckBox("Junior");
check.setBorderPaintedFlat(true);
panel.add(check);
check = new JCheckBox("Senior");
panel.add(check);
check = new JCheckBox("Jogger");
panel.add(check);
check = new JCheckBox("Contest swimmer");
panel.add(check);
check.setBorderPaintedFlat(true);

Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);

frame.setSize(300, 350);
frame.setVisible(true);
}
}


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Probably best to remove the check boxes from the main() method.

In the Frame1 jbInit method try adding the JCheckBoxes. You can probably copy most of the code.Now you can write similar code to what I deleted in the jbInit method. See how that works.

And you don't need a content pane to add to a JFrame if you are using Java 5 or Java 6. It's add(startButton);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCheckBox?