How to personalize TextField design with Swing - java

Is there a way to let the textField of eclipse/swing this way?

You can add a matt look to the border of text field like this :
JTextField textField = new JTextField();
// left side border
textField .setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
//right side border
textField.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));

If you need to customize only Inputfield, extends JTextInputField and Override the paint() method.
But probably you will need a full Look and Feel.

Related

JLabel multiline with HTML

I have a JLabel that I would like to contain a text that can go over multiple lines, and resizes if the window changes shape.
I've looked this up and most people seem to recommend wrapping the label text in HTML. This however does not make new lines for me.
My label is located in a JPanel and I suspect that the problem may be that my panel has misconfigured its border, and so the label text just continues beyond the panel border.
here is how the label looks inside the status panel
Here are the settings of the panel:
private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
statusPanel.add(latestOrdreLabel);
this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
Then the label is set up like this:
private final JLabel latestOrdreLabelResult = new JLabel();
String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());
latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");
statusPanel.add(latestOrdreLabelResult);
In HTML, to down line your'e need to use <br/> instead of \n.
You can to use in replace String method, for example:
latestOrdreStatus.replace("\n", "<br/>");
You just need to resize the label.

Java Swing - JButton wrong border

I try to set new red border to my "Ok" button, but instead of I get from this. How to make 1-st picture? (it's photoshop)
I try
button.setBorder(new LineBorder(Color.RED, 1));
You overrided the button's default border. You should set a compound border using the existing one and a new red one.
Border innerBorder = button.getBorder();
Border outerBorder = new LineBorder(Color.RED, 1);
button.setBorder(new CompoundBorder(outerBorder, innerBorder));
Here is a one liner combined with using the BorderFactory :-)
button.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.RED, 1),
button.getBorder()));
Using BorderFactory - http://docs.oracle.com/javase/tutorial/uiswing/components/border.html#createapi

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.

Java Swing - setting margins on TextArea with Line Border

As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.
aboutArea = new JTextArea("program info etc.....");
Border border = BorderFactory.createLineBorder(Color.BLACK);
aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));
add(aboutArea);
I have tried each of these:
aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));
but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?
What if you try adding a CompoundBorder , won't this do, this will give you almost same thing
JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));

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) );

Categories