whats the most simple way to make a JButton only show the background color? I don't need any other effects like borders, 3D-look or hover-highlighting.
Thanks in advance.
I don't know if I have missed a point...
But I usually do somtehing like this:
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
Just set the colours and the border:
private static JButton createSimpleButton(String text) {
JButton button = new JButton(text);
button.setForeground(Color.BLACK);
button.setBackground(Color.WHITE);
Border line = new LineBorder(Color.BLACK);
Border margin = new EmptyBorder(5, 15, 5, 15);
Border compound = new CompoundBorder(line, margin);
button.setBorder(compound);
return button;
}
Use setOpaque(false); to make the background transparent.
How about
yourButton.setBorder(null);
?
You may want to use a JLabel with a MouseListener instead... unless you're tied to using a JButton or ActionListener in some way.
First, set your look and feel to something cool, like 'metal'.
try
{
for (UIManager.LookAndFeelInfo lnf :
UIManager.getInstalledLookAndFeels()) {
if ("Metal".equals(lnf.getName())) {
UIManager.setLookAndFeel(lnf.getClassName());
break;
}
}
} catch (Exception e) { /* Lazy handling this >.> */ }
Then do something like this:
my_jbutton.setBackground(new Color(240,240,241);
If the color of your button is exactly equals to swing control color (240,240,240), windows will apply windows-like button style to the button, otherwise, the button assumes a simple flat style.
I think you could do it like this
JButton button = new JButton("Click Me!!");
button.setBorderPainted(false);
button.setBackground(new Color());// inside the brackets your rgb color value like 255,255,255
button.setFocusPainted(false);
you can use the color picker to get rgb code(just search color picker)
Related
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);
}
I am trying to align the position of text within a JTextArea and a JButton, but with everything I tried, either nothing happens, or the alignment is still slightly off.
Here is what is looks like:
(You can see with the highlighted option that the JButton (center) is slightly lower than the two JTextAreas on either side.)
Here is some code:
categoryFile[i][j] = tempButton;
categoryFile[i][j].setBackground(Color.white);
categoryFile[i][j].setForeground(Color.black);
categoryFile[i][j].setOpaque(true);
categoryFile[i][j].setFocusable(false);
categoryFile[i][j].setBorderPainted(false);;
categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP);
categoryFile[i][j].setPreferredSize(new Dimension(500,10));
categoryFile[i][j].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
openPDFWithOptions(filePath,fileName);
}
});
JPanel listRow = new JPanel();
listRow.setBackground(Color.white);
listRow.setLayout(new BorderLayout());
listRow.setPreferredSize(new Dimension(800, 40));
JTextArea category = new JTextArea(fileElements[0]);
category.setEditable(false);
JTextArea parent = new JTextArea(fileElements[1]);
parent.setEditable(false);
listRow.add(parent,BorderLayout.WEST);
listRow.add(categoryFile[i][j],BorderLayout.CENTER);
listRow.add(category,BorderLayout.EAST);
categoryLists[i].add(listRow,c);
Right now I am using categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP) to change the position of the JButton, which ALMOST works. I've also tried changing the vertical alignment of the JTextAreas, but nothing changed.
How can I align the text within these components?
Quickest way to fix this would probably be to just add some padding on the 1st and third columns to set all the text to the same height. See Jpanel Padding
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:
I am wishing to draw a number onto a JTextField by overwriting the paint method. So that when the user edits the text field the number doesn't disappear. However, at the moment, the number isn't appearing at all, I have tried:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(number != 0){
g.setColor(Color.RED);
g.drawString(String.valueOf(number),0,0);
}
}
Any ideas, is this even possible?
Try to play with Y position in the g.drawString(String.valueOf(number),0,0); call. E.g. use getHeight()/2
..when the user edits the text field the number doesn't disappear.
As pointed out by #mKorbel, there is no need to override a JTextField in order to get red numbers, simply configure it using the public methods. OTOH..
g.drawString(String.valueOf(number),0,0);
If this is really all about numbers, perhaps the best approach is to use a JSpinner with a SpinnerNumberModel, and set a custom SpinnerUI.
Why don't you just add a small JLabel to the front of the JTextField? The JLabel could contain the number, and because it isn't editable it will always be there no matter what the user changes in the JTextField. You could also format the JLabel to make it red by calling setForeground(Color.RED);. This might be a much simpler solution?
For example, instead of doing this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
You might do something like this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
JLabel label = new JLabel("1.");
label.setForeground(Color.RED);
panel.add(label,BorderLayout.WEST);
Which adds a red JLabel to the left of the JTextField, and because you're using BorderLayout for the JPanel then it automatically makes the JLabel the smallest it can possibly be.
maybe there no reason override paintComponent() for JTextField, instead of use
JTextField.setBackground()
JTextField.setForeground()
JTextField.setFont()
JTextField.setHorizontalAlignment(javax.swing.SwingConstants.LEFT)
some hacks are possible by put there Html colored or special formatted text
EDIT
maybe this question is about
filtering KeyEvents in the Document / DocumentListener
or
JFormattedTextField with Number Formatter
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.