• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing background color form1 from form 2

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am building a gui for my project

I want to change background color from form 2
actually I want to apply dark mode from settings from form2

I made all the component of form 1 public so that I can access it in form2

Interface_Home is my form1 and bluePaneLeft is a panel left side containing some menu items like home about settings
I want to change the bg of bluePanelLeft to dark mode
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid abbreviations like “bg”, which are difficult for non‑native‑English speakers to understand.
Please don't put _ characters in your class names. The _ should be reserved for CONSTANT_NAMEs and some _package.names.

obaid abbassi wrote:. . . I made all the component of form 1 public so that I can access it in form2 . . .

That sounds a bad idea. You should have an application program with an interface and that interface links to the displays. Whenever something happens that can cause a method to be called to change the colour of one of your displays. Creating a new instance in such a method won't work; you will change the colour of something completely different, which in this instance you can't even see.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not getting your point why this logic is not working
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe somebody else can explain it better. Look at the code you have shown us; you are not affecting the object you think you are.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one can explain things better than Campbell,  

What I like to know is how 'obj' (= new Interface_Home()) gets a reference to bluePaneleft.

Can you show us the code of the Interface_Home-class? (by the way: Interface_Home is a bad name for a class).
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should i post pictures of code?
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



this is made public in Interface_Home class so that it can easily be accessed in settings_user class


below code is in settings_user class trying to access bluePaneLeft jPanel

                             
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

obaid abbassi wrote: . . . this is made public . . .

I have already told you that is a mistake.

. . .

At this stage, please don't allow NetBeans to write any code for you. Write everything by hand, until you are much more experienced. Please space and indent the code correctly.
Creating a new instance is a mistake, as I have told you twice already.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

obaid abbassi wrote:should i post pictures of code?

Please don't post any pictures, but seeing the code may allow us to find more errors. It depends how long it is.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works for me:

Don't forget to drag each Interface_Home-instance to some other location, otherwise they will all overlap.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, Piet, that is different from what OP wants. You are cerating a new display and the old display remains, with its red panel.Changes in lines 20, 27, and 39. Because I didn't redeclare the display, the original display changes to grey and there is no old red display to confuse things.
Of course calling a red panel in the centre bluePaneLeft confused me severely
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . .

Try something like this:-. . .You can expand that toThat gets you out of making the fields public.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Unfortunately, Piet, that is different from what OP wants. You are cerating a new display and the old display remains, with its red pane


Op talks about changing colors in form2 from form1, that's why OP made that bluePane public and that's why I created another frame to illustrate the point.

But anyway, let's wait for comment from OP, what actual problem he (or she) is facing.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am unable to deliver my point correctly!
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can I send you a complete zip of my project
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

obaid abbassi wrote:can I send you a complete zip of my project

No. Always keep all discussions on the forum, where everybody can help you.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for better understanding, I am attaching pictures.
blueLeft is menu I want to change its color from setting menu which is completely different class that's why I m creating new object in settings class
Capture.JPG
[Thumbnail for Capture.JPG]
image_2022-07-13_183857020.png
[Thumbnail for image_2022-07-13_183857020.png]
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

obaid abbassi wrote:for better understanding, I am attaching pictures.
blueLeft is menu I want to change its color from setting menu which is completely different class that's why I m creating new object in settings class

I don't know about anybody else but I'm finding the language barrier to be a bit of an issue for me. Instead of trying to explain things in English sentences we have a couple of options:
1) Supply an SSCCE (Simple Self Contained Compilable Example). Probably about 100 lines of code that we can compile and run.
2) Pseudo code
3) Bullet points
4) UML

 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above pictures, the form1 contains all menus

the setting is also part of the form1 but on clicking the icon of settings
a new form2 is opens. Now we are in form2 .now we created a button for the dark mode to change the colour of form1

so how logic will work am I understandable now?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, when you create form2, give it a reference to form1. Give form1 a method 'public setBackground(Color newColor)' and in the actionListener of that button in form2, just do: referenceToForm1.setBackground(...);
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when you create form2, give it a reference to form1.


That could be done by using the this variable.  In form1, use code like:
The Form2 constructor:
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not working this one too!
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not working this one too!
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





//this is the code of form2 in setting_user and creating a reference of form1
here I m passing color but background is not changing why?





 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code needs to use a reference to the existing form1 instance.  It should not be creating a new instance of form1.

Can you make a small complete program that compiles and executes for testing?

It is very hard to see what is happening with the small bits of code that were posted.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
form1 code





form2 complete code


 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a missing class for me:
org.netbeans.lib.awtextra.AbsoluteLayout()
Can you get rid of the usage of that class?  I do not use netbeans.

Both classes have a main method.  Which one should be executed to do the test?
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
form1 has a primary main class to start
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 64 creates a new instance of Form1.  It should use the existing instance of Form1.  Form2 should receive a reference to Form1 when Form1 creates the instance of Form2.

To see what your code does add this code at line 65:

See my post from 2 days ago.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


are you refering to this code

this is creating new form2 code.
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
although i created a public Colorchange method in form 1 but not working
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

are you refering to this code

Where is that code from?
No.  Please reread my post to see that I referred to line 65.
form1 is declared and given a value on line 64.

but not working


I think it is working,  but that instance of Form1 is  not visible.  Add the call to setVisible to see
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

       

this is working but creating a new form
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some one will give you the code.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Obaid,

these are the changes you must do in your code to get things running:

1) in class Form2 change the beginning to this:

2) also in Form2: change the method 'darkActionPerformed' to:


3) in your class 'Form1' change the method 'button1MouseClicked' to

and all should work
 
obaid abbassi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much problem got solved!
One of the best forums to get help.

 
reply
    Bookmark Topic Watch Topic
  • New Topic