I tried to implement a simple user interface of my programm.It is the first time that I used swing designer plugin.
1st problem :User enters start and end date and program automatically calculates the duration. But i dont know how i can write the restriction of the yyyy/MM/dd. I mean that user begins to enter year then skip the another block without make anything.
Here is my code:
public class MyFrame extends JFrame {
public MyFrame() {
}
private static JLabel labelBegin;
private static JLabel labelEnd;
private static JLabel labelDuration;
private static JLabel labelA;
private static JLabel labelB;
private static JLabel labelC;
private static JLabel labelD;
private static JLabel labelTotal;
private static JLabel labelSafety;
private static JLabel labelSpeed;
private static JLabel labelEfficiency;
private static JLabel message;
private static JTextField textFieldBegin;
private static JTextField EndingField;
private static JTextField DurationField;
private static JTextField AField;
private static JTextField BField;
private static JTextField CField;
private static JTextField DField;
private static JTextField TotalField;
private static JTextField SafetyField;
private static JTextField SpeedField;
private static JTextField EfficiencyField;
private static JButton clear;
private static JButton buttonNext;
private static JButton buttonBack;
private static JRadioButton PlacementResult;
private static JRadioButton Collision;
private static JPanel panelOptions;
private static JPanel panelLogin;
private static JFrame frame;
private static JButton Mybutton;
private static JPanel panelCons;
private static void mydesign() {
frame = new JFrame("Placement Tool V1.0");
frame.getContentPane().setLayout(new GridLayout(1, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelLogin = new JPanel();
panelLogin.setLayout(null);
message = new JLabel ("In order to begin your testing, please enter the data");
labelBegin = new JLabel("Enter Begin Date :");
textFieldBegin = new JTextField(10);
labelEnd = new JLabel("Test Duration :");
EndingField = new JTextField(10);
labelDuration = new JLabel ("Enter End Date :");
DurationField = new JTextField(10);
labelA = new JLabel ("Enter Group A :");
AField = new JTextField (10);
labelB = new JLabel ("Enter Group B:");
BField = new JTextField (10);
labelC = new JLabel ("Enter Group C:");
CField = new JTextField (10);
labelD = new JLabel ("Enter Group D:");
DField = new JTextField (10);
labelTotal = new JLabel ("Total Test Devices :");
TotalField = new JTextField (10);
labelSafety = new JLabel ("Enter Safety (%) : ");
SafetyField = new JTextField (10);
labelSpeed = new JLabel ("Enter Speed (kpbs)");
SpeedField = new JTextField (10);
labelEfficiency = new JLabel ("Enter Efficiency (%)");
EfficiencyField = new JTextField (10);
buttonNext = new JButton("Next");
clear = new JButton("Cancel");
panelLogin.add(buttonNext);
panelLogin.add(clear);
panelLogin.add(labelBegin);
panelLogin.add(message);
panelLogin.add(textFieldBegin);
panelLogin.add(labelEnd);
panelLogin.add(EndingField);
panelLogin.add(labelDuration);
panelLogin.add(DurationField);
panelLogin.add (labelA);
panelLogin.add(labelB);
panelLogin.add(labelC);
panelLogin.add(labelD);
panelLogin.add(labelTotal);
panelLogin.add(AField);
panelLogin.add(BField);
panelLogin.add(CField);
panelLogin.add(DField);
panelLogin.add(TotalField);
panelLogin.add(labelSafety);
panelLogin.add(labelSpeed);
panelLogin.add(labelEfficiency);
panelLogin.add(SpeedField);
panelLogin.add(EfficiencyField);
panelLogin.add(SafetyField);
message.setBounds(50, 70, 500, 20);
textFieldBegin.setBounds(200, 120, 100, 20);
labelBegin.setBounds(50, 120, 120, 20);
EndingField.setBounds(200, 140, 100, 20);
labelEnd.setBounds(50, 160, 120, 20);
DurationField.setBounds(200, 160, 100, 20);
labelDuration.setBounds(50, 140, 120, 20);
AField.setBounds(200, 180, 100, 20);
labelA.setBounds(50, 180, 120, 20);
BField.setBounds(200, 200, 100, 20);
labelB.setBounds(50, 200, 120, 20);
CField.setBounds(200, 220, 100, 20);
labelC.setBounds(50, 220, 120, 20);
DField.setBounds(200, 240, 100, 20);
labelD.setBounds(50, 240, 120, 20);
TotalField.setBounds(200, 260, 100, 20);
labelTotal.setBounds(50, 260, 120, 20);
SafetyField.setBounds(200, 280, 100, 20);
labelSafety.setBounds(50, 280, 120, 20);
SpeedField.setBounds(200, 300, 100, 20);
labelSpeed.setBounds(50, 300, 120, 20);
EfficiencyField.setBounds(200, 320, 100, 20);
labelEfficiency.setBounds(50, 320, 120, 20);
buttonNext.setBounds(70, 350, 100, 20);
clear.setBounds(200, 350, 100, 20);
buttonNext.addActionListener(actionEvent);
clear.addActionListener(actionEvent);
frame.getContentPane().add(panelLogin);
//frame ends.
//Options frame starts.
panelOptions = new JPanel();
panelOptions.setLayout(null);
PlacementResult = new JRadioButton ("Show the placement result.");
Collision = new JRadioButton ("Show the collision (%)");
buttonBack = new JButton("Back");
buttonNext = new JButton("Next");
panelOptions.add(buttonBack);
panelOptions.add(buttonNext);
panelOptions.add(Collision);
panelOptions.add(PlacementResult);
Collision.setBounds(150, 25, 200, 25);
PlacementResult.setBounds(150, 50, 200, 25);
buttonBack.setBounds(150, 100, 200, 25);
buttonNext.setBounds(150, 150, 200, 25);
buttonBack.addActionListener(actionEvent);
buttonNext.addActionListener(actionEvent);
//Options Frame ends.
panelCons = new JPanel();
panelCons.setLayout(null);
Mybutton = new JButton("Button");
panelCons.add(Mybutton);
Mybutton.setBounds(180, 180, 150, 25);
Mybutton.addActionListener(actionEvent);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
mydesign();
}
});
}
private static ActionListener actionEvent = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(frame == null)
frame = new JFrame();
else {
//remove the previous JFrame
frame.setVisible(false);
frame.dispose();
//create a new one
frame = new JFrame();
}
String action = e.getActionCommand();
if ("Login".equals(action)) {
System.out.println("Login action" + e.getActionCommand());
if ("".equals(textFieldBegin.getText()) || "".equals(EndingField.getText()) ||"".equals(DurationField.getText()) ) {
JOptionPane.showMessageDialog(null,
"Enter Both begin test date and end test date");
} else if ("admin".equals(textFieldBegin.getText())&&"admin".equals(AField.getText())&&
"admin".equals(BField.getText())&&
"admin".equals(CField.getText())&&"admin".equals(DField.getText())&& "admin".equals(TotalField.getText())&&
"admin".equals(EfficiencyField.getText()) && "admin".equals(SafetyField.getText()) &&
"admin".equals(SpeedField.getText()) && "admin".equals(EndingField.getText()))
{
System.out.println("Test begin date" + textFieldBegin.getText() + " Test end date" + EndingField.getText()
//+" Test duration" + (EndingField.getText() - textFieldBegin.getText())
+" Group A devices" + AField.getText() +" Group B devices"+ BField.getText()
+ " Group C devices "+ CField.getText()
+ "Group D devices"+ DField.getText()
+ "Total devices" + (AField.getText() + BField.getText() + CField.getText() + DField.getText())
+"Max speed (kbps)" + SpeedField.getText()
+ "Efficiency (%)" + EfficiencyField.getText() + "Safety (%)" + SafetyField.getText()
);
frame.remove(panelLogin);
frame.getContentPane().add(panelOptions);
frame.setVisible(true);
} else {
JOptionPane.showMessageDialog(null,
"Date format that you entered is not appropriate! Hint: YYYY/MM/DD");
}
} else if ("Cancel".equals(action)) {
System.out.println(e.getActionCommand());
System.exit(0);
}
{
frame.remove(panelOptions);
frame.getContentPane().add(panelCons);
frame.setVisible(true);
}
}
;
};
}
2nd problem is i want to make my program user friendly so i want to make a group every component that are similar each other.Here is output:
Advance thanks for all helps
SOLUTION :
Use TF(For 4 digits) + LB(For Slash) + TF(For 2 digits) + LB(For Slash) + TF(For 2 digits) to achive that format in date [TF : TextField and LB : Label] OR you can simply validate on Next button click whether date format match the specific format or not or even precisely date is valid or not.
Use JPanel and set border of it for grouping. (Hint : Use BorderFactory)
I didn't get you in this specific problem.SO can't say anything.
Related
So I want to store a string value in a variable, the value is given through a JTextField and after a confirm button is clicked, I want it to store what's written in the text field in a string variable.
This is the relevant part of the code:
public class Window {
private JButton confirm;
private JTextField textfield;
private JLabel label;
public void drawWindow() {
JFrame window = new JFrame("CountryQuiz");
ClickChecker click = new ClickChecker();
JPanel panel = new JPanel();
panel.setBounds(40, 80, 200, 200);
panel.setBackground(Color.green);
JTextField t1 = new JTextField("Enter country...");
t1.setBounds(50, 100, 200, 30);
window.add(t1);
JButton confirm = new JButton("Confirm");
confirm.setBounds(50, 50, 95, 30);
confirm.addActionListener(click);
window.add(confirm);
window.setSize(400, 400);
window.setLayout(null);
window.setVisible(true);
window.add(panel);
}
private class ClickChecker implements ActionListener {
public void actionPerformed(ActionEvent e) {
String answer = textfield.getText();
System.out.println(answer);
}
}
}
Results in the following error:
Cannot invoke "javax.swing.JTextField.getText()" because "this.this$0.textfield" is null
Your textfield variable is only declared but never used.
Replace the below
JTextField t1 = new JTextField("Enter country...");
t1.setBounds(50, 100, 200, 30);
window.add(t1);
With
textfield = new JTextField("Enter country...");
textfield.setBounds(50, 100, 200, 30);
window.add(textfield);
So I am creating a program that converts binary, hexadecimal, and decimal values to each other. So a mass converter for the three data types. I have completed all the algorithms for the conversion however, in my GUI I have encountered a problem. The problem is that my buttons do not respond to the being clicked and therefore does not launch the code within the action listener.
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public menu() {
GUI();
}
public static void main(String args[]) {
new menu();
}
private void GUI() {
JFrame main = new JFrame("Converter App");
main.setSize(1000, 1000);
main.setVisible(true);
main.setLayout(null);
JPanel main_Panel = new JPanel();
main_Panel.setBounds(5,5, 1000, 275);
main_Panel.setBackground(Color.gray);
main_Panel.setVisible(true);
main_Panel.setLayout(null);
JRadioButton binary = new JRadioButton("Binary");
JRadioButton hexadecimal = new JRadioButton("Hexadecimal");
JRadioButton decimal = new JRadioButton("Decimal");
JRadioButton binary_Fraction = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed = new JRadioButton ("Signed Decimal");
ButtonGroup input = new ButtonGroup();
input.add(binary); input.add(decimal); input.add(decimal_Fraction); input.add(decimal_Signed);
input.add(hexadecimal); input.add(binary_Fraction); input.add(binary_Signed);
JLabel input_Ins = new JLabel ("Please choose your input: ");
JLabel output_Ins = new JLabel ("Please choose your output: ");
binary.setBounds(15, 50, 150, 30);
binary.setBackground(Color.white);
binary_Fraction.setBounds(15, 100, 150, 30);
binary_Fraction.setBackground(Color.white);
binary_Signed.setBounds(15, 150, 150, 30);
binary_Signed.setBackground(Color.white);
hexadecimal.setBounds(90, 200, 150, 30);
hexadecimal.setBackground(Color.white);
decimal.setBounds(180, 50, 150, 30);
decimal.setBackground(Color.white);
decimal_Fraction.setBounds(180, 100, 150, 30);
decimal_Fraction.setBackground(Color.white);
decimal_Signed.setBounds(180, 150, 150, 30);
decimal_Signed.setBackground(Color.white);
input_Ins.setBounds( 70, 10, 200, 20);
input_Ins.setBackground(Color.white);
input_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
input_Ins.setOpaque(true);
output_Ins.setBounds( 575, 10, 200, 20);
output_Ins.setBackground(Color.white);
output_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
output_Ins.setOpaque(true);
JRadioButton binary_Out = new JRadioButton("Binary");
JRadioButton hexadecimal_Out = new JRadioButton("Hexadecimal");
JRadioButton decimal_Out = new JRadioButton("Decimal");
JRadioButton binary_Fraction_Out = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed_Out = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction_Out = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed_Out = new JRadioButton ("Signed Decimal");
ButtonGroup output = new ButtonGroup();
output.add(binary_Out); output.add(binary_Fraction_Out); output.add(binary_Signed_Out); output.add(hexadecimal_Out);
output.add(decimal_Out); output.add(decimal_Fraction_Out); output.add(decimal_Signed_Out);
binary_Out.setBounds(500, 50, 150, 30);
binary_Out.setBackground(Color.white);
binary_Fraction_Out.setBounds(500, 100, 150, 30);
binary_Fraction_Out.setBackground(Color.white);
binary_Signed_Out.setBounds(500, 150, 150, 30);
binary_Signed_Out.setBackground(Color.white);
hexadecimal_Out.setBounds(600, 200, 150, 30);
hexadecimal_Out.setBackground(Color.white);
decimal_Out.setBounds(700, 50, 150, 30);
decimal_Out.setBackground(Color.white);
decimal_Fraction_Out.setBounds(700, 100, 150, 30);
decimal_Fraction_Out.setBackground(Color.white);
decimal_Signed_Out.setBounds(700, 150, 150, 30);
decimal_Signed_Out.setBackground(Color.white);
main.add(main_Panel);
main_Panel.revalidate();
main_Panel.repaint();
main_Panel.add(binary); main_Panel.add(hexadecimal); main_Panel.add(decimal); main_Panel.add(binary_Fraction); main_Panel.add(input_Ins); main_Panel.add(output_Ins);
main_Panel.add(decimal_Signed); main_Panel.add(decimal_Fraction); main_Panel.add(binary_Signed);
main_Panel.add(binary_Out); main_Panel.add(hexadecimal_Out); main_Panel.add(decimal_Out); main_Panel.add(binary_Fraction_Out);
main_Panel.add(decimal_Signed_Out); main_Panel.add(decimal_Fraction_Out); main_Panel.add(binary_Signed_Out);
JPanel work = new JPanel();
work.setBounds(0, 300, 600, 400);
work.setBackground(Color.gray);
JButton instructions = new JButton("Instructions");
JLabel user_Input = new JLabel("Please insert an appropriate input: ");
JLabel user_Output = new JLabel("Your converted output is: ");
JLabel instructions_For_Instructions = new JLabel("Please click on the instructions button first.");
JTextField user_in = new JTextField();
JLabel user_out = new JLabel();
JButton source = new JButton("Sources");
JButton convert = new JButton("Convert");
instructions.setBounds(400, 5, 150, 50);
instructions.setBackground( Color.white);
user_Input.setBounds(150, 125, 300, 50);
user_Input.setFont(new Font("Serif", Font.PLAIN, 20));
user_Input.setBackground(Color.white);
user_Input.setOpaque(true);
user_Output.setBounds( 150, 250, 300, 50 );
user_Output.setFont(new Font("Serif", Font.PLAIN, 20));
user_Output.setBackground(Color.white);
user_Output.setOpaque(true);
user_in.setBounds(500, 125, 200, 50);
user_in.setBackground( Color.white);
user_in.setFont(new Font("Serif", Font.PLAIN, 20));
user_in.setDocument(new JTextFieldLimit(10));
user_out.setBounds(500, 250, 200, 50);
user_out.setBackground( Color.white);
user_out.setOpaque(true);
source.setBounds(880, 5, 100, 50);
source.setBackground(Color.white);
instructions_For_Instructions.setBounds(100, 10, 270, 25);
instructions_For_Instructions.setBackground(Color.WHITE);
instructions_For_Instructions.setOpaque(true);
convert.setBounds(750, 200, 100, 50);
convert.setBackground(Color.white);
System.out.println("Yes");
convert.addActionListener(this);
binary.addActionListener(this);
source.addActionListener(this);
main.add(work);
work.add(instructions); work.add(user_Input); work.add(user_Output); work.add(user_in);
work.add(user_out); work.add(source); work.add(instructions_For_Instructions); work.add(convert);
work.setVisible(true);
work.setSize(1000, 400);
work.setLayout(null);
main.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == source) {
JOptionPane.showMessageDialog(null, "YES");
}
if (e.getSource() == convert) {
user_out.setText("Yes");
//binary_To_Dec_Converter();
}
}
public void binary_To_Dec_Converter() {
if (binary.isSelected() ) {
String input_From_User = user_in.getText();
String converted = Integer.toString(Binary_Converter.Binary_To_Dec(input_From_User));
System.out.println(converted);
user_out.setText("YES");
}
}
}
im doing a java vending machine os and I've just imported my original project into eclipse and added a guy page and since then its been throwing errors everywhere no mater what i do, can i get some help? the main error now is 'Syntax error on token(s), misplaced construct(s)' i do apologise in advance if the code is bad or inefficient of scraps laying around.
package JavaOS;
class OS {
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;
public class OS extends Frame {
ImageIcon ironman3 = new ImageIcon ("H:\\School\\TECH\\Javaimages\\ironman3");
ImageIcon dredd = new ImageIcon ("H:\\School\\TECH\\Javaimages\\dredd");
ImageIcon indiana = new ImageIcon ("H:\\School\\TECH\\Javaimages\\indiana");
ImageIcon startrek = new ImageIcon ("H:\\School\\TECH\\Javaimages\\startrek");
public static String newline = System.getProperty("line.separator");
private static final long serialVersionUID = 1L;
private static Object activate;
String movie = null;
static boolean rightCreditCard = false;
public static void main(String[] args) throws IOException {
slot.d(activate);
beginProgram();
Welcome_GUI();
System.out.println("Part A - Intalising JFrame Windows");
checkCard();
if (rightCreditCard == false) {
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is invalid!");
checkCard();
} else {
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is valid!");
}
}
//Scanner scanner = new Scanner(System.in);
//System.out.println(scanner.nextLine());
//PrintWriter out = new PrintWriter(new FileWriter("H:\\School\\TECH\\JavaFileOutputs\\outputfile.txt"));
//out.print("Hello ");
//out.println("world");
//out.close();
//BufferedReader in = new BufferedReader(new FileReader("H:\\School\\TECH\\JavaFileOutputs\\outputfile.txt"));
//String text = in.readLine();
//in.close();
//System.out.println(text);
public static void beginProgram() {
Object[] options = { "OK", "Cancel" };
int JOP_Start = JOptionPane.showOptionDialog(null,
"Do you want to begin program?",
"Start?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (JOP_Start == JOptionPane.NO_OPTION)
{
System.out.println("Program Terminated");
System.exit(0);
}
}
public static void checkCard() {
String number = JOptionPane.showInputDialog("Enter your creditcard");;
int sum1=0,sum2=0;
//find the first Sum
for(int i=number.length()-1;i>=0;i=i-2)
{
sum1 = sum1 + Character . getNumericValue( number . charAt(i));
}
//find the second sum
for(int i=number.length()-2;i>=0;i=i-2)
{
int doublenumber = 2 * Character . getNumericValue( number.charAt(i));
String doublestring = Integer.toString(doublenumber);
//System.out.println(doublenumber+ " "+doublestring);
for(int j=0;j<doublestring.length();j++)
{
//System.out.println( Character . getNumericValue( doublestring.charAt(j)));
sum2 = sum2 + Character . getNumericValue( doublestring.charAt(j)) ;
}
}
//System.out.println(sum1+" "+sum2);
//Check the result
if((sum1+sum2)%10 == 0) {
System.out.println("Valid Credit Card!");
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Your Credit Card is valid!");
boolean rightCreditCard = true;
}
else
{
System.out.println("Invalid Credit Card!");
boolean rightCreditCard = false;
//Suggesting right check digit
System.out.println(sum1+sum2);
int totalsum = sum1+sum2;
int lastdigitoftotalsum = totalsum%10;
int numbertoaddtocheckdigit = 10 - lastdigitoftotalsum;
int userenteredcheckdigit = Character . getNumericValue( number . charAt(number.length()-1));
int progsuggestedcheckdigit = userenteredcheckdigit + numbertoaddtocheckdigit;
System.out.println(lastdigitoftotalsum + " " + numbertoaddtocheckdigit + " " + userenteredcheckdigit + " "+ progsuggestedcheckdigit);
System.out.println("The check digit should be " + progsuggestedcheckdigit);
////////////////////////////
//CREDIT CARD CHECK /\ END//
////////////////////////////
}
}
public static void rentMovie() {
Object[] options = { "OK", "Cancel" };
int accept = JOptionPane.showOptionDialog(null,
"Are you shoure you wish to rent this movie?",
"???",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[0]);
if (accept == JOptionPane.NO_OPTION)
{
System.out.println("You have not rented moive");
}
if (accept == JOptionPane.YES_OPTION)
{
System.out.println("You have rented moive");
slot.d(activate);
System.out.println("Slot dispenser activated");
}
}
public static void overDue() {
}
public static void delay(int i) {
try { //a wait or delay
Thread.sleep(i);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public static void Welcome_GUI() {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new JPanel());
frame.pack();
frame.setVisible (true);
class MyPanel extends JPanel {
private JLabel jcomp1;
private JLabel jcomp2;
private JButton jcomp3;
private JLabel jcomp4;
private JButton jcomp5;
private JButton jcomp6;
private JButton jcomp7;
private JLabel jcomp8;
private JLabel jcomp9;
private JLabel jcomp10;
private JLabel jcomp11;
private JLabel jcomp12;
private JLabel jcomp13;
private JLabel jcomp14;
private JLabel jcomp15;
private JLabel jcomp16;
public MyPanel() {
//construct components
jcomp1 = new JLabel (" Welcome To Video Pro 2000-XD");
jcomp2 = new JLabel (" Iron Man 3");
jcomp3 = new JButton ("Rent");
jcomp4 = new JLabel ("Dredd 3D");
jcomp5 = new JButton ("Rent");
jcomp6 = new JButton ("Rent");
jcomp7 = new JButton ("Rent");
jcomp8 = new JLabel ("Indiana Jones");
jcomp9 = new JLabel ("Star Trek");
jcomp10 = new JLabel ("Into Darkness");
jcomp11 = new JLabel ("Kindom Of the ");
jcomp12 = new JLabel ("Crystal Skull");
jcomp13 = new JLabel ("PIC _STAR_TREK");
jcomp14 = new JLabel ("PIC_INDINA_JONES");
jcomp15 = new JLabel ("PIC_IRONMAN3");
jcomp16 = new JLabel ("PIC_DREDD_3D");
//adjust size and set layout
setPreferredSize (new Dimension (1241, 746));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
add (jcomp5);
add (jcomp6);
add (jcomp7);
add (jcomp8);
add (jcomp9);
add (jcomp10);
add (jcomp11);
add (jcomp12);
add (jcomp13);
add (jcomp14);
add (jcomp15);
add (jcomp16);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (285, 35, 210, 35);
jcomp2.setBounds (280, 170, 80, 30);
jcomp3.setBounds (280, 205, 100, 25);
jcomp4.setBounds (400, 170, 70, 30);
jcomp5.setBounds (400, 205, 100, 25);
jcomp6.setBounds (280, 580, 100, 25);
jcomp7.setBounds (400, 565, 100, 25);
jcomp8.setBounds (280, 500, 100, 25);
jcomp9.setBounds (400, 510, 100, 25);
jcomp10.setBounds (400, 535, 100, 25);
jcomp11.setBounds (280, 525, 100, 25);
jcomp12.setBounds (280, 550, 100, 25);
jcomp13.setBounds (530, 400, 220, 326);
jcomp14.setBounds (40, 400, 220, 326);
jcomp15.setBounds (40, 40, 220, 326);
jcomp16.setBounds (530, 40, 220, 326);
}
}
}
}
You have
class OS {
and also
public class OS extends Frame {
The first one should be removed.
Other than that, the object 'slot' was not declared and initialized.
After removing
class OS {
and two lines of
slot.d(activate);
your file should compile and run.
Following is my Java Swing applet code in which I have first field as employee code and text box for entering employee code, I want to retrieve data of employee from MySql database when I will enter employee code in text box after hitting Enter key on keyboard and set retrieve data in respective text boxes and combo boxes. and same to embed it in JSP as applet.
Please help.
Here is my code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
public class App extends JApplet{
public void start()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");
String data []={"A","AB","B","B +","A +","O +","O -"};
JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
//pin.setBounds(10, 110, 100, 25);
//pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
//button2.setBounds(10, 70, 75, 25);
c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);
c.add(but_per);
//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}
public void stop(){}
}
In your ActionListener for that button retrieve the text of the textfield using textfield.getText() and then use this value to query database. I'm assuming that you know well how to query database.
Wait! you haven't implemented ActionListener!
Do the following:
but_per.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
/// write your code here!
String a= field.getText();
System.out.print(a);
///or DB code
}
});
Here is a LINK if you aren't familiar with Action Listener
UPDATE:
If you want all processing after a key press, you have to implement keybord events,
you can find a tutorial link here
This source is the closest workable version of your code I could produce, but it still has many serious problems.
Using the start() method incorrectly. The method will be called each time the browser is restored from minimized.
Use of a null layout and setBounds().
A free floating, non modal GUI element will cause focus problems..
Having said that, type a number in the employee code field and hit enter to see something like (screenshot shortened vertically)..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* <applet code=App width=400 height=600></applet> */
public class App extends JApplet {
public void start() {
final JFrame frame = new JFrame("Form");
ActionListener doDB = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "Query the DB off the EDT!");
}
};
// This cannot be done in a sand-boxed applet
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// hides the bottom field..
//frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
Container c;
c = frame.getContentPane();
c.setLayout(null);
JLabel name = new JLabel("Name :");
JLabel compcode = new JLabel("Company Code :");
JLabel cardno = new JLabel("Card Number: ");
JLabel cardtype = new JLabel("Card Type :");
JLabel pin = new JLabel("Pin :");
JLabel bldgrp = new JLabel("Blood Group :");
JLabel empcode = new JLabel("Employee Code :");
JLabel dob = new JLabel("DOB :");
JLabel valupto = new JLabel("Valid Upto :");
JLabel jdate = new JLabel("Joining Date :");
JLabel dept = new JLabel("Department :");
JLabel uid = new JLabel("UID :");
String data[] = {"A", "AB", "B", "B +", "A +", "O +", "O -"};
JTextField nametxt = new JTextField(10);
JComboBox compcodetxt = new JComboBox();
JTextField cardnumtxt = new JTextField(10);
JTextField cardtypetxt = new JTextField(10);
JTextField pintxt = new JTextField(10);
JComboBox bldgrptxt = new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt = new JTextField(10);
empcodetxt.addActionListener(doDB);
JTextField dobtxt = new JTextField(10);
JTextField valuptotxt = new JTextField(10);
JTextField jdatetxt = new JTextField(10);
JTextField depttxt = new JTextField(10);
JTextField uidtxt = new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
c.add(name);
c.add(nametxt);
c.add(compcode);
c.add(compcodetxt);
c.add(cardno);
c.add(cardnumtxt);
c.add(pin);
c.add(pintxt);
c.add(bldgrp);
c.add(bldgrptxt);
c.add(empcode);
c.add(empcodetxt);
c.add(dob);
c.add(dobtxt);
c.add(valupto);
c.add(valuptotxt);
c.add(jdate);
c.add(jdatetxt);
c.add(dept);
c.add(depttxt);
c.add(uid);
c.add(uidtxt);
c.add(but_per);
frame.setSize(350, 400);
frame.setVisible(true);
}
public void stop() {
}
}
Is there any code that I can use to pass value of selected radio button to another frame?
This is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class bookBatman extends JFrame implements ActionListener {
private JLabel jlbName, jlbTime, jlbPic, jlbDate, jlbDescription, jlbAuthor, jlbDateProduce, jlbDirector, jlbActor, jlbRate, jlbNoOfTicket, jlbPrice, jlbTotal;
private JTextField jtfNoOfTicket;
private JRadioButton jr1, jr2, jr3, jr4, jr5, jr6, jr7, jr8, jr9, jr10;
private JButton jTotal, jBook, jCancel;
Font f = new Font("Times",Font.BOLD,30);
public bookBatman () {
setLayout(null); //set LayoutManager
// initialized the label
jlbName = new JLabel ("Batman The Dark Knight");
jlbTime = new JLabel ("Time :");
jlbPrice = new JLabel ("RM 9.00");
jlbPic = new JLabel ();
jlbPic.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\Batman.jpg"));
jlbTotal = new JLabel (" Total : RM 9.00");
// add all the label on the frame
add(jlbName);
add(jlbPic);
add(jlbTime);
add(jlbPrice);
add(jlbTotal);
// set all the label positions
jlbName.setBounds(85, 78, 300, 18); //(int x, int y, int width, int height)
jlbPic.setBounds(74, 101, 180, 288);
jlbTime.setBounds(74, 400, 60, 18);
jlbPrice.setBounds (270, 477, 60, 18);
jlbTotal.setBounds (339, 475, 300, 22);
// initialized the textfield
jlbAuthor = new JLabel ("Directed by Christopher Nolan");
jlbDateProduce = new JLabel ("Date : 17 July 2008");
jlbDirector = new JLabel ("Author : Jonathan Nolan, Christopher Nolan");
jlbActor = new JLabel ("Main Actor : Christian Bale");
jlbRate = new JLabel ("Movie Rate : 13 PG (Parental Guidance)");
jlbNoOfTicket = new JLabel ("Number of Ticket :");
// add all the textfield on the frame
add(jlbAuthor);
add(jlbDateProduce);
add(jlbDirector);
add(jlbActor);
add(jlbRate);
add(jlbNoOfTicket);
// set the textfield position
jlbAuthor.setBounds (273, 102, 300, 18);
jlbDateProduce.setBounds (273, 132, 300, 18);
jlbDirector.setBounds (273, 162, 300, 18);
jlbActor.setBounds (273, 192, 300, 18);
jlbRate.setBounds (273, 222, 300, 18);
jlbNoOfTicket.setBounds (77, 478, 150, 18);
// initialize the Radio Button
jr1 = new JRadioButton ("11.40 AM");
jr2 = new JRadioButton ("12.00 PM");
jr3 = new JRadioButton ("1.40 PM");
jr4 = new JRadioButton ("3.40 PM");
jr5 = new JRadioButton ("5.40 PM");
jr6 = new JRadioButton ("7.00 PM");
jr7 = new JRadioButton ("9.00 PM");
jr8 = new JRadioButton ("10.40 PM");
jr9 = new JRadioButton ("11.40 PM");
jr10 = new JRadioButton ("12.40 AM");
// add all the radion button
add(jr1);
add(jr2);
add(jr3);
add(jr4);
add(jr5);
add(jr6);
add(jr7);
add(jr8);
add(jr9);
add(jr10);
// set the radion button positions
jr1.setBounds (75, 423, 100, 24);
jr2.setBounds (172, 423, 100, 24);
jr3.setBounds (269, 423, 100, 24);
jr4.setBounds (366, 423, 100, 24);
jr5.setBounds (463, 423, 100, 24);
jr6.setBounds (75, 447, 100, 24);
jr7.setBounds (172, 447, 100, 24);
jr8.setBounds (269, 447, 100, 24);
jr9.setBounds (366, 447, 100, 24);
jr10.setBounds (463, 447, 100, 24);
// group the button
ButtonGroup group = new ButtonGroup ();
group.add(jr1);
group.add(jr2);
group.add(jr3);
group.add(jr4);
group.add(jr5);
group.add(jr6);
group.add(jr7);
group.add(jr8);
group.add(jr9);
group.add(jr10);
jr1.setActionCommand("radio1"); // for ButtonGroup
String sel = group.getSelection().getActionCommand();
// initialize all the button
jTotal = new JButton ("Total");
jBook = new JButton ("Book Now");
jCancel = new JButton ("Cancel");
// add all the button
add (jTotal);
add (jBook);
add (jCancel);
// set the button positions
jTotal.setBounds (191, 519, 83, 28);
jBook.setBounds (285, 519, 93, 28);
jCancel.setBounds (389, 519, 83, 28);
// add actionlistener
jTotal.addActionListener (this);
jBook.addActionListener (this);
jCancel.addActionListener (this);
// initialize all text field
jtfNoOfTicket = new JTextField (15);
// add all the text field
add (jtfNoOfTicket);
// set the text field positions
jtfNoOfTicket.setBounds (200, 477, 56, 22);
}
public void actionPerformed(ActionEvent e){
if((e.getSource() == jTotal)) {
double price = 12.00;
double number = (Integer.parseInt(jtfNoOfTicket.getText().trim()));
double total = 0.0;
total = price * number;
jlbTotal.setText(" Total : RM" + total +"0");
}
if((e.getSource() == jBook)) {
String name = jlbName.getText ();
String date = jlbDateProduce.getText ();
String time = jr1.getText ();
int number = (Integer.parseInt(jtfNoOfTicket.getText().trim()));
String total = jlbTotal.getText ();
String price = jlbPrice.getText ();
//Passing
ticketReservation frame = new ticketReservation(name, date, time, price, total, String.valueOf(number));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Ticket Reservation"); //set title of the window
frame.setSize(800,600); //size of the window
frame.setVisible(true); //visible the window
frame.setLocationRelativeTo (null); //center the window
}
if((e.getSource() == jCancel)) {
listOfMovies frame = new listOfMovies ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("List of Movies"); //set title of thewindow
frame.setSize(800,600); //size of the window
frame.setVisible(true); //visible the window
frame.setLocationRelativeTo (null); //center the window
}
}
public static void main (String [] args) {
bookBatman frame = new bookBatman ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Book Batman : The Dark Knight"); //set title of thewindow
frame.setSize(800,600); //size of the window
frame.setVisible(true); //visible the window
frame.setLocationRelativeTo (null); //center the window
}
}
Don't think of it as passing information from one GUI to another, but rather think of it in its most basic OOP form: as passing object state from one object to another. Often we use public accessor methods (i.e., "getter" methods) for this purpose and this can work here too.
Your ButtonGroup object will hold the ButtonModel of the selected JRadioButton (or null if none are selected) and so you can get the information from the model and return it from your getter method.
As an aside, your code has a lot of redundancies that can be reduced by using arrays and by using appropriate layout managers.
edit 1:
For Example
Say we create a JPanel that holds a bunch of JRadioButtons:
import java.awt.GridLayout;
import javax.swing.*;
class RadioBtnDialogPanel extends JPanel {
private static final String[] BUTTON_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private ButtonGroup buttonGroup = new ButtonGroup();
public RadioBtnDialogPanel() {
setLayout(new GridLayout(0, 1)); // give JPanel a decent layout
// create radio buttons, add to button group and to JPanel
for (String buttonText : BUTTON_TEXTS) {
JRadioButton radioBtn = new JRadioButton(buttonText);
radioBtn.setActionCommand(buttonText); // set the actionCommand here
buttonGroup.add(radioBtn);
add(radioBtn);
}
}
// getter or accessor method to get selected JRadioButton's actionCommand text
public String getSelectedButtonText() {
ButtonModel model = buttonGroup.getSelection();
if (model == null) { // no radiobutton selected
return "";
} else {
return model.getActionCommand();
}
}
}
We also give it a public getter method that queries the state of the ButtonGroup to find out which button model has been selected and then return its actionCommand, a String that holds the text that describes the radio button (here it's the same as the text of the radio button).
We can then show this JPanel in a JOptionPane in our main GUI and after the JOptionPane is done, query the object above by calling its getSelectedButtonText() method:
import java.awt.event.*;
import javax.swing.*;
public class RadioButtonInfo extends JPanel {
private RadioBtnDialogPanel radioBtnDlgPanel = new RadioBtnDialogPanel();
private JTextField textfield = new JTextField(10);
public RadioButtonInfo() {
JButton getDayOfWeekBtn = new JButton("Get Day Of Week");
getDayOfWeekBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getDayOfWeekActionPerformed();
}
});
textfield.setFocusable(false);
add(getDayOfWeekBtn);
add(textfield);
}
private void getDayOfWeekActionPerformed() {
// display a JOptionPane that holds the radioBtnDlgPanel
int result = JOptionPane.showConfirmDialog(this, radioBtnDlgPanel, "Select Day Of Week", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) { // if use presses "OK" get the selected radio button text
// here we call the getter method to get the selected button text
String selectedButtonText = radioBtnDlgPanel.getSelectedButtonText();
textfield.setText(selectedButtonText); // and put it into a JTextField
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("RadioButtonInfo");
frame.getContentPane().add(new RadioButtonInfo());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}