I have two text fields that I make invisible when the form is initialised.
What I want to happen is the following.
and then when the button is clicked, they appear like so.
I have tried making the text fields not visible when the form initialised then triggering a action performed event when the button is clicked making the text fields visible again.
import javax.swing.JFrame;
public class Weather extends javax.swing.JFrame {
public Weather() {
initComponents();
this.jTextField3.setVisible(false);
this.jTextField10.setVisible(false);
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(true);
this.jTextField3.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Weather().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
etc..
etc...
}
In C#, this method of making things visible and invisible works but the same logic doesn't apply to java. Nothing happens when I click the button. The two text fields just stay invisible.
When I don't make the text fields not visible when the form is initialised and make them invisible upon button click via the button clicked actionevent method, it works.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField10.setVisible(false);
this.jTextField3.setVisible(false);
}
Why can I only make the text fields invisible via button click but I can't make the text fields visible via button click?
That's probably because the window and the GUI was already created with the buttons invisible. When you just set them to visible true it doesn't know how to rewrite them. You can try:
this.revalidate(); //Here this being the jframe
this.repaint();
Related
I am working with JavaFX, and I have a Scene object I created in SceneBuilder with a bunch of Button objects. I want the text of each of these Button objects to be underlined when the mouse cursor enters their area in the window, and to remove the underlining when the cursor leaves.
I know I can just type:
public class Controller {
#FXML
private Button exitButton;
public void exitButtonMouseEntered() {
this.exitButton.setUnderline(true);
}
public void exitButtonMouseLeft() {
this.exitButton.setUnderline(false);
}
}
However, doing this for each Button object is time-consuming, and it seems like there must be an easier way to force all the Button objects to have the same behavior given the same conditions.
How can I make a controller method that will affect all the Buttons in the Scene?
Apply the following css:
Button:hover{
-fx-underline: true;
}
i'm at a standstill developing a java application.
I have a series of JFrames that open each other, and once I open the next, the frame before shall close.
Now, all works fine, I used
setVisible(false);
to "close" the frame, but I have some problems with a specific form I generated:
The form has some panels inside, and the panels each have a button which opens the next form. Now, I wonder how can I apply setVisible(false) to the form that contains those panels.
ChooseForm is the name of the form that contains the panels;
And I have this event handling the button click of each panel's button
private void btn_scegliMouseClicked(java.awt.event.MouseEvent evt) {
Database.getAbitazioneByCodice(label_codice.getText());
MainForm.showFrame();
}
Thanks in advance for the help.
There are many ways to do this.
A Simple way you can do like this:
class YourFrame extends JFrame implements ActionListener {
private YourPanel panel;
public YourFrame() {
//...
panel.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//show other Frame and hide this Frame
}
}
class YourPanel extends JPanel {
private JButton yourButton;
public void addButtonActionListener(ActionListener listener) {
yourButton.addActionListener(listener);
}
}
The situation is as follows:
My app consists of a dialog box containing x elements and a button. The user presses button after interacting with elements and if he interacted them with a specific way, only then the parent frame in which the Dialog Box resides should appear.
For this purpose, I currently know of this approach:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(false);
jDialog.setVisible(true);
}
});
}
And then add this command on Button which resides inside jDialog:
new NewJFrame().setVisible(true);
This does the trick quite well and neat, but the previous instance called using new NewJFrame().setVisible(false); is still running (as far as I know).
Isn't there anyway I could perform this action on button (residing inside jDialog) press as using something like:
NewJFrame.setVisible(true);
(It currently gives me error: Non-static method cannot be referenced from static context)
Make sure that the dialog is modal, and you can simply do:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame newJFrame = new NewJFrame();
newJFrame.pack();
// no need to set visible false. It already is
MyDialog myDialog = new MyDialog(newJFrame);
// make sure the super constructor makes the dialog modal
myDialog.pack();
myDialog.setVisible(true);
// here the dialog is no longer visible
// and we can extract data from it and send it to the JFrame if needed
newJFrame.setVisible(true); // ****** here
}
});
}
Otherwise if you absolutely must fiddle with the JFrame from within the JDialog, simply pass the NewJFrame into the JDialog's constructor, something that you need to do regardless since it should be used in the JDialog super constructor, use it to set a NewJFrame field, and call setVisible(true) on the instance inside of your dialog.
I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way.
I have done coding this way:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame form files
In the frame that is the launching program (we'll call it MyFrame1) add a button to it (we'll call it jButton1)
Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
}
In that actionPerformed, just instantiate the second frame (we'll call it MyFrame2) and setVisible(false) to MyFrame1. MyFrame2 should already be visible upon instantiation, so you wouldn't have to setVisisble(true) on it
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
MyFrame2 frame2 = new MyFrame2();
MyFrame1.this.setVisible(false);
// You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again
}
I think this should work
you need to setVisible Jframe2 as true...so it can apear on output sceen
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
create action event for the button such that when when you click will take
you
to the next page for my case next page is nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.
i have a two frame, frameA and frameB, in frameA i add one button with name buttonA to show frameB and progressbar (setIndeterminate(false)), in frameB i add one button with name buttonB , i want to when i click buttonB, progressbar in frameA.setindeterminate(true)
in frameA
frameB b;
public frameA() {
initComponents();
progressbar.setIndeterminate(false);
b = new frameB();
}
public JProgressBar getProgressbar() {
return progressbar;
}
private void buttonAActionPerformed(java.awt.event.ActionEvent evt)
{
b.setVisible(true);
}
in frameB
i use this code in event buttonB clicked
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
but it doesnt worked
This...
private void buttonBActionPerformed(java.awt.event.ActionEvent evt) {
frameA a= new frameA();
a.getProgressbar().setIndeterminate(true);
}
Isn't going to work, you've just created another instance of frameA that's not visible. It has no relationship to the frame that is currently open.
There are any number of ways you could achieve this...
You could...
Pass a reference of frameA to frameB as part of the constructor call for frameB. Then in you actionPerformed method, you would simply use this reference to change the progress bar state.
But this would create a tight coupling between frameA and frameB which would greatly reduce the re-use of frameB
You could...
Provide a means by which an interested party could attach an ActionListener to frameB which would be triggered when the button is clicked.
This assumes a work flow and exposes components to outside sources (via the ActionEvent#getSource), which could allow people to change your component...
You probably should...
Fire a PropertyChanged event.
This is probably the easiest and safest of all the options I've come up with. Using a property change listener in this manner means you don't need to expose the JProgressBar, the JButton or produce a tight link between the two frames.
Property change support is built in to Container so all components/controls have it.
For example, you would attach a PropertyChangeListener to b when you construct it.
b = new frameB();
b.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progressState")) {
progressbar.setIndeterminate((Boolean)evt.getNewValue());
}
}
});
Add in bFrame's actionPerformed method, you would simple call...
firePropertyChange("progressState", false, true);
When you want to set the indeterminate state (you could swap the boolean values to reset it if you wanted to)