I'm stuck on something silly (it feels that way). I'm trying to create a Java TitledBorder with a specific Color for the border and the Title.
Border varBorder;
// this works (creates red border with black text)
varBorder = BorderFactory.createLineBorder(Color.RED);
varBorder = BorderFactory.createTitledBorder(lv_border, "Info");
// this crashes
varBorder.setTitleColor(Color.RED);
varBorder.setBorder(lv_border);
What is the correct way to set the color of a TitleBorder?
In your code varBorder is of Border type. You may cast it to TitledBorder or change the type of variable. In Border class there is no such method like setTitleColor.
Related
I have made a frame and put a button and a text field in it.
I am using the setMargin method to set a margin between the caret and the border of the text field and works perfectly fine until I add a border to it.
After adding the border the setMargin call method does not work.
Could you please help me understand the origin of the issue and find an alternative of having both a border and a certain margin?
public class main extends JFrame {
public static void main(String[]args){
JTextField textfield0;
JButton button0;
Border border7=BorderFactory.createDashedBorder(new Color(0xA524FF), 2, 5, 4, true);
Border border8=BorderFactory.createCompoundBorder();
Border border01=BorderFactory.createLineBorder(Color.RED);
Border border02=BorderFactory.createLineBorder(Color.YELLOW);
Border border9=BorderFactory.createCompoundBorder(border01, border02);
textfield0=new JTextField();
textfield0.setPreferredSize(new Dimension(300,30));
textfield0.setFont(new Font("Consolas",Font.BOLD,15));
textfield0.setCaretColor(Color.RED);
textfield0.setBackground(Color.CYAN);
textfield0.setForeground(Color.MAGENTA);
textfield0.setText("name");
//textfield0.setBorder(border9);
textfield0.setSelectedTextColor(Color.YELLOW);
textfield0.setMargin(new Insets(0,7,0,5));
textfield0.setCaretPosition(0);
textfield0.setSelectionColor(Color.PINK);
button0=new JButton();
button0.setText("submit");
button0.setPreferredSize(new Dimension(100,30));
button0.setFocusable(false);
button0.setBackground(textfield0.getBackground());
button0.setFont(textfield0.getFont());
button0.setBorder(textfield0.getBorder());
JFrame frame00=new JFrame();
frame00.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame00.setLayout(new FlowLayout());
frame00.add(button0);
frame00.add(textfield0);
frame00.pack();
frame00.setVisible(true);
}
}
We can find an answer for this in the JavaDoc which requires a few extra steps:
Sets margin space between the text component's border and its text.
The text component's default Border object will use this value to
create the proper margin. However, if a non-default border is set on
the text component, it is that Border object's responsibility to
create the appropriate margin space (else this property will
effectively be ignored).
Source: https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#setMargin-java.awt.Insets-
And to do the above we can do one of two things, we can use a compound border as shown here: Java Swing - setting margins on TextArea with Line Border or we can take the following steps:
Overriding both AbstractBorder.getBorderInsets(Component c) and
AbstractBorder.getBorderInsets(Component c, Insets insets) to
provide the correct insets
Source: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html#custom
I strongly recommend reading through the above link to get a better understanding of borders and how you can work with them.
If you read the javadoc for Border, it states: "Use EmptyBorder to create a plain border (this mechanism replaces its predecessor, setInsets)"
So you can either use a CompoundBorder with a PlainBorder and the one you are setting, or override getBorderInsets(Component c) on your existing Border
I'm trying to change the color of the default blue-gray border on my JTabbedPane. You can see the border circled in the image below.
I want to make this a dark gray color. I've tried using the following UIManager properties (which result in the UI in the image above):
UIManager.put("TabbedPane.background", Color.decode(Colors.FACE_BG));
UIManager.put("TabbedPane.foreground", Color.decode(Colors.FONT_WHITE));
UIManager.put("TabbedPane.opaque", true);
UIManager.put("TabbedPane.selected", Color.decode(Colors.TABLE_SELECTION));
UIManager.put("TabbedPane.border", BorderFactory.createLineBorder(Color.decode(Colors.DARK_GRAY), 1));
How do I achieve this? Thanks!
Try
nameOfYourComponent.setBorder(BorderFactory.createLineBorder(Color.black, 1));
// the second argument is thickness
Please have a look at the below code
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
I used this code to create a line border for the JTextField. However now I need to remove it and replace it's normal view. Below is what I tried.
introducerFeesTxt.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
That code above again created a border which is not similar to other normal JTextFields. Below is a screenshot.
You can clearly see the differnece between the "Normal" JTextField and the JTextField with the added border. How can I reset it to be "normal" ?
You could keep the original border in a variable before change it and then use this border to set it back to its original state:
Border originalBorder;
...
JTextField textField = new JTextField(20);
originalBorder = textField.getBorder();
// here you can safely change text field's border
Of course the scope of this originalBorder variable should be wide enough to use it when needed (f.e.: class member).
Note: Please note this approach is independent of the PLAF used by your application.
You should use the original border from the L&F (Look and Feel).
Border border = BorderFactory.createLineBorder(Color.RED, 1);
introducerFeesTxt.setBorder(border);
// some operation
introducerFeesTxt.setBorder(UIManager.getBorder("TextField.border"));
I am making a simple swing application and I want to add some titled borders to my components. The border on both of my JScrollPanes work fine, but the JTextField and the JButtons don't. Allow me to share some screen shots.
I just have simple code for this. i.e
TitledBorder border = new TitledBorder("Border");
convert.setBorder(border); //convert is the JButton
I don't see why it would not work for one thing, and work for the other. Can anyone help me out?
A JTextField and JButton both use a Border already. So the titled border works but it changes the appearance of the component because you lose the default Border.
I also agree that normally you don't use a TitledBorder for an individual component but I suppose you could try to use a CompoundBorder to see if it looks any better:
CompoundBorder border = new CompoundBorder(titledBorder, button.getBorder());
button.setBorder( border );
but then the problem with the above approach is that you lose dynamic repainting of the border when you press/release the mouse on the button.
I would like to make an option dialog in my application. In this dialog I want to make kind of Areas surrounded with a border and with a title.
An example of what I want is in Firefox:
How can I do something like that in Java?
Here you can find all informations you need.
Basically you can use border factory to create a Border using types available in Swing:
Border lineBorder = BorderFactory.createLineBorder(Color.black);
JPanel panel = new JPanel();
panel.setBorder(lineBorder);
You can also define your custom borders implementing Border interface.