package test2;
public class NewJFrame extends javax.swing.JFrame {
private static void valueGen(javax.swing.JTextField jTextField1) {
String x = jTextField1.getText();
System.out.println(x);
}
public NewJFrame() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
jTextField1.setText("Hello");
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
public javax.swing.JTextField getTextField() {
jTextField1.getText();
return this.jTextField1;
}
public static void main(String args[]) {
NewJFrame myFrame = new NewJFrame();
valueGen(myFrame.getTextField());
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
}
I have a program as shown above. I need to set the text "hello" in a text field when the submit button is clicked. It works. But then i need to use that text in a function called valueGen where it is printed. But the text doesn't get printed by executing the above code. What is wrong with this code?
It's unorganized. Always put class variables at the top.
Like Cool Guy said, put myFrame instead of new NewJTest()
valueGen is actually being called before you can click the button. Put it in the jTextField1ActionPerformed; that might fix it.
In netbeans out put of the System.out.println(); will be displayed in the output window as shown in the bellow picture.
If you want to display it as a message replace the following method with your valueGen() method.
private static void valueGen(javax.swing.JTextField jTextField1) {
String x = jTextField1.getText();
System.out.println(x);
JOptionPane.showMessageDialog(null, x);
}
And use myFrame.setVisible(true); to make visible your JFrame.
public void run() {
NewJFrame myFrame = new NewJFrame();
myFrame.setVisible(true);
valueGen(myFrame.getTextField());
}
Related
I created two "jFrame form" with netbeans. I can open the both same time. This codes run but no effect. I try to setSelect a checkbox in form2 with a button in form1. Somehow, I can't effect another thread. Could you please fix it for me. Thanks. (sorry my bad English, I am learning English, too)
This is form1.java (I removed some automatic codes)
package test;
import javax.swing.SwingUtilities;
public class form1 extends javax.swing.JFrame {
public form1() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new form2().setVisible(true);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
final form2 click = new form2();
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
click.jCheckBox1.setSelected(true);
}
}
);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
This is form2.java (I removed some automatic codes)
package test;
public class form2 extends javax.swing.JFrame {
public form2() {
initComponents();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form2().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JCheckBox jCheckBox1;
// End of variables declaration
}
In your jButton2ActionPerformed method, you're creating a new JFrame which is never shown. You're not manipulating the JFrame you displayed in jButton1ActionPerformed.
Try this:
package test;
import javax.swing.SwingUtilities;
public class form1 extends javax.swing.JFrame {
private form2 click = null; // this will hold the second JFrame (the one with the checkbox)
public form1() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (click == null) { // only create the second JFrame once
click = new form2(); // this stores the second JFrame with the name "click"
click.setVisible(true);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
if (click != null) { // don't do anything if the second JFrame isn't displayed yet
click.jCheckBox1.setSelected(true);
}
}
}
);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new form1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
BTW, it is bad form and very confusing for class names to start with a lower case letter. Rename form1 to Form1 and form2 to Form2.
I want to get the text of private access JTextArea from another class in the same package and store/save the text into a String.
public class JTextAreaDemo extends javax.swing.JFrame {
public JTextAreaDemo() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class();
d.readJtxtAreaText();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTextAreaDemo().setVisible(true);
}
});
}
private javax.swing.JTextArea jTextArea1;
/**
* #return the jTextArea1
*/
public String getjTextArea1() {
return jTextArea1.getText();
}
/**
* #param jTextArea1 the jTextArea1 to set
*/
public void setjTextArea1(javax.swing.JTextArea jTextArea1) {
this.jTextArea1 = jTextArea1;
}
Now I want to save the text of JTextArea to string in below class
public class TxtArea_Class {
JTextAreaDemo demo;
String txt;
public TxtArea_Class(){
demo = new JTextAreaDemo();
txt = new String();
}
public void readJtxtAreaText(){
txt = demo.getjTextArea1();
if(txt.isEmpty()){
System.out.println("Failed To Get TextArea Contents ");
}
else{
System.out.println("Successfully Get TextArea Contents ");
}
}
Console Output :
Failed to Get TextArea Contents
Problem is in your TextArea_Calss's constructor
try the following.
public TextArea_class(TextAreaDemo demo) {
this.demo = demo;
this.str = new String();
}
and in button event. do this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class(this);
d.readJtxtAreaText();
}
In current implementation, Every-time you create an instance of TextArea-calss a new frame get created. because in TextArea_Class constructor you are creating an new instance of demo class.
and you are trying to get value from newly created demoFrame(that might be invisible for you but exist).
I'm hoping this will solve your issue.
You have two different instances of JTextAreaDemo!! One created in main and made visible, the other created in TxtArea_Class. The first one is the one on the screen, and the second is the one you read the string from. So the text you enter into the first doesn't show in the second.
I got the contents of JTxtArea from another class by updating my code by this.
TextArea_class
public TextArea_class(TextAreaDemo demo) {
this.demo = demo;
this.str = new String();
}
JTxtAreaDemo
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
TxtArea_Class d = new TxtArea_Class(this);
d.readJtxtAreaText();
}
why doesn't this work correctly? i cant seem to find the problem
its suppose to increment the text display by one for each time the button is clicked
public class ClickerGame extends javax.swing.JFrame {
public ClickerGame() {
initComponents();
}
//declare
int clicks;
String clicksout = "" + clicks;
//Swing GUI netbeans code is here, removed because it is irrelevant
//click increments number by 1
private void clickActionPerformed(java.awt.event.ActionEvent evt) {
clicks++;
clickercounter.setText(clicksout);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ClickerGame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton click;
private javax.swing.JTextField clickercounter;
// End of variables declaration
}
You only set clicksout once:
String clicksout = "" + clicks;
The value isn't reset dynamically if clicks change. If you never change it, you'll always get the same result here:
clickercounter.setText(clicksout);
Try this instead:
clickercounter.setText("" + clicks);
You won't need clicksout as a separate variable.
I want to disable a button on the frame from a JDialog, I tried everything but it won't work. The program's execution starts from a frame and when the button is clicked the Dialog pops up. Simple, when you click the button on the dialog the frame's button should get disabled and the dialog will close.
BTW: everything works, its just the frame's button that does not get disabled!
PS: I am coding this on NetBeans so i have removed unnecessary coding for the sake of simplicity.
Here is the coding for the frame:
public class Frame extends javax.swing.JFrame {
Dialog D = new Dialog(this, true);
public Frame(){
setTitle("Frame");
initComponents();
setResizable(false);
}
void buttonDisable(){
Btn1.setEnabled(false);
}
private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {
D.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton Btn1;
// End of variables declaration
}
Here is the coding for the JDialog Box:
public class Dialog extends javax.swing.JDialog {
public Dialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setTitle("Dialog");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Frame().buttonDisable();
dispose();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
I haven't run this through an IDE. But I'm fairly confident that calling the buttonDisable() on a new Frame() instead of calling it on the actual parent frame is your problem.
You need to save your "parent" in your Dialog so you can access it later on and do something like this in your Dialog.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
parentFrame.buttonDisable();
dispose();
}
So your complete code would look like this:
public class Frame extends javax.swing.JFrame {
Dialog d = new Dialog(this, true);
public Frame(){
setTitle("Frame");
initComponents();
setResizable(false);
}
void buttonDisable(){
Btn1.setEnabled(false);
}
private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {
d.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton Btn1;
// End of variables declaration
}
and the dialog would look like this
public class Dialog extends javax.swing.JDialog {
private Frame parentFrame;
public Dialog(Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setTitle("Dialog");
this.parentFrame=parent;//hold reference to the parent
this.setVisible(true);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.parentFrame.buttonDisable();//invoke method on the parent reference
dispose();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
public class checkUsernames extends JFrame {
private static JTextArea textArea1;
private static JButton button1;
private static JScrollPane scrollPane1;
private static JTextField textField1;
private static JPasswordField passwordField1;
private static JLabel label3;
private static JButton button2;
private static JLabel label1;
private static JLabel label2;
public checkUsernames() {
initComponents();
}
public static void main(String[] args) throws IOException {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Nimbus isn't available");
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
checkUsernames GUI = new checkUsernames();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
String username = textField1.toString();
String password = passwordField1.toString();
Exception in thread "main" java.lang.NullPointerException
checkUsernames is the only class. When I try to run the application the program executes further (proceeding without String username & String password) without waiting for input. How can I fix this?
If you want the code execution to stop while waiting for input, make the JFrame a modal JDialog or JOptionPane instead.
Well invokeLater will invoke the code later. It dosent gurantee textfield1.toString() to execute after checkUserNames()
None of your private static members appear to be initialized at all. You won't get past the NullPointerException until you initialize each and every object you create before you use it.
I have no idea what you're talking about. You have to do something like this for all those objects:
private static JTextArea textArea1 = new JTextArea();
You aren't entering a username or password; you're creating a text area UI element that can accept them when you do enter them.
Why dont you use SwingUtilities.invokeAndWait() instead? This seems to solve your problem immediately.
Here is the simple example:
import javax.swing.SwingUtilities;
public class InvokeAndWaitExample {
public static void main (String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run () {
System.out.println("Hello World on " + Thread.currentThread());
}
});
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished on " + Thread.currentThread());
}
}