Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

painting buttons

Hi,

I would like to use a button in java that I can paint onto. Currently I am using JButtons with "WindowUtilities.setNativeLookAndFeel();". Because of this, I can not use the set button.setBackground(color.BLUE) to fill the button because as I have read, windows does not want you to use their (L & F) and be able to modify it!

Well, is there a way around this in the sense of say using a different type button etc ...

Basicly I just need a few buttons that are square, the color of my choice and the ability to add actions to - simple right?

thanks,

waffe
0
waffe
Asked:
waffe
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
sciuriwareCommented:
You can start simple:
   ImageIcon i = ...................

   JButton b = new JButton(i);

;JOOP!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I just need a few buttons that are square, the color of my choice and the ability to add actions

A button is by default square, you can set the back-ground to any Color you want, and actions can be handed by action-listeners. If you're worrying about look and feel, I would suggest using the same look and feel for all buttons (you can color them differently).
0
 
StillUnAwareCommented:
Here is a simple way to do that:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import javax.accessibility.*;

public class Z extends JFrame implements Runnable, ActionListener {
  JButton jb = new JButton(":)");
  public void init() {
    jb.setPreferredSize(new Dimension(100,100));
    jb.setBackground(Color.GREEN);
    jb.addActionListener(this);
    add(jb);
    pack();
    center(this);
  }
  public void actionPerformed(ActionEvent ae) {
    jb.setText(jb.getText()+"+");
  }
  public void run() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    init();
    setVisible(true);
  }
  public void center(Component c) {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dim.width - c.getWidth()) / 2, (dim.height - c.getHeight()) / 2);
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Z());
  }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
waffeAuthor Commented:
Thanks everyone - I will implement these ideas soon - but just to mention it again I am using WindowUtilities.setNativeLookAndFeel(); which overrides/disables the button.setBackground(Color.GREEN);. Are you all taking this into account?

Thanks

waffe
0
 
sciuriwareCommented:
My simple solution sets the foreground.

;JOOP!
0
 
waffeAuthor Commented:
WindowUtilities.setNativeLookAndFeel() also overrides/disables the set foreground command.

waffe
0
 
sciuriwareCommented:
I didn't SET the foreground; I load it.
Just try and we'll see what happens.
;JOOP!
0
 
waffeAuthor Commented:
Ok, so I am able to load an image into a button with your code JOOP - is there a way to paint the loaded image a color I want?

waffe
0
 
sciuriwareCommented:
My solution implies that you load ready-to-use images.
That is the simplest if you only want to display a limited set of images.
If you want to paint over it, that's another story.
;JOOP!
0
 
waffeAuthor Commented:
Thanks ;JOOP!, but I allready knew how to add an icon to a button -  My first words are "I would like to use a button in java that I can paint onto". Can this be down? - how?

Thanks,

waffe
0
 
waffeAuthor Commented:
I added a hundred points to the question which is obviously becoming more difficult - so how does one paint a button?

waffe
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I'm not sure if you can do a lot of stuff inside the button. You can change the fore-ground, back-ground, text, but that will be for the entire button. If you don't want to keep changing images then you'd probably be better painting on something else like a panel
0
 
waffeAuthor Commented:
So paint the panel and add that to the button as an Icon?

waffe
0
 
hoomanvCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can't add a panel to a button.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now