About Jcheckbox state, java - java

I am trying to do an action on button being clicked, but i need to make a check whether JCheckBox is checked or not.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
public class RandomPassword extends JFrame{
RandomPassword(String s){
super(s);
setSize(300,300);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
setLayout(null);
setFont(new Font("Serif", Font.PLAIN, 20));
Label l1 = new Label("Введите количество символов:");
l1.setBounds(50, 100, 200, 30);
add(l1);
JTextField tf1 = new JTextField(1002);
tf1.setBounds(50,130,200,30);
add(tf1);
JTextArea ta1 = new JTextArea();
ta1.setPreferredSize(new Dimension(150,30));
ta1.setBounds(50,210,230,30);
add(ta1);
JCheckBox ch1 = new JCheckBox("Использовать заглавные буквы");
ch1.setBounds(50, 0, 200, 30);
add(ch1);
JCheckBox ch2 = new JCheckBox("Использовать цифры");
ch2.setBounds(50, 30, 200, 30);
add(ch2);
JCheckBox ch3 = new JCheckBox("Использовать спецсимволы");
ch3.setBounds(50, 60, 200, 30);
add(ch3);
JButton b1 = new JButton("Сгенерировать");
b1.setBounds(75, 170, 150, 30);
add(b1);
b1.addActionListener(new Action());
}
public static void main(String[] args){
new RandomPassword("Генератор случайных паролей");
}
static class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
}
I want to make a Checkbox in static class Action, but he is throwing me an exception. What do i have to do?
Trying this one didn`t help me.
JButton b1 = new JButton(new AbstractAction("Сгенерировать") {
public void actionPerformed(ActionEvent e) {
ch1.isSelected();
}
});

You can access to the cliked JCheckBox with:
((JCheckBox)e.getSource())

The way your program is structured your check boxes are not in the scope of the actionPerformed method. One way to counter this would be to use an anonymous inner class directly in the contructor.
final JCheckBox ch3 = new JCheckBox("Использовать спецсимволы");
...
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(ch3.isSelected());
}});
Note that this way you need to make the check box variable final, so it can be accessed in the inner class. You can then use the isSelected method to check whether the check box is currently selected.
As an unrelated note, better put the call to setVisible(true) at the end of the constructor, otherwise it seems like some GUI elements are not drawn correctly.

Related

How to pass inputted data from JDialog to parent JFrame when user clicks button in JDialog?

How do I pass user-inputted date from a JDialog to the parent JFrame when the user clicks a certain button in the JDialog?
Here's how I want the program to work: When the user clicks a button in the JFrame, a JDialog pops up. The user then enters some data of various types (string and integer). If the user clicks an "Add Task" button, the data is passed back to the original JFrame, which will display the data, and the JDialog closes. If the user clicks the "Cancel" button, the data is discarded and the JDialog closes.
I thought about using JOptionPane, but I don't think it allows for data of various types. I thought about creating a method in the JFrame and calling it from the JDialog, but I don't know how to reference the JFrame. I thought about creating a variable in the JDialog, but I don't know to stop the JDialog from immediately passing an empty variable to the JFrame.
Any help?
Code for JFrame:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Dialog.ModalityType;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainInterface extends JFrame {
private JPanel contentPane;
public MainInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton addTask = new JButton("Add");
addTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
NewTask newTask = new NewTask();
newTask.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Set window title
newTask.setTitle("Add Task");
newTask.setVisible(true);
}
});
addTask.setBounds(0, 728, 97, 25);
contentPane.add(addTask);
JButton modifyTask = new JButton("Modify");
modifyTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
modifyTask.setBounds(95, 728, 97, 25);
contentPane.add(modifyTask);
JButton deleteTask = new JButton("Delete");
deleteTask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
deleteTask.setBounds(190, 728, 97, 25);
contentPane.add(deleteTask);
JButton settingMenu = new JButton("Settings");
settingMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Setting settings = new Setting();
settings.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
settings.setVisible(true);
settings.setTitle("Settings");
}
});
settingMenu.setBounds(285, 728, 97, 25);
contentPane.add(settingMenu);
}
}
The JFrame is launched by another class, so it doesn't have a main method.
Code for JDialog:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.omg.CORBA.PUBLIC_MEMBER;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
import java.awt.event.ActionListener;
import java.util.jar.Attributes.Name;
import java.awt.event.ActionEvent;
public class NewTask extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField taskName;
public NewTask() {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblNewLabel = new JLabel("Name:");
lblNewLabel.setBounds(24, 13, 38, 16);
contentPanel.add(lblNewLabel);
}
{
taskName = new JTextField();
taskName.setBounds(79, 10, 304, 22);
contentPanel.add(taskName);
taskName.setColumns(10);
}
JLabel lblNewLabel_1 = new JLabel("Time Required:");
lblNewLabel_1.setBounds(24, 58, 97, 16);
contentPanel.add(lblNewLabel_1);
JSpinner hourSpinner = new JSpinner();
hourSpinner.setBounds(125, 55, 44, 22);
contentPanel.add(hourSpinner);
JLabel lblNewLabel_2 = new JLabel("hours");
lblNewLabel_2.setBounds(175, 58, 44, 16);
contentPanel.add(lblNewLabel_2);
// Set maximum value for minutes to 59
int min = 0;
int max = 59;
int step = 1;
int i = 1;
SpinnerModel value = new SpinnerNumberModel(i, min, max, step);
JSpinner minuteSpinner = new JSpinner(value);
minuteSpinner.setBounds(225, 55, 44, 22);
contentPanel.add(minuteSpinner);
JLabel lblNewLabel_3 = new JLabel("minutes");
lblNewLabel_3.setBounds(281, 58, 56, 16);
contentPanel.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("Deadline:");
lblNewLabel_4.setBounds(24, 108, 56, 16);
contentPanel.add(lblNewLabel_4);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton addButton = new JButton("Add Task");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
addButton.setActionCommand("OK");
buttonPane.add(addButton);
getRootPane().setDefaultButton(addButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Close dialog window
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
I thought about creating a method in the JFrame and calling it from
the JDialog, but I don't know how to reference the JFrame.
Even though this could be a nice solution, referencing the object who called the method is very discouraged.
See why (first answer): How to find the object that called a method in Java
One possible approach would be to create a method in the JFrame and adding a JFrame as a parameter for your newTask() function. Then, when invoking newTask() you pass this as an argument: newTask(this); .
Inside the modified newTask(JFrame frame) method, you just use the argument passed to reference the method in the parent JFrame.
This might seem the same as referencing the object who called the method, but it's not. Here you are passing as an argument the parent JFrame, which may not always be the object which invoked the method.
I hope I was clear enough, have a nice day!

Issues with ActionListener

I'm trying to write a program that asks you to click on a button to generate a random number and then makes the user try to guess what that number is.
Right now I'm working on getting a button click to generate a random number and to tell the user that the random number has been generated through a JTextPane. For whatever reason this part of the code doesn't seem to be executing correctly.
I have already instanced the object in the main class and I've added an actionlistener to the button. I have no idea what's causing this not to work appropriately:
package GUIs;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Principal extends JFrame {
//Elements
private JPanel contentPane;
private JTextField attemptField;
private JTextPane hint;
private JButton btnGenerate;
private JButton btnEnter;
//Events
private GenerateRandomNumber genRanNum;
//Misc variables
int ranNum = 1;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Principal frame = new Principal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Principal() {
//Generates JPanel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Generates text field where the user will write the number
attemptField = new JTextField();
attemptField.setBounds(130, 59, 130, 26);
contentPane.add(attemptField);
attemptField.setColumns(10);
//Generates the hint
hint = new JTextPane();
hint.setBackground(SystemColor.window);
hint.setText("First, generate a new number!");
hint.setBounds(130, 110, 192, 19);
contentPane.add(hint);
//Generates the button that should call for the creation of the new number
btnGenerate = new JButton("Generate number");
btnGenerate.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
btnGenerate.setBounds(165, 153, 117, 29);
contentPane.add(btnGenerate);
btnGenerate.addActionListener(genRanNum);
//Generates button that will be used to enter the number
btnEnter = new JButton("Enter");
btnEnter.setBounds(269, 59, 58, 29);
contentPane.add(btnEnter);
}
public class GenerateRandomNumber implements ActionListener {
public void actionPerformed(ActionEvent e) {
ranNum = (int) (Math.random() * 101);
hint.setText("The number has been created!");
}
}
}
private GenerateRandomNumber genRanNum;
Your random number generator is null. I don't see where you ever create an instance of it.
btnGenerate.addActionListener(genRanNum);
The above statement does nothing because the variable is null. (ie. no ActionListener has been added to the button)
I don't even know how your GenerateRandomNumber class would compile because it doesn't have access to the "hint" variable of your "Principal" class.
Don't make the GenerateRandomNumber class a public class. Instead make it an inner class of the "Principal" class.
Read the section from the Swing tutorial on How to Use Actions for an example showing how to use inner classes. Note you can even use an Action instead of an ActionListener.

How to create a lock with user input on JPanel?

I am a beginner at Java so if there are some simple problems that are in my code please tell me. Thank you in advance!
In Java I have a "login" JPanel which I want the user to enter in a password they like, then have them enter the password on another JPanel (the same password that they created). If it is right and they click the login button again then it would bring them to a screen which says "Welcome" and if they don't, then a screen that says "False. Error". However something on Eclipse is saying that something is wrong and that I can't run it. Please tell me where I went wrong and if you can tell me how to fix it. I would appreciate it!
There are two problems with my code. Both of them are "Syntax error, insert "}" to complete ClassBody" but every time I add one in, it says "Syntax error on token "}", delete this token"
Here is my code:
package Button;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.util.Scanner;
#SuppressWarnings("unused")
public class Login {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Page");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Pasword");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText);
JButton loginButton = new JButton("Create");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
#SuppressWarnings("resource")
Scanner user = new Scanner (System.in);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
loginButton.setVisible(false);
registerButton.setVisible(false);
userText.setVisible(false);
userLabel.setVisible(false);
JTextField userText1 = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText1);
JButton loginButton1 = new JButton("Password");
loginButton1.setBounds(10, 80, 80, 25);
panel.add(loginButton1);
});
loginButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String name = userText.getText();
String accept = name;
String good;
if (accept.equals(name)) {
good = "Welcome";
} else {
good = "False. Error";
}
JLabel label1 = new JLabel(good);
label1.setBounds(100, 40, 100, 100);
label1.setVisible(true);
panel.add(label1);
}
});
}
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
);
Is missing a closing embrace (}) - one thing you will learn to do is count brackets and braces
It should look more like
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
});
registerButton and loginButton1 are undefined and you seem to be missing closing brace (}) at the end of the file as well
I would also highly recommend that you make use of layout managers, they will make life a lot easier for you. I'd recommend starting with How to use CardLayout as it will allow you to switch between multiple views simply
Thank you for helping me fix the problem. In the end I was able to fix it and it now works. Here it is:
package Button;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.util.Scanner;
#SuppressWarnings("unused")
public class AddOnButton {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Page");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Password");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText);
JButton loginButton = new JButton("Create");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
String name = userText.getText();
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButton.setVisible(false);
userText.setVisible(false);
userLabel.setVisible(false);
JTextField userText1 = new JTextField(20);
userText1.setBounds(100, 10, 160, 25);
panel.add(userText1);
JLabel userLabel1 = new JLabel("Password");
userLabel1.setBounds(10, 10, 80, 25);
panel.add(userLabel1);
JButton loginButton1 = new JButton("Enter");
loginButton1.setBounds(10, 80, 80, 25);
panel.add(loginButton1);
String check = userText1.getText();
loginButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userText1.setVisible(false);
userLabel1.setVisible(false);
loginButton1.setVisible(false);
String good;
if (name.equals(check)) {
good = "Welcome";
} else {
good = "False. Error";
}
JLabel label1 = new JLabel(good);
label1.setBounds(100, 40, 100, 100);
label1.setVisible(true);
panel.add(label1);
}
});
}
});
}
}

Using an event to do something in another class in Java

based on my understanding,
`list_1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
}}
will do something when something in the list was selected, and
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
do something when the button have been pushed.
I want to write a code to delete the selected item from one list and add it to another one.
I can't use Jlist methods because it is not in the scope of button.
I am not sure how to do it. and I can't find something that solve my problem on net or books.
Thank you so much
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JList;
import java.awt.GridLayout;
import javax.swing.JSplitPane;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import javax.swing.JSeparator;
import java.awt.Panel;
import java.awt.List;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Color;
import java.awt.Font;
public class Window {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application. This is the constructor for this Window class.
* All of the code here will be executed as soon as a Window object is made.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame. This is where Window Builder
* will generate its code.
*/
public void initialize() {
//creates an array for the list of components
String pclist[]={"case","moderboard","CPU","GPU","PSU","RAM","HDD"};
frame = new JFrame();
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmLoad = new JMenuItem("Load");
mnFile.add(mntmLoad);
JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
frame.getContentPane().setLayout(null);
JButton button = new JButton(">>");
button.setBounds(244, 170, 82, 36);
button.setFont(new Font("Tahoma", Font.BOLD, 15));
frame.getContentPane().add(button);
JButton button_1 = new JButton("<<");
button_1.setBounds(244, 219, 82, 36);
button_1.setFont(new Font("Tahoma", Font.BOLD, 15));
frame.getContentPane().add(button_1);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 205, 493);
panel.setBackground(Color.WHITE);
frame.getContentPane().add(panel);
panel.setLayout(null);
JList list = new JList();// implements ActionListener;
list.setBounds(0, 0, 205, 493);
list.setListData(pclist); //populate the Jlist
list.setFont(new Font("Tahoma", Font.BOLD, 18));
panel.add(list);
JPanel panel_1 = new JPanel();
panel_1.setBounds(358, 0, 212, 493);
panel_1.setBackground(Color.WHITE);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JList list_1 = new JList();
list_1.setBounds(203, 0, -200, 480);
list_1.setSelectedIndex(0);
list_1.setFont(new Font("Tahoma", Font.BOLD, 18));
panel_1.add(list_1);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//list_1.addElement("hi");
System.out.println("hoi");
}
});
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent arg0) {
}});
list_1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
}
}
);
}
}
Start by making your JLists instance variables
public class Window {
private JFrame frame;
private JList list;
private JList list_1;
Make sure you're initialising the instance variables and not creating new local variables...
//JList list = new JList();// implements ActionListener;
list = new JList();// implements ActionListener;
//...
panel.add(list);
//JList list_1 = new JList();
list_1 = new JList();
//...
panel_1.add(list_1);
This now means that the JLists are accessible from within the context of an instance of the class...
Then in your ActionListeners, you can simple do something like...
Object selected = list.getSelectedValue();
or...
int index = list.getSelectedIndex();
You can then use these values to modify the state of the underlying ListModel...if it supports those operations...

Problem in passing value of JTable cell of one JFrame form to another form's JTextField

I have two JFrame Forms-SelectContactsfrm.java and Taskfrm.java. There is JTable in SelectContactsfrm file to show the contacts.When user select a contact from JTable and when he/she press OK button,selected values should be copied into Taskfrm.java's JTextField.
Taskfrm.java's JTextField's name is-txtContacts and access modifier is-public
Below is the code which I wrote on SelectContactsfrm's OK button's actionPerformed.Button name-btnOK
private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
// Code to get the selected rows value and paste Contact's full name in Taskfrm's txtContacts JTextField:
selrow=ctable.getSelectedRow();
selcol=ctable.getSelectedColumn();
Object value=ctable.getModel().getValueAt(selrow,1);
new Taskfrm().txtContacts.setText(value.toString());
//Just to check whether I get the correct values or not.
System.out.println("selrow=="+selrow);
System.out.println("selcol=="+selcol);
System.out.println("txtContacts=="+value);
}
I can see the correct selected values in output but didnt get why this value has not been set in Taskfrm's JTextField.In Taskfrm's constructor only initComponents() is there.Is there any way to attach class files here instead of pasting?
It depends on how Taskfrm is set up and what is in it's constructor.
I tried something like this and it works
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ClassA extends JFrame {
JTextField text;
public ClassA() {
JLabel l = new JLabel("Name: ");
text = new JTextField(20);
JButton b = new JButton("Send");
setLayout(null);
l.setBounds(10, 10, 100, 20);
text.setBounds(120, 10, 150, 20);
b.setBounds(120, 40, 80, 20);
add(l);
add(text);
add(b);
setVisible(true);
setSize(300, 100);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ClassB().text.setText(ClassA.this.text.getText());
}
});
}
public static void main(String a[]) {
new ClassA();
}
}
class ClassB extends JFrame {
JTextField text;
public ClassB() {
JLabel l = new JLabel("Name: ");
text = new JTextField(20);
setLayout(null);
l.setBounds(10, 10, 100, 20);
text.setBounds(120, 10, 150, 20);
add(l);
add(text);
setVisible(true);
setSize(300, 100);
}
}
set its visibility and made changes in Taskfrm's constructor.Now its working.
new Taskfrm(value);
new Taskfrm(value).setVisible(true);

Categories

Resources