Passing variable value to another tab (JPanel) - java

I was looking for the solution, but the problem still remains. I'm designing a GUI and trying to change the variable by clicking a button:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
var=(String)jComboBox2.getSelectedItem();
}
Then i want to use it to change label in another tab, which already exist, but it always says null (value of the variable is not updated even though it is defined in the most-upper class.
I was trying with:
jLabel4.revalidate();
jLabel4.repaint();
but it also fails.
Ok, I will explain it better :D I have a button defined as follows:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
var=(String)jComboBox2.getSelectedItem();
jLabel4.revalidate(); //actually makes no difference if i delete this 2 lines...
jLabel4.repaint(); }
And I want to use this variable in another tab (JPanel):
jLabel4.setText("Studenci z przedmiotu " + var);
but the variable var always remains null as defined in the mother class (this is quite logical, because all components are initiated at the start-up, before pressing the button...). But do you know why functions repaint and revalidate do not work? How can I substitute them? sry for not posting the whole code, but i think it will be too much spam ;d

if you want change a Text variable use setText() Method in gui
For Example:
jlable4.setText("new text");
and put it in your action like this:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jlable4.setText("new text");
}
if that was your question,Enjoy!if not give me a peace of code and tell me what is error message and i'be help you.

Related

NetBeans: Making a JTextField disapear

I'm trying to make an interface for a login/register app and I have, in the email box (jTextField) an example as text (example#gmail.com) but when I run my program when I click that box to write my email on it, I have to delete my set text to write what I want.
What I thought to do was to create 2 jTextFields, the one behind not editable and the one forward where I'd put my text. So there are two things I don't know how to do:
put the forward jTextField invisible so we can see the behind
one
make the text on the behind jTextField disappear when I click the front one
Thanks for trying the help.
Can easily done with FocusGained and focuseLost events
private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {
if (txtEmail.getText().equals("example#example.com")) {
txtEmail.setText(null);
}
}
private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {
if ( txtEmail.getText().equals("")) {
txtEmail.setText("example#example.com");
}
}

How to show names in Jtextfield click on JButton using Netbeans

I am fresher in Netbeans. I have a small assignment related Netbeans. I want to show some names in JTextfield(for example "abcd","ijkl","mnop" etc as next by next) and it must show one by one. I had create some code which not working properly. I am looking some helps related to it. Please mention as full code here that I can understand in future.I do not know how to create it. So i am done it some another ways. But showing error message. My code is as follows.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s[]={"abcd","ijkl","mnop" } ;
for(int d=0; d<s.length;d++)
jTextField1.setText(""+s);
}
Both these variables should be inside of your class but outside of any method.
String s[]={"abcd","ijkl","mnop" } ;
int i=0;
In action performed method
if(i>=s.length)
i=0;
jTextField1.setText(s[i]);
i++;
For the first click, display abcd.
Next click, display ijkl.
Next click, display mnop.
But I didn't check it. Just try it. It will works.

Why doesn't the value passed to a method of a different class reflect in Java (using NetBeans 7.01)?

Before I start, Hi. This is is my first question here. I am not good with Java so have been trying and improve that and here it goes.
I am trying to create an email client and server application using sockets in Java. However I have been running into a problem. I have created a jFrame which is basically the Welcome window. The code is too huge to post so I'll post the relevant portions. There is a preferences jDialog. When the OK button on the dialog, an action handler comes in to play. The code:
private void okActionPerformed(java.awt.event.ActionEvent evt) {
Welcome wel = new Welcome();
wel.setStatusBar("Pressed OK");
dispose();
}
Obviously, the setStatusBar() sets the text of the statusLabel. The code for setStatusBar():
public void setStatusBar(String s)
{
statusLabel.setText(s);
}
Also, the preferences dialog is opened through menu item with this code:
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
settings pref=new settings(null,true);
pref.show();
}
The problem is if I set the status label from any other class, for instance settings class, it does not reflect but if I do so from the Welcome class ( the class where the statusLabel is present), it works fine. This problem is not only limited to this setStatus() but virtually pops up whenever I try to use a method of a different class.
If you guys need more of the code, I could post it. I would be grateful if could help a Java beginner out.
Thanks.
private void okActionPerformed(java.awt.event.ActionEvent evt) {
Welcome wel = new Welcome();
wel.setStatusBar("Pressed OK");
dispose();
}
You're creating a new (hence the keyword new) object of type Welcome. This new object is different from the already existing object of type Welcome, that you have created earlier. It thus has its own label, and you're setting the text of this different label, which is not displayed anywhere in the screen.
Java objects work like regular object. Let's say you would like a cool logo on one of your blue t-shirts. You go to a T-shirt vendor and ask him to print a cool logo. The vendor doesn't have your blue t-shirt. If the vendor gets another red t-shirt from his shop and prints the logo on this red t-shirt, your blue t-shirt will still have no logo at all.
For the vendor to be able to print a logo on your blue t-shirt, you need to give him this blue t-shirt. Same in Java: you need to pass the existing Welcome object to the preferences dialog, and the actionPerformed method must set the label on this Welcome object. Not on a new Welcome object.

Understanding button/mouse listener in Netbeans GUI generated code

can any tell me what this part of code is doing?
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
jButton1MouseClicked(evt);
}
});
why are there methods in the parameter for the method addMouseListener? can some one explain in details? im using netbeans and this is code generated.
It's an anonymous MouseAdapter, meaning it calls a new class instance without a variable/assignment. The code itself is calling a new custom event handling method jButton1MouseClicked(), which is what netbeans generates for you so that you can add in your own handling for the code.
Yes, don't add a mouse listener to a button. Well, I use this method to get the position of X and Y mouse click in my GUI using NetBeans.
Right-click the swing container: Choose Events: choose Mouse: choose MouseClicked. Add the following piece of code; (of course my text fields have the variable names tfMouseX and tfMouseY).
tfMouseX.setText(evt.getX() + "");
tfMouseY.setText(evt.getY() + "");

JLabel does not update when using setText method

In the project I am currently working on I have several pieces of information that I would like to make visible via Jlabel. There are a few buttons and textfields elsewhere in the GUI that allow for altering said information I would like to update the JLabel but the text never changes, or updates upon startup.
I have attempted to use concurrency to update the labels, as suggested in other questions on this site, but I have had no luck with the labels updating. The concurrency does work with updating textfields and comboBoxes as needed.
The current iteration of my code looks as follows,
The JFrame
//// This is a snippet from the JFrame
public void start()
{
this.setSize(900, 700);
this.setVisible(true);
devicePanel.populateDeviceDefinitions();
updateServiceInfo();
updateCommandInfo();
startUpdateTimer();
}
public void updateServiceInfo()
{
EventService service = JetstreamApp.getService();
generalPanel.updateServiceInfo(service.getBaseUri(),
service.getAccessKey(), String.valueOf(service.getWindowTime()));
}
public void updateCommandInfo()
{
JetstreamServiceClient client = JetstreamApp.getClient();
generalPanel.updateCommandInfo(client.getBaseUri(), client.getAccessKey());
}
The JPanel named generalPanel
//// This is a snippet from the generalPanel
//// All of the variables in the following code are JLabels
public void updateServiceInfo(String baseUrl, String accessKey,
String windowTime)
{
serviceUrl.setText(baseUrl);
serviceAccessKey.setText(accessKey);
serviceWindowTime.setText(windowTime);
}
public void updateCommandInfo(String baseUrl, String accessKey)
{
commandUrl.setText(baseUrl);
commandAccessKey.setText(accessKey);
}
The labels start with an Empty string for their text and upon window start it is intended that they be updated by grabbing the information from the relevant sources. Can I please have some insight as to why the JLabels never update and display their information?
How did you create the JLabel? If the text starts out as "", and you've created it with new JLabel(""), the width of the JLabel may be initialized to 0 and then none of your text would show up when you update it. I believe I've had that sort of problem in the past. As a test, try using new JLabel("aaaaaaaaaa") or some longer string to create the label, then setText(""); then later, when you setText(somethingElse), see if that causes text to show up. If it does, then the width is probably the problem and you can work on it from there. – ajb 19 mins ago
This comment is the actual answer, when creating a JLabel with an empty string as the text the label's dimensions do not get set properly when using WindowBuilderPro. My labels did exist, and were being updated with the code provided in my question but the labels were not visible.
Starting with a label that has text in it, then setting the text to an empty string works properly.
The method paintImmediately() can be used to cause a Swing component to get updated immediately. after setText(), you should call paintImmediately() like below.
jLabel.setText("new text")
jLabel.paintImmediately(jLabel.getVisibleRect());
You should try to call revalidate() or repaint() on the component that contains your JLabels.
Cheers

Categories