How to Change the Colour of a JLabel and JButton - java

I just changed the background colour of my frame using:
panel1.setBackground(Color.GRAY);
Now, it would probably look better if the text and the buttons are changed. How can I change the JLabel and the JButtons a bit more brighter? It will be much better when it is white because right now it looks like this:
Imagine that!

How to Change the Colour of a JLabel and JButton
To change the background color of JLabel and JButton, you can do this:
JButton btn = new JButton();
btn.setBackground(Color.WHITE);
JLabel lbl= new JLabel ();
lbl.setBackground(Color.WHITE);
lbl.setOpaque(true); //If opaque property is false, you can't see the color
To change the text color of JLabel and JButton, you can do this:
btn.setForeground(Color.WHITE);
lbl.setForeground(Color.WHITE);

JButton button = new JButton("test");//sample JButton
button.setBackground(Color.WHITE);//Set background color
button.setOpaque(true);//needs to be true in order to show color
JLabel title = new JLabel("I love stackoverflow!", JLabel.CENTER);
title.setForeground(Color.white);//simply set color

Related

how to stop JButton from changing position when a Jlabel is added before it

I have a JLabel (seriesInformationLabel) that is initially blank. There is a Jbutton (copyButton) next to it. Problem is everytime the JLabel loads a text value, the JButton moves to the right. How do I stop this JButton from moving?
private void init() {
super.initializeLayout();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Box buttonPanel = Box.createHorizontalBox();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
buttonPanel.setLayout(null);
copyButton = new JButton(Utilities.getString("COPY"));
copyButton.setActionCommand(COPY);
copyButton.setEnabled(false);
seriesInformationLabel = new JLabel();
seriesInformationLabel.setAlignmentY(CENTER_ALIGNMENT);
seriesInformationLabel.setName(this.getClass().getSimpleName() + "_seriesInformationLabel");
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
}
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
Well the free space changes as the text of the label changes.
This means that the space available for the "glue" will change equally between the two glue components causing the button to shift.
If you don't want the button to move then you need to get rid of the second "glue" component:
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
//buttonPanel.add(Box.createHorizontalGlue());
Maybe instead add another rigid area if you don't want the button completely at the right edge of the panel.

Java Button pressing visual style

JButton button = new JButton("Green Button");
button.setBackground(Color.green);
button.setBorderPainted(false);
button.setFocusPainted(false);
With above code, Java remove button's hover style.
Pressing down button makes button's background blue.
But how can Java make button's background never change (in this case keep green) while pressing down it?
You could use something as simple as...
JButton btn = new JButton("Hello");
btn.setBackground(Color.GREEN);
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setContentAreaFilled(false);
btn.setOpaque(true);
btn.setMargin(new Insets(10, 10, 10, 10));
Which paints a nice green button...
This has no "pressed" state rendering
Depend on what you are doing, you may be better off making a JPanel act like a button.
Another way which appears to work is to do button.setEnabled(false); although it fades the text.
The best way I can think of (I am not sure it is the best) is to do:
btnConvert.setContentAreaFilled(false);
btnConvert.setBackground(null);
btnConvert.setBorderPainted(false);
btnConvert.setFocusPainted(false);
btnConvert.setOpaque(true);
This will make everything transparent except for the text. You can then put a JPanel with a green background behind the button.
The following code works and does not change when you hover or click:
JPanel panel = new JPanel();
panel.setBackground(Color.green);
JButton button = new JButton("Button");
button.setContentAreaFilled(false);
button.setBackground(null);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setOpaque(true);
panel.setLayout(new BorderLayout(0, 0));
panel.add(button);
getContentPane().add(panel);
This is what it looks like when you hover or click:

Is it possible to implement scrollbar in Jframe without Jpanel?

I've written a long code involving several frames using only JFrame, and now I need a scrollbar for one of the frames. It seems like the only way to use JScrollPane is with JPanel, but I dont want to rewrite everything, is there a way?
frames[2] = new JFrame("Title");
frames[2].setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//EXIT_ON_CLOSE
frames[2].setContentPane(new JLabel(new ImageIcon("/Users/mac/Downloads/bg.000.jpg")));
frames[2].setLayout(new BorderLayout());
frames[2].setLayout(new FlowLayout());
frames[2].setResizable(true);
frames[2].setSize(750,433);
label[4] = new JLabel (" Search : ", JLabel.CENTER);
label[4].setFont(new Font("Serif", Font.ITALIC, 16));
label[4].setForeground(Color.BLACK);// font color
frames[2].add(label[4]);
TextField[2]= new JTextField ("Enter name", 15);
TextField[2].setFont(font);
frames[2].add(TextField[2]);
This is the frame (frame[2]) that I would like the scrollbar to be used in.

BorderLayout and JPanel with JLabel

I use:
BorderLayout a = new BorderLayout();
setLayout(a);
JPanel b = new JPanel();
now, if I use:
JButton c = new JButton("Press");
b.add(c);
add("East", b);
my JButton will appear normally. BUT if I say instead:
JLabel c = new JLabel();
c.setBackground(Color.BLACK);
c.setOpaque(true);
add("East", b);
my black JLabel won't appear, which I want to. Why does this happen? Thanks a lot
JLabel c = new JLabel();
You have an empty label, so I'm guessing the size if (0, 0) and there is nothing to paint. Try adding some text.
Also the following is incorrect:
add("East", b);
That is the old way of adding a constraint. Don't use hardcoded values and the constraint is specified second:
add(b, BorderLayout.???);
Read the BorderLayout API or the Swing tutorial on Using a Border Layout for the appropriate constraint.

Best way to add a String in a JFrame

which is the best way to add formated string in jframe.
Previously I was trying to add jlabel
If you want to display some text in a window, yes, adding a JLabel to your JFrame is fine.
Just create an instance of the font you want and assign it to the JLabel using setFont.
Here is a code samle (taken from here):
Font font = new Font("Jokerman", Font.PLAIN, 35);
JLabel textLabel = new JLabel(textMessage);
textLabel.setFont(font);

Categories

Resources