How can I set background Color of JTextField in Java?

View previous topic View next topic Go down

User verified solution How can I set background Color of JTextField in Java?

Post by BIT0208-Shuvo on Mon Apr 04, 2011 5:48 pm

I've got some problems while setting the background color of a JTextField . I think the following code should work for setting the background color of the JTextField .But It doesn't work . I also search for this in the Internet but I found the same thing which doesn't work .


Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Timer extends JFrame implements Runnable {

   private static final long serialVersionUID = 1L;

   Container container;
   Thread t;
   JTextField txtfield;
   JButton startButton, stopButton;

   public Timer() {

      setSize(300, 300);
      setLayout(null);
      setLocationRelativeTo(null);

      startButton = new JButton("Start");
      startButton.setBounds(80, 200, 80, 30);
      add(startButton);

      stopButton = new JButton("Stop");
      stopButton.setBounds(165, 200, 80, 30);
      add(stopButton);
      
      t = new Thread(this);

      txtfield = new JTextField(16);
      txtfield.setBounds(80, 100, 140, 50);
      txtfield.setBackground(Color.RED);
      add(txtfield);

      txtfield.setEditable(false);
      txtfield.setText("00 : 00 : 00 : 00");
      txtfield.setFont(new Font("Time New Romans", Font.BOLD, 15));
      

      startButton.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {

            t.start();

         }
      });

      stopButton.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {

            t.stop();
         }
      });

      setVisible(true);
   }

   public static void main(String[] args) {

      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (InstantiationException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IllegalAccessException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (UnsupportedLookAndFeelException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      new Timer();
   }

   int i = 0;
   String s;

   @Override
   public void run() {

      for (int i = 0;; i++) {
         for (int v = 0; v < 10; v++) {
            for (int j = 0; j < 6; j++) {
               for (int b = 0; b < 10; b++)
                  for (int k = 0; k < 6; k++) {
                     for (int n = 0; n < 10; n++) {
                        for (int l = 0; l < 10; l++) {
                           for (int m = 0; m < 10; m++) {
                              String s1, s2, s3, s4, s5, s6, s7, s8;

                              s1 = Integer.toString(i);
                              s2 = Integer.toString(j);
                              s3 = Integer.toString(k);
                              s4 = Integer.toString(l);
                              s5 = Integer.toString(m);
                              s6 = Integer.toString(n);
                              s7 = Integer.toString(b);
                              s8 = Integer.toString(v);

                              txtfield.setText(s1+s8 + " : " + s2+s7
                                    + " : " + s3+s6 + " : " + s4+s5);
                              try {
                                 Thread.sleep(10);
                              } catch (InterruptedException e) {
                                 // TODO Auto-generated catch block
                                 e.printStackTrace();
                              }

                           }
                        }
                     }
                  }
            }
         }
      }
   }
}



Please anyone help me with the solution of this problem. Question


Last edited by BIT0208-Shuvo on Tue Apr 05, 2011 12:13 am; edited 2 times in total
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0122-Amit on Mon Apr 04, 2011 5:56 pm

Can you please provide the full code? Just this segment may not give details about the problem.

Also, the title is a bit confusing. I didn't realize whether you are discussing about how to set background color in JTextField or you are discussing why you can not set background color in JTextField.


_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0208-Shuvo on Mon Apr 04, 2011 6:12 pm

I think now both the tittle and the code is is self explanatory , isn't it ?? Very Happy


Last edited by BIT0208-Shuvo on Mon Apr 04, 2011 11:43 pm; edited 1 time in total
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0122-Amit on Mon Apr 04, 2011 6:28 pm

This is the output I am seeing, which shows the color? Neutral



_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0208-Shuvo on Mon Apr 04, 2011 6:51 pm

But It's not working with my IDE . Now I'm not understanding the problem scratch
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0208-Shuvo on Mon Apr 04, 2011 7:02 pm

Just now I've got the point , the problem was in the look and fill which is the system look and fill. I think , the system look and fill in linux doesn't support the background color of the JTextField .
Now the problem is solved.
Thank you Amit vai for helping me and
rep++
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0122-Amit on Mon Apr 04, 2011 7:22 pm

Most welcome Smile You can add the word solved in your top post in a bold letter so that it may not create confusion. No need to edit the title though :p

BTW, IDE is not exactly an issue unless you change the run configurations manually. IDE just helps you write the code, and things like that. Running.. is based on your compiler and the code.


_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0102-Mohaimin on Mon Apr 04, 2011 7:41 pm

Good to see that you have figured that out yourself. Smile
I have an observation that is not related to your problem.

You have declared some objects like:
Code:
Container container;
  Thread t;
  JTextField txtfield;
My problem is with the variable names. They does not tell us what they do.
This should be like you have named the following two:
Code:
JButton startButton, stopButton;
For sure you start and stop the timer with these two.

Another thing is, swing already defines a Timer class that works with ActionListener. You can use that one to create your timer. Be careful, Java defines more than one Timer classes. Choose the one that is in the swing package.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0102-Mohaimin on Mon Apr 04, 2011 8:02 pm

Again, Thread.stop() is deprecated. It may do devastating things in your program. There is the deprecation statement of the methods Javadoc:
/ *This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
* ThreadDeath exception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
* other threads, potentially resulting in arbitrary behavior. Many
* uses of stop should be replaced by code that simply
* modifies some variable to indicate that the target thread should
* stop running. The target thread should check this variable
* regularly, and return from its run method in an orderly fashion
* if the variable indicates that it is to stop running. If the
* target thread waits for long periods (on a condition variable,
* for example), the interrupt method should be used to
* interrupt the wait.
* For more information, see
* Why are thread.sTop, Thread.suspend and Thread.resume Deprecated?.
*/
Dont ask me what all these mean. I dont know really.

You can do these:

Code:
startButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            t = new Thread(Timer.this);
            t.start();
        }
      });
