Unable to disable frame button from JDialog - java

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
}

Related

JSliders only sliding halfway

I have a number JSliders in my application and I'm wondering as to why they only slide to half way.
public class test1 extends javax.swing.JFrame {
public test1() {
initComponents();
jSlider1.setExtent(255);
jSlider1.setValue(-255);
}
#SuppressWarnings("unchecked")
private void initComponents() {
jSlider1 = new javax.swing.JSlider();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
}
private javax.swing.JSlider jSlider1;
}
Using setExtent or setMaximum would seem to work the same but setExtent makes it so that the slider only slides half way. The proper code looks like this:
public class test1 extends javax.swing.JFrame {
public test1() {
initComponents();
jSlider1.setMaximum(255);
jSlider1.setValue(-255);
}
#SuppressWarnings("unchecked")
private void initComponents() {
jSlider1 = new javax.swing.JSlider();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
}
private javax.swing.JSlider jSlider1;
}
You should set jslider1.setMinimum(-255) and jslider1.setMaximum(255) if your intention is to create range from -255 to 255.
You can also implement ChangeListener interface and use addChangeListener to add the listener to the slider. You can do something in the method stateChanged(ChangeEvent e) e.g. updating the RGB color.

How can I effect to another thread? (for example: to setSelect the checkbox)

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.

Saving Data on a new JTable (new JFrame) using NetBeans GUI builder

I'm very new to java which is why I'm using NetBeans GUI builder, basically I've created a JFrame which has two components and I'm able to save the data of two text fields and use a submit button to put this into a JTable thats in the JFrame. But I've created a new JFrame specifically to hold the JTable. so one JFrame has two textfield and a submit button, and another JFrame as a JTable. below is the code I used when I had the JTable, button and two textfield in one JFrame. How would I go about trying to save data into a different JFrame containing only JTable?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{/*some stuff here ignore for this question*/});
}
One way to update table from Frame2 with values from text fields from Frame1 is to use the observer pattern. Frame1 will have a list of observers which need to be updated once the observable (Frame1) inserts or has new values. I will add the code to be able to understand this better. Also, have a look at the Observer Pattern.
Let's define an Observable interface (these are all the methods that an Observable needs to implement)
public interface Observable {
public void addObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObserver(String[] row);
}
Let's define Frame1 which will be the Observervable
public class Frame1 extends javax.swing.JFrame implements Observable{
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JButton submitButton;
private List<Observer> observers = new ArrayList<>();
public Frame1() {
initComponents(); // 2 text fields and 1 button
}
private void initComponents() {
// I will skip this part you can generate it with NetBeans
// Basically initialise jTextField1, jTextField2, and submitButton
}
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
String[] row = {jTextField1.getText(), jTextField2.getText()};
notifyObserver(row);
}
#Override
public void addObserver(Observer o) {
observers.add(o); // subscribe new observer
}
#Override
public void removeObserver(Observer o) {
observers.remove(o); // unsubscribe new observer
}
#Override
public void notifyObserver(String[] row) {
for (Observer observer: observers) {
observer.update(row); // notify all observers that new row values are available
}
}
}
Also, let's define an Observer interface (these are all the methods that an Observer needs to implement)
public interface Observer {
public void update(String[] row);
}
Let's define Frame2 which will be the Observer
public class Frame2 extends javax.swing.JFrame implements Observer {
private javax.swing.JTable jTable1;
public Frame2() {
initComponents();
}
private void initComponents() {
// I will skip this part you can generate it with NetBeans
// Basically initialise jTable1
}
public void addRow(String column1, String column2){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{column1, column2});
}
#Override
public void update(String[] row) {
addRow(row[0], row[1]);
}
}
Now, let's wrap everything and test:
public class Main {
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Frame2 frame2 = new Frame2();
Frame1 frame1 = new Frame1();
// Register frame2 as an observer of frame1
frame1.addObserver(frame2);
frame1.setVisible(true);
frame2.setVisible(true);
}
});
}
}

Setting the text in jTextField and then retrieving that text

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());
}

spinner image on JFrame while UI are creating

I have an application that after successfull login (on a JFrame), starts to create the main frame (class MainUI that extends from JFrame). That MainUI class contains a JTabbedPane (which each tab is a class that extends from JPanel) and, on setVisible method, creates and shows each tab.
I want to add on the login form, after successfull login, a Spinner image to indicate that the MainUI is being created.
After display the Spinner image, I invoke the creation of the MainUI and call the setVisible method on EventQueue.invokeLater(); but the Spinner image is not updated. If I use new Thread(runner).start(); is updated, but I get a lot of Component creation must be done on Event Dispatch Thread
Some code of Login.java:
buttonLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
login();
}
});
private void login()
{
//check DB and permissions
//if all is ok
lMsj.setIcon(spinner);
new Thread(new Runnable() {
public void run() {
showMainUI(usr);
}
}).start();
}
private void showMainUI(final Usuario usr)
{
Runnable runner = new Runnable() {
public void run() {
final MainUI mui = new MainUI();
mui.setVisible(true);
dispose();
}
};
EventQueue.invokeLater(runner);
}
and some code of MainUI.java
public MainUI()
{
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
setMinimumSize(new Dimension(1280, 960));
createComponents();
}
});
}
private void initComponents()
{
//..
// menuItem = new ...
// ...
}
#Override
public void setVisible(boolean value)
{
//..
if (Security.get().isAllowed("tab1")){
addTab1();
}
//..
}
private void addTab1(){
//..
getTabbedPane().addTab("Tab1", new Tab1());
//..
}
How I can fix this, so that the image is updated and the user interface is created in the "background"?

Categories