?
Solved

easy question - Swing applet background color

Posted on 2003-03-09
15
Medium Priority
?
285 Views
Last Modified: 2013-11-23
I am trying to make a swing applet with a green background, the code fragment below doesn't work, it just displays the background with the default gray color. How to i make it work? This worked fine for a normal applet

import java.awt.*;
import javax.swing.*;
import java.awt.Color.*;

/*
<APPLET CODE = "swinger.class" WIDTH = 400 HEIGHT = 400>
</APPLET>
*/

public class swinger extends JApplet
{
     
     public void init()
     {
            getContentPane().setLayout(null);
         setSize(350,250);
         this.setBackground(Color.green);                  
     }

}
0
Comment
Question by:danBosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097541
Hi, this should work, but if you then add a JPanel on it, you must make the background af that panel green to!

I'm working on a applet that needs to be in same color as web page, and I must put the bacground color on every panel...

You can do it the same way

JPanel p = new JPanel();
p.setBackground(Clor.green);

let's hop this is the problem
0
 

Author Comment

by:danBosh
ID: 8097601
aye that should work but what about for labels,text fields,etc:

myLabel = new JLabel("text");
myLabel.setBounds(170,100,90,20);
myLabel.setBackground(Color.yellow);
getContentPane().add(myLabel);
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097606
No, I don't think you need to do that. Labels don't have any visible background, textfield, I think, have white background (not sure you wanna change that to)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:danBosh
ID: 8097613
so how do i change the colour of text fields and labels:
0
 

Author Comment

by:danBosh
ID: 8097616
so how do i change the colour of text fields and labels:
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097640
JTextField f = new JTextField();
f.setForeground(Color.red); //text color)
f.setBackground(Color.blue); //Field color)

JLabel l = new JLabel("Some label");
l.setForeground(Colro.red); //Text color)
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097647
You can create your own textfields and labels...

import javax.swing.*;
import java.awt.*;

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097680
You can create your own textfields and labels...

import javax.swing.*;
import java.awt.*;

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097724
You can create your own textfields and labels...

import javax.swing.*;
import java.awt.*;

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097728
Hmm, this is strange, I only posted this once... but, lucky number three! hehe
0
 

Author Comment

by:danBosh
ID: 8097952
?
but when i did this:
JLabel myLabel = new JLabel("text");
myLabel.setBounds(170,100,90,20);
myLabel.setBackground(Color.yellow);
getContentPane().add(myLabel);

it didnt work

so why would this work:
JLabel l = new JLabel("Some label");
l.setForeground(Color.red); //Text color)
l.setBackground(Color.blue); //Text color)  
?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097987
myLabel.setBackground(Color.yellow); //Nothing happends (no backround on labels)
myLabel.setForeground(Color.yellow); //Yellow Text
0
 

Author Comment

by:danBosh
ID: 8098004
sorry for being thick here, so it is impossible to change the color of textfields and labels in java swing?
0
 
LVL 2

Accepted Solution

by:
antonsigur earned 60 total points
ID: 8098017
Yes it is.

try this ...


import javax.swing.*;
import java.awt.*;

public class Bensin extends javax.swing.JApplet {

    public static Color getBgColor() {
         return new Color(255,255,204);
    }


    public void init() {

        this.setBackground(this.getBgColor());
        this.setSize(600,200);
        JTextField f = new JTextField();
        f.setForeground(Color.red); //text color)
        f.setBackground(Color.blue); //Field color)

        JLabel l = new JLabel("Some label");
        l.setForeground(Colro.red); //Text color)
        this.getContentPane().add(f);
        this.getContentPane().add(l);


    }


}
0
 

Author Comment

by:danBosh
ID: 8098197
for any else wiv simular problems this is how i did it

import java.awt.*;
import javax.swing.*;

//import java.awt.Color.*;
/*
<APPLET CODE = "swinger.class" WIDTH = 400 HEIGHT = 400>
     <param name="param1" value="dan">
       <param name="param2" value="biff">
       <param name="param3" value="Chris">
 </APPLET>

*/
public class swinger extends JApplet
{
     public static Color getBgColor() {
        return new Color(255,255,204);
   }
     
     public void init()
     {
          Container c = getContentPane();
        c.setLayout(new FlowLayout());
          c.setSize(350,250);
          c.setBackground(Color.green);
                 
          JTextField f = new JTextField(20);
        JLabel l = new JLabel("Some label");
     
       c.add(f);
       c.add(l);
       f.setForeground(Color.red); //text color)
       f.setBackground(Color.blue); //Field color)
       f.setText("foff");
       l.setForeground(Color.red); //Text color)
                   
     }

}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question