LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2003, 05:15 AM   #1
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Rep: Reputation: 50
java gui question


hello.

I was wondering how to get colored components.
like buttons, combo boxes etc.

titanium_geek
 
Old 11-13-2003, 06:56 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Try using the

setForeground

and

setBackground


methods
 
Old 11-13-2003, 06:26 PM   #3
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
nope.
My problem: I'm writing a hangman program as a learning program. It's really complicated. grr.
I have multiple panels... I tried to get the background colour to be orange.. it flashed orange on repaint, and went grey.
never mind. problem I'll be able to work out as a challenge.
What i want is like: red buttons. red combo boxes.

thanks

titanium_geek
 
Old 11-14-2003, 10:08 AM   #4
linorg
Member
 
Registered: Nov 2003
Location: delhi
Distribution: redhat 8.0 ,fedora
Posts: 32

Rep: Reputation: 15
hey i guess u could do this by embedding a red colored image on the button to get a red button
 
Old 11-14-2003, 03:20 PM   #5
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
linorg, could you elaborate on how I would do this?

and how about JComboBoxes?

thanks.

titanium_geek
 
Old 11-14-2003, 03:50 PM   #6
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
You should really be able to just do,


mybutton.setBackground(Color.RED);

etc.. sort of thing...

I can only think that maybe there's a wee quirk in your code somewhere that's messing things up...but there ya go...
 
Old 11-15-2003, 09:58 AM   #7
linorg
Member
 
Registered: Nov 2003
Location: delhi
Distribution: redhat 8.0 ,fedora
Posts: 32

Rep: Reputation: 15
hi there
according to me the foll peice of code should work
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class OneButton extends Applet {
public void init(){ Button testBut = new Button("BUTTON");
testBut.setBackground(Color.blue);
testBut.setForeground(Color.red);
add(testBut); } }

i haven't tested it yet
still better u culd use
create an image icon object say penguin
now just use
new Button(penguin)
this should wrk
fine

bye
 
Old 11-15-2003, 01:32 PM   #8
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
what you do is subclass the components you want to change the color of. You can also do cool things like make them circles or whatever, add gradients, etc. Here is the code I used for custom text fields (this is assuming you are using Swing/JFC.. IE JButton, JTextArea, etc.). It makes them orange, and with round corners.

Code:
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;

public class KTextField extends JTextField {
  private Paint main = new Color(0xff,0xa5,0x00); // orange
  private Paint hilite = new Color(0xff,0xff,0xff); // white
  private Paint lolite = new Color(0xa0,0x64,0x00); // dark orange

  public KTextField(int cols) {
    super(cols);
    init();
  }

  public KTextField(int cols, Paint color) {
    super(cols);
    this.main = color;
    init();
  }

  private void init() {
    setOpaque(false); // if true it paints a regular btn on top of what we do
    setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
  }

  protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    int width = getWidth();
    int height = getHeight();

    g2d.setColor(lolite);
    g.fillRoundRect(0, 0, width-1, height-4, height, height-4);
    g2d.setColor(hilite);
    g.fillRoundRect(1, 4, width, height-4, height, height-4);
    g2d.setPaint(main);
    g.fillRoundRect(1, 2, width-2, height-4, height, height-4);

    super.paintComponent(g);
  }
}
This formula can be applied to most swing components, but the more complex ones will give you trouble. Another thing that's worth looking into is creating a custom MetalTheme (which I also have example code for) which globally replaces colors in the default Swing Theme.

HTH

B.

Last edited by german; 11-15-2003 at 01:33 PM.
 
Old 11-15-2003, 02:20 PM   #9
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
NICE!!

well, I've messed about with this program to much, I'll probably have to re write it.

thanks to all of you.

titanium_geek

PS it's moments like this were i wonder what i'd do without LQ?!
 
Old 11-16-2003, 01:25 PM   #10
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
yes! it all works!
quick question:
how to change the highlight/hover colour on the menu/JComboBox?
(metal)

titanium_geek
 
Old 11-17-2003, 05:41 AM   #11
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
In order to set colors for swing components globally without subclassing each one, there's a class javax.swing.UIManager , which all swing components use for their icons, fonts and colors (in the Metal theme).

This is an application to show the values for UIDefaults (it is essentially a hash of settings)
http://www.discoverteenergy.com/file...IDefaults.java

And make sure you set all of the colors you want to change BEFORE you create the objects (a static initializer of the main class is a good place) or else you'll have to call
Code:
SwingUtilities.updateComponentTreeUI([main app frame object]);
which slows things down. Also, the previous example I posted does use a fair bit of CPU if you have a lot of KTextField's on screen, because when you call setOpaque(false); you force Swing to traverse the entire component hierarchy to fill in every pixel you don't (it's a speed hack that you have to set it true or false at all). If you don't want round buttons or text fields, then I'd suggest setting UIManager defaults to just change the color of stuff.

HTH

B.

edit:

Read and learn this for homework:
http://java.about.com/library/swing/...apter_21-1.htm

and this is an excellent reference to have bookmarked:
http://java.sun.com/docs/books/tutorial/uiswing/

Last edited by german; 11-17-2003 at 05:49 AM.
 
Old 11-18-2003, 06:17 PM   #12
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
thanks guys.

next challenge: making it do something!

titanium_geek
 
Old 11-19-2003, 06:57 PM   #13
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Original Poster
Rep: Reputation: 50
yes it works. and for color of background:

let's make a panel "pain"

pain.setBackground(Color.RED);

note: set it for all the panels you use, will have gaps...

titanium_geek
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Like GUI Tool For Java? jamie_barrow Programming 11 12-17-2005 06:01 AM
java GUI in QT? true_atlantis Linux - Software 5 09-27-2004 05:59 PM
java gui linux_ub Programming 16 07-02-2004 08:13 AM
edonkey java gui not finding java Sephiroth Linux - Software 0 03-13-2004 08:14 PM
java and non-GUI RH8 Zeke Linux - Newbie 1 08-09-2003 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration