I saw a site that implements a TextPrompt.
http://tips4java.wordpress.com/2009/11/29/text-prompt/
In the implementation, there is a setShow(Show.ALWAYS) method to always show the prompt text whether a JTextField has focus or not.
JTextField tf = new JTextField();
TextPrompt tp = new TextPrompt("Prompt", tf);
tp.setShow(Show.ALWAYS);
Is there a way to do it in JavaFX?
The way is to override the default css style of pseudo class "focused" of textfield. To do that load your own css file with this
.text-field:focused {
-fx-background-color: -fx-focus-color, -fx-text-box-border, -fx-control-inner-background;
-fx-background-insets: -0.4, 1, 2;
-fx-background-radius: 3.4, 2, 2;
-fx-prompt-text-fill: transparent; /* <----- Remove this line */
}
content and remove the -fx-prompt-text-fill attribute from it.
CSS loading example https://stackoverflow.com/a/9739698.
Related
I have a button as part of a Xamarin Forms custom renderer. I can style the button as I wish on iOS and Android in code without an issue and for the most part, can do the same on Windows Phone 8.
I'm having a problem though with including an image on a button with text next to it and altering the border on the button so it is rounded. I've found plenty of examples using XAML, but not in pure C#.
Currently, the custom renderer on Windows Phone looks like this
[assembly: ExportRenderer(typeof(NewButton), typeof(NewButtonRenderer))]
namespace WinPhone
{
class NewButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.ApplyTemplate();
var border = new Border
{
CornerRadius = new System.Windows.CornerRadius(10),
};
Control.Foreground = new SolidColorBrush(Colors.White);
Control.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 130, 186, 132));
Control.BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255,45,176,51)) ;
Control.BorderThickness = new System.Windows.Thickness(0.8);
}
}
}
}
I have a Border set up, but can't find a way to add it (and secondary to this, if I have the background, border brush and border thickness in the Border object, does it have the same effect as applying directly to the control?), nor a way of creating an image button with an image in my Images directory.
Is what I'm trying to do correct or am I missing a piece of the jigsaw? I'm doing this in pure C# for a specific reason.
I have a JScrollPane that holds a JPanel. The layout on the JPanel is a GridBagLayout. On that JPanel, I add a number of custom components - each is a JPanel with 3 JLabels.
The first time in the program I lay all of this out, it works fine. When I invoke the code to add another custom component to the JPanel, the panel appears empty, but I can determine by examining the contents of the JPanel that my components are actually there. If I resize the JDialog in which this all sites, the JPanel will paint properly. It also works if I scroll the JScrollPane horizontally even a tiny bit.
I use the same method for the initial layout as I do when adding an item.
I've tried various combinations of repaint(), invalidate() and doLayout() but nothing seems to work all the time. I've run into this situation before and have never been able to fully solve it. Any suggestions?
Running under OpenJDK 7u25. Below is the code that lays out the scroll pane and panel.
private void displayRelatedBug(ArrayList<Bug> a_bugs) {
// sort the bugs by ID
ArrayList<Bug> l_sorted = new ArrayList<>(a_bugs);
Collections.sort(l_sorted);
pnlRelatedBugs.removeAll();
pnlRelatedBugs.setLayout(new GridBagLayout());
GridBagConstraints l_gbc = new GridBagConstraints();
l_gbc.gridx = 0;
l_gbc.gridy = 0;
l_gbc.gridwidth = 1;
l_gbc.gridheight = 1;
l_gbc.anchor = GridBagConstraints.NORTHWEST;
l_gbc.fill = GridBagConstraints.NONE;
l_gbc.insets = new Insets(3, 4, 0, 0);
for (Bug r : l_sorted) {
pnlRelatedBugs.add(new RelatedBugDisplay(r, this), l_gbc);
l_gbc.gridy++;
}
// add a filler at the bottom to push it up
l_gbc.weighty = 1.0;
pnlRelatedBugs.add(new MMPanel(), l_gbc);
// add a filler on the right to push them left
l_gbc.weighty = 0.0;
l_gbc.weightx = 1.0;
l_gbc.gridx++;
pnlRelatedBugs.add(new MMPanel(), l_gbc);
// try in vain to make it show up!!!
pnlRelatedBugs.invalidate();
pnlRelatedBugs.doLayout();
pnlRelatedBugs.repaint();
scrollerRelatedBugs.doLayout();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
pnlRelatedBugs.repaint();
scrollerRelatedBugs.repaint();
// this seems to help if the scroll bar is showing
scrollerRelatedBugs.getHorizontalScrollBar().setValue(1);
scrollerRelatedBugs.getHorizontalScrollBar().setValue(0);
}
});
}
Whenever you add/remove components from a visible panel, the basic code is:
panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();
Without a proper SSCCE we can't really tell what your code is doing.
If you do add/remove/replace/others actions with components on showing container, you must to revalidate and repaint your container, to which you add components for proper displaying.
When using fl.controls.CheckBox inside a fl.controls.ScrollPane, it seems that whenever the last item in ScrollPane's children container is a CheckBox, ScrollPane's update() method always leaves significant blank space below the CheckBox, although I specifically set the size of the CheckBox.
Question: How do I remove the invisible height from the CheckBox?
I've researched in many places and finally found a useful post by this awesome person in the link below. All credits goes to him, Infineon. I can't trace to the flash support forum he is referring to though.
http://www.actionscript.org/forums/showpost.php3?p=842806&postcount=6
The solution to the above invisible height problem is really hard to find as a dig of the Flash AS3 documentation doesn't turn up override protected function configUI():void method easily. At least I haven't found it till now, and I will never have known about it either without some kind of prior knowledge into the inner workings.
*Note:* This solution doesn't only work on CheckBox. Other form UI widgets like fl.controls.RadioButton also works the same way.
As noted below, this is because of the package hard-coding width and height of the rectangle in the super class of the widget. We will need to override it so that the invisible rectangle becomes smaller or editable.
I will reproduce his code verbatim here to prevent link rot:
package com.your.package.here
{
import fl.controls.CheckBox;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.text.TextFieldAutoSize;
public class WidgetCheckBox extends CheckBox
{
public function WidgetCheckBox()
{
}
override protected function configUI():void
{
super.configUI();
// remove the background movie clip added in the superclass
removeChild(background);
// redraw the hit area of the checkbox component
var bg:Shape = new Shape();
var g:Graphics = bg.graphics;
g.beginFill(0, 0);
// draw the background area using the width and the height of the
// component, instead of hardcoding these properties ( in the
// superclass the width and height of the rectangle were 100 and 100
g.drawRect(0, 0, _width, _height);
g.endFill();
background = bg as DisplayObject;
// add the new background
addChildAt(background, 0);
}
override public function set label(value:String):void
{
super.label = value;
// in the superclass the size of the label textfield was set to
// 100 by 100 px; instead of using these values, autosize the
// textfield every time a new label is set
textField.multiline = false;
textField.autoSize = TextFieldAutoSize.LEFT;
}
}
}
Then all you need to do is to import the new class you have extended:
var checkBox:WidgetCheckBox = new WidgetCheckBox();
Hope this helps some people to relieve their head-scratching.
I want to create a customised title bar for my JFrame. I can remove the default title bar with
JFrame.setUndecorated(true)
Now i need to create a customised title bar for my JFrame with a close button?
Without having done that ever, I think I would go this way:
Indeed set the JFrame to undecorated
Extend JRootPane to add an additional field titleBar
Create a TitleBar component holding the title, the close button, etc...
Set a new LayoutManager on that JRootPane (have a look at JRootPane.RootLayout) and layout the components in the appropriate order (first the title bar, then below the menubar, then below the content pane)
Set an instance of that extends RootPane on your JFrame
There are maybe better ways.
I'm not quite sure of how you want to customize the close button, but maybe this can point you in the right direction: How can I customize the title bar on JFrame?
EDIT: Here's an updated working link to a forum about customizing his GUI and one user posted code on his creation of a simple GUI: Here
It looks like you can just modify his removeComponents method and create an addComponents method to fit your needs.
The Code According to the Above Link :
(Edited for Java 8)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class Testing {
public void buildGUI() throws UnsupportedLookAndFeelException {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame();
f.setResizable(false);
removeMinMaxClose(f);
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("Exit");
p.add(btn, new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener((ActionEvent ae) -> {
System.exit(0);
});
}
public void removeMinMaxClose(Component comp) {
if (comp instanceof AbstractButton) {
comp.getParent().remove(comp);
}
if (comp instanceof Container) {
Component[] comps = ((Container) comp).getComponents();
for (int x = 0, y = comps.length; x < y; x++) {
removeMinMaxClose(comps[x]);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
new Testing().buildGUI();
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
}
may Work Fine but what if user also Want to set a L&F
such as nimbus
There are really three ways to approach this:
Set the frame to undecorated and implement everything, which includes control buttons, snapping, resizing and moving.
Get the root pane of the JFrame and directly edit that pane. You will need to add the control buttons and the snapping behaviour.
Use JNI to get the window's handle at the creation of a JFrame to get the control of it's attributes. This is better explained in this post. I have also built a little project which is basically an extension of the JFrame class that handles everything that needs to be dealt with... This last approach does not break native functions like snapping and resizing. But you do need to create the control buttons again since you have a new title bar if you want to build it from scratch.
If you've ever used Visio or a UML class diagram editor, you have an idea of what I'm trying to accomplish: Within a JFrame, users can add ellipses that enclose a small editable text field. These ellipses can be repositioned within the frame when the user drags them. Clicking on an ellipse causes the text to become editable: a carat appears, highlighting a substring is possible, etc.
I've got the basic structure set up: the 'ellipse' is a self-contained component, with methods called on it from the containing JFrame and its listeners. I've tried two approaches:
in the component's draw() method, use a TextLayout to find bounds, position the contained text within the ellipse, and draw it to the frame using TextLayout's draw(). This is fast. Dragging the components around in the JFrame, mouse-over and mouse-click behavior are all straightforward. However for the editing functionality it looks like I will need to write a lot of custom code to handle hit testing, carat positioning, text highlighting, line wrapping, etc.
having the component contain a reference to the containing JFrame, and adding or repositioning a TextComponent in that JFrame after drawing the ellipse. This has the advantage of all the built-in TextComponent behavior for editing and line wrapping. But the logistics are really sloppy, and positioning the TextComponent becomes messy too - especially when the user drags the component around.
I'm quite possibly thinking about this all wrong. Can anyone suggest a simple way to do this that I haven't yet stumbled across?
Why don't you combine both your approaches. As long as you are editing, display the text component, otherwise paint all text using a TextLayout. The following example code shows such an approach extending a simple JComponent. It draws a rectangular shape with some text in it and if you click inside it shows an editing possibility. As soon as you click outside again, the component vanished. Note that all the edit-handling functionality is missing in this basic example.
class TestComponent extends JComponent {
JTextArea jta = new JTextArea("12345");
public TestComponent() {
setPreferredSize(new Dimension(400, 400));
setLayout(null);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(final MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
if (e.getX() >= 40 && e.getX() <= 200 && e.getY() >= 40 && e.getY() <= 80) {
TestComponent.this.add(jta);
jta.setBounds(42, 42, 156, 36);
} else {
TestComponent.this.remove(jta);
}
repaint();
}
});
}
});
}
#Override
public void paintComponent(Graphics _g) {
Graphics2D g = (Graphics2D) _g;
g.drawRect(40, 40, 160, 40);
TextLayout layout = new TextLayout("12345", g.getFont(), g.getFontRenderContext());
layout.draw(g, 42, 42 + layout.getAscent());
}
}