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
Related
I wish to obtain and modify the number of pixels between the text contained inside the text area and the border of the text area. To provide a more descriptive visual:
where the length of the blue line is what I desired. When I received both the padding and margins of the text area in my application, I got 0px. I assume that this is the padding/margin of the TextArea relative to the outside, not the inside relative to the Ttext area.
Many thanks.
In java to set the insets of a textarea you can use setMargin().
public void setMargin(Insets m)
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). This causes a redraw of
the component. A PropertyChange event ("margin") is sent to all
listeners.
Parameters: m - the space between the border and the text
for example:
JTextArea txtArea = new JTextArea("Hello world!");
txtArea.setMargin( new Insets(15,15,15,15) );
more on insets and setMargin().
Or another approach would be to add a compound border and then setting the insets on it like this:
JTextArea txtArea = new JTextArea("Hello world!");
Border border = BorderFactory.createLineBorder(Color.RED);
txtArea.setBorder(BorderFactory.createCompoundBorder(
border, BorderFactory.createEmptyBorder(15, 15, 15, 15))
);
see this answer.
I have a JTextField in my JFrame and I set the text in the JTextField.
When the text is long, JTextField gets the text's length and then jTextField's new width is equal to the text. It changes my window's shape and other components' places. How can I make JTextFields have static width so they won't be resized based on the length of the text that pass in?
The simplest thing you can do is set the column width, usually during initialization of the JTextField, e.g.:
new JTextField("Hello World!", 5);
new JTextField(10);
But the container will have a layout manager -- if you didn't specify it explicitly, it likely has a default. JFrame starts with BorderLayout in the content pane, although if you've added any other panels between the JFrame and the JTextField, we'd need to know that to have a better sense of the layout manager.
Some layout managers will constrain the width of the field as well, which is another way you might address your problem.
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 have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane