How to add margin of text in the JTextArea? - java

How can I add a Margin for text to the right and top like 5 pixel each inside the JTextArea.
Here is the Image: Click Here
public class SubTextField extends JTextArea{
public SubTextField()
{
setLineWrap(true);
setWrapStyleWord(true);
setPreferredSize(new Dimension(0,50));
Border b = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black);
setBorder(b);
setFont(new Font("Arial",Font.PLAIN,16));
actionButtons();
}
}

setMargin(new Insets(5,0,0,5)); would create a margin of 5 pixels at the top and right.
(the parameters to the Insets object are top, left, bottom and right, respectively).

If you are using window builder then you can use the Layout Assistant support and set the insets and many other features like growing,filling easily.

Related

Set a gap between the beginning of the JLabel and the icon

I have a Java Swing form and JLabel like this:
What I need to do is inserting a gap in the beginning of the JLabel:
So it will not stuck to the border line.
Note 1 : I already used jLabName.setIconTextGap(35); but it did the below:
I need to insert the gap before the icon not after it!
Note 2 : The Border Setting And Type And other setting:
You can use compound borders for that.
For Example.
//get border of your component which is button as you say
Border border = myButton.getBorder();
//create a new empty border with name it margin
Border margin = new EmptyBorder(0,10,0,0); //top 0, left 10 , bottom 0, right 0
//now set compound border to your button(component), with margin
myButton.setBorder(new CompoundBorder(border, margin));
//NOTE: CompoundBorder accepts two borders as arguments, first one is inner border and last one is outer border
Fixed it with the following code:
a1.setBorder(new CompoundBorder(new EtchedBorder(), BorderFactory.createEmptyBorder(1, 8, 1, 1)));

Trying to get an Hbox to be in front of a parent Pane

I've just been trying to put a blue box inside of a black box, to put it simply.
I have a class called DialoguePane, which extends Javafx Pane. It also has a field, an HBox that's supposed to hold buttons called actionButtonHBox, which I want to put inside of the DialoguePane.
Below is a picture of the window. The box with the label "select an action" is the DialoguePane object. Right now I'm as far as I have the two boxes inside one another. However, when I try to call the .setBackground method or use css styling to try to style the background of the Hbox, nothing changes, the background does not change. I also use toFront(), but nothing changes. I'm glad I'm at least at the point of having the boxes properly inside of one another, but I just want to change the background color of this region for formatting and design purposes.
Here's relevant parts of the Dialogue Pane's constructor
public class DialoguePane extends Pane {
private HBox actionButtonHbox;
public DialoguePane(String string) {
super();
this.resizeRelocate(480, 200, 450, 140); //Makes the DialoguePane's whole region have a width of 450, height of 140, at position x-cor 480px and y-cor 200px
this.setStyle("-fx-background-color: rgba(0, 0, 0, .95)");
//this.setDisplayableText(string);
this.makeActionButtonHbox();
}
public void makeActionButtonHbox() {
actionButtonHbox = new HBox();
System.out.println("Before: " + actionButtonHbox.backgroundProperty());
actionButtonHbox.resizeRelocate(0, 100, 400, 40); //Sets it at this position inside of the DialoguePane, a box with 400 width and 40 height. It's pretty thin.
actionButtonHbox.setBackground(new Background(new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY)));
actionButtonHbox.toFront();
System.out.println("After " + actionButtonHbox.backgroundProperty());
this.getChildren().add(actionButtonHbox);
}
There's other code that's responsible for putting a TextArea above the hbox (which is what says "Select an Action below" in the Pane, which happens in the setDisplayableText method).
So what could I do to make the background of this pane visible?

JPanel how to add a ToolTipText

I have a little problem, I need to add a ToolTipText to JPanel. How should I do this?
I want to have a tooltip when I have mouse over the circle.
This is part of my code.
JPanel component1 = new JPanel();
JPanel component11 = new JPanel();
okno.add(component1,"align left,cell 0 0, h 75!, grow,wrap");
component1.setLayout(new MigLayout("","[][grow][grow]", "[grow]"));
component1.add((okno.add(creLab("Kraj", i, czcionka, etykietki))),"left align, cell 0 0");
component1.add(t1,"cell 1 0,grow");
//component1.add(new circle1(),"right align, cell 2 0,h 50!, w 53!, gapleft 50, wrap");
component1.add(component11," right align, cell 2 0, h 30!, gapleft 300, wrap");
component11.setLayout(new MigLayout("","[]","[]"));
component11.add(new circle1(),"cell 0 0,h 50!, w 50!, dock north");
component11.setToolTipText("<html>W polu obok wpisz kraj pochodzenia towaru</html>");
I add also code of circle1:
class circle1 extends Applet{
public void paint(Graphics g){
setForeground(Color.yellow);
g.drawOval(0, 0, 50, 50);
g.fillOval(0, 0, 50, 50);
g.setColor(Color.black);
g.drawString("Jak", 14, 14);
g.drawString("wpisac", 3, 28);
g.setColor(Color.red);
g.drawString("kraj?", 14, 42);
//g.drawString(arg0, arg1, arg2)
}
}
Take a look at JComponent#getToolTipText(MouseEvent)
This will allow you to determine what text to return based on the location of the mouse.
It's difficult to determine for your code snippet, exactly where the circle is been drawen, but I would avoid drawing directly to the surface of the applet, but instead use a custom component (like a JPanel) instead (overriding its paintComponent method). This I would then either add to the applet or to the control panel.
This way your going to avoid issues with the mouse events been consumed
I would also take a look at Ellipse2D, which can be used to determine if the ellipse contains a given point
The first thing is to identify when the mouse is inside the circle. To do that you could verify the mouse position on a mouseMotionlister according to the circle area
http://www.java2s.com/Code/JavaAPI/javax.swing/JPaneladdMouseMotionListenerMouseMotionListenerlis.htm
Once you identify this situation you could proceed to change the tooltip
See Playing With Shapes. You can create a JLabel with a ShapeIcon. Then you just use the setToolTipText() method of the JLabel. You can then add the label to the panel like any other component.
Now that you can use a component to represent a Shape there is no need to do custom painting. Just create a panel add add components to the panel. You can also create JLabels for all your text strings.
Don't do custom painting, unless you have a good reason to do so.

swing: creating thin button borders that look the same as the look&feel default

I have an icon button that I want to use with a border. The default border looks too thick.
What's the easiest way to create a border with the same colors as the current look + feel?
Button myButton = createMyIconButton();
...
public Button createMyIconButton()
{
...
setBorder(BorderFactory.createLineBorder(???, 1));
}
The default border looks too thick.
Maybe you are looking for:
button.setMargin( new Insets(2, 2, 2, 2) );

SWT: Problem with sashform children's width

My application's got one horizontal SashForm with two children. The leftmost one should always be, at least, 200 pixels width. So I added a ControlListener to the left component. In its controlResized event I check the component's width. When it's smaller than 200, I set its bounds' width to 200. It apparently works as the component gets resized to that width, but the sashform stops working - the sash is no longer visible, it remains under the component.
Does anybody know what am I missing?
I've tried:
component.addControlListener(new ControlAdapter() {
#Override
public void controlResized(ControlEvent e) {
if (component.getBounds().width < 200) {
Rectangle bounds = component.getBounds();
bounds.width = 200;
component.setBounds(bounds);
}
}
});
here is an example
Just a guess, but maybe you need to call pack() or layout() on the SashForm after you programatically resize the inner component.

Categories