Create a boolean flag named threadRunning which should be true when the thread is running, false otherwise.
In the innermost for loop in the run() add this:
Code:
if(!threadRunning){
  return;
}

And in action listener of the stopButton, make the threadRunning false.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0208-Shuvo on Mon Apr 04, 2011 11:37 pm

BIT0102-Mohaimin wrote:
You have declared some objects like:
Code:
Container container;
  Thread t;
  JTextField txtfield;
My problem is with the variable names. They does not tell us what they do.
This should be like you have named the following two:
Code:
JButton startButton, stopButton;
For sure you start and stop the timer with these two.

You see, there is only one object for each of the two defined types . That's why I've not used the explanatory name of the objects . We use the explanatory name when there is a number of different objects of similar types for making the work more easier while doing some other things with the objects which I've used in the buttons .But thank you to inform me that , I will try to do these everywhere Smile .
Though you have tried to give an explanation of deprecation , I've not understand it well . But again thank you for your effort and rep++
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0122-Amit on Tue Apr 05, 2011 3:49 pm

Erm, by "we", I will assume you are talking about your classmates perhaps.

But good coding involves writing understandable variable names even if you use just one variable.

For example,

Code:
JTextField txtfield;

Does NOT explain why you inserted a text Field, unless we run the code. or go through the whole code line by line and figure it out our self.

But,

Code:
JTextField displayTimeTextField;
At least gives us an idea that it displays time.

remember, we don't write codes for compilers, we code codes for Programmers Smile The more understandable it is, the more clean code it will be Smile

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by adamricky on Tue Aug 02, 2011 4:09 pm

Must it be JavaScript?

A stylesheet works fine, if not.



And then:

input type="text" id="input_box"

adamricky
Pre-Alpha Release
Pre-Alpha Release

Course(s) :
  • N/A

Blood Group : NULL
Posts : 5
Points : 5

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by BIT0102-Mohaimin on Tue Aug 02, 2011 7:45 pm

adamricky wrote:Must it be JavaScript?

A stylesheet works fine, if not.



And then:

input type="text" id="input_box"

No man, its a Java desktop application. Stylesheet has nothing to do here.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by phoniex05 on Fri Sep 02, 2011 9:43 pm

You can get good examples from sites like
roseindia.net
java2s.com

jtextfield you can call the static import of color class to give it.


phoniex05
Pre-Alpha Release
Pre-Alpha Release

Course(s) :
  • N/A

Blood Group : NULL
Posts : 3
Points : 3

View user profile http://www.orionedconservices.com/

Back to top Go down

User verified solution Re: How can I set background Color of JTextField in Java?

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum