How to make JTextField semi-transparent - java

I am trying to create a JTextField with semi-transparent background (i.e. black background with alpha value of 120). My current code is:
public static void designTextField(final JTextField tf) {
tf.setBorder(null);
tf.setFont(new Font("Comfortaa", Font.PLAIN, 30));
tf.setBackground(new Color(0, 0, 0, 120));
tf.setForeground(new Color(200, 200, 200, 200));
}
However, this doesn't seem to be working. Here are the pictures this code results in (there are two text fields):
No text entered:
Text entered:
As you see there're several weird drawing bugs, and both text fields seem to be fully non-transparent. How can I fix this?

Swing perfectly works with transparency. You just need to add
tf.setOpaque(false);
https://docs.oracle.com/javase/tutorial/uiswing/painting/problems.html
section "Problem: Visual artifacts appear in my GUI" explains.

Related

When hovering over element, a copy of the parent JFrame appears inside it [duplicate]

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.
This related example also makes the JPanel translucent.
try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

Remove focus from JComboBox Java Swing

I am trying to remove the dotted line from my JComboBox.
The initial ComboBox Initial JComboBox has a dotted line after it has gained focus:
After clicked
Now, I want to remove that focus. However I can't find it in the UIManager's options (https://gist.github.com/itzg/5938035). I have looked at this post from May 2018, but the answer is not there yet.
I have tried the following:
jComboBox.setFocusable(false);
UIManager.put("ComboBox.focus", new Color(0, 0, 0, 0));
but none of them worked.
Any help would be greatly appreciated!
You can do the following:
comboBox.setUI(new BasicComboBoxUI());
This will result in the following after an element was selected and get you rid of the dotted border:
For removing any 'kind' of focus border, you need to override the ComboBoxUI which is used for drawing the box and its component.
Here is the code I used to achieve the example:
public ComboboxWithoutDottedBorder() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception ignored){}
this.setVisible(true);
JLabel label = new JLabel("Label");
JComboBox<String> combo = new JComboBox<>();
this.setLayout(new BorderLayout());
combo.addItem("A");
combo.addItem("B");
combo.addItem("C");
combo.addItem("D");
combo.setUI(new BasicComboBoxUI());
this.add(label, BorderLayout.NORTH);
this.add(combo, BorderLayout.SOUTH);
}

Java making a GUI

Probably gonna get pooped on for this, I am not asking anybody to do my homework, but I am reaching out for help. I've already done the first question but the professor is asking for me to make a GUI in the second question. I've actually never made a GUI in Java before so this is all really new to me.
Make a simple, but visually appealing, GUI that has at least the following features:
a. A text box to enter an email address;
b. A text box to enter a message title;
c. A text area to enter a message;
d. Appropriate labels for text entry areas;
e. Buttons to “Send,” “Save for later,” and “Discard” the message;
f. A checkbox to “Flag as Important.”
As you marked Netbeans i assume you use this IDE.
To get in touch with gui building i would recommend you the netbeans gui builder:
Netbeans Gui Builder Tutorial
For your simple task this should be more than enough if using such tools for your task is allowed
If you are to just create the controls (text 'box', text area, etc) and not make them do anything, this is really simple to do in Netbeans. just start a new application and drag and drop from the Swing Controls shown on the right of the Netbeans IDE.
For more on building java GUIs, try my guide at
http://philofjava.webstarts.com/
Definitely research on Java applets, that is most likely what the problem is references.
With applets, you can insert all those components onto a pop-up window at the coordinates of your choosing.
Edit: here's a sample from a project I had to do for a project in my java class. It creates the JFrame then puts the components on it. I'd really recommend looking up how to use Swing, a lot of websites offer in depth tutorials for every component you can use.
import javax.swing.*;
public class aSimpleApplet
{
public static void main(String args[])
{
JFrame frame = new JFrame("This is the frame that holds all the components");
frame.setSize(800, 800);
frame.setLayout(null);
frame.setVisible(true);
JButton button = new JButton("A button");
button.setBounds(25, 25, 150, 50);
JTextField textField = new JTextField("A text field");
textField.setBounds(200, 25, 575, 100);
JTextArea textArea = new JTextArea("A text area");
JScrollPane scrollBar = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setBounds(50, 175, 350, 300);
JCheckBox checkBox = new JCheckBox("A checkbox");
checkBox.setBounds(400, 200, 250, 50);
JLabel label = new JLabel("A label");
label.setBounds(100, 600, 150, 50);
frame.add(button);
frame.add(textField);
frame.add(scrollBar);
frame.add(checkBox);
frame.add(label);
}
}

Setting a 1 or 2-character text on a JButton 32x32

I want to set the text on a JButton that is size 32x32 but it only shows "...". yeah I know you could see the text if you make the button bigger, but how do you make the text be shown on a 32x32 jbutton? The text is only 1 or 2 digits(characters), it is actually a counter. Thanks
The insets are probably crowding out the text...
try
button.setMargin(new Insets(1, 1, 1, 1));
edit: Also, use a smaller font.
edit2: you can also control the insets for all buttons:
UIManager.put("Button.margin", new Insets(1, 1, 1, 1));
I don't think you can, this is managed directly by the look'n'feel' that is used by Java. You could try by changing it to another one to see if there is one with different insets. You could try changing them by setting smaller insects programatically.
A more complex way would be to subclass the JButton class and provide a custom drawing implementation but I think you will lose all the other cool effect.
As per my idea , its quite simple to making GUI application easier.I am writing some code below it may help you .
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frm=new JFrame("Manoj Button Test");
frm.setVisible(true);
frm.setSize(500,500);
Container cnt=frm.getContentPane();
//You can add any text to the JButton
JButton btn=new JButton("Hello Button");
cnt.add(btn);
//2nd type of example
JButton btn2=new JButton();
int number_btntext=4;
btn2.setText(String.valueOf(number_btntext));
cnt.add(btn2);
}
In the above code I have set text to GUI JButton.

JPanel transparency problem

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.
This related example also makes the JPanel translucent.
try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

Categories

Resources