I have tp place a AutoCompleteField in one of my screen in Blackberry app. I have to show a place holder text to provide hint for user to enter the information.
Here is the below code of AutoCompleteField
BasicFilteredList filterList = new BasicFilteredList();
String[] address = { "T 115 Centro Galleria Shopping Centre, Cnr Old Collier and Walters Road Morley WA 1522",
"1423 SEAVIEW POINT POINT COOK VIC 2674",
"Lot 1498 Yarraman Road Wyndham Vale VIC 3795",
"Lot 3506 Witchmount Close Hillside VIC 4055",
"6 Paas Place Williamstown VIC 4233",
"Lot 99 14 James Close Sunbury VIC 4502",
"1 Charlotte Street Clayton South VIC 4779" };
filterList.addDataSet(1, address, "address", BasicFilteredList.COMPARISON_IGNORE_CASE);
AutoCompleteField autoCompleteField = new AutoCompleteField(filterList){
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
if(selectedText!=null){
BasicFilteredListResult result = (BasicFilteredListResult) selection;
selectedText.setText(result._object.toString());
}
}
}
};
add(autoCompleteField);
Anyone, please suggest me how could I implement the same.
Thanks.
You can use a similar technique to the one shown here for normal EditFields. Basically, you need to override the paint() method in an AutoCompleteField subclass. In paint(), you check and see if the field is empty, and if so, you manually draw the placeholder text you want.
The difference is that AutoCompleteField is a Manager with a BasicEditField inside of it. So, to draw the text properly, you need to figure out the x and y offsets of the edit field within the parent Manager (the AutoCompleteField).
So, replace your AutoCompleteField instance with an instance of this class:
private class CustomAutoCompleteField extends AutoCompleteField {
private int yOffset = 0;
private int xOffset = 0;
public CustomAutoCompleteField(BasicFilteredList filteredList) {
super(filteredList);
}
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
if(selectedText!=null){
BasicFilteredListResult result = (BasicFilteredListResult) selection;
selectedText.setText(result._object.toString());
}
}
}
}
I tested this on OS 5.0, with an instance that didn't have any margin or padding set. It's possible that with different layouts, you may need to adjust the logic for calculating the x and y offsets. But, the above code shows you the basic idea. Good luck.
Edit: the above code is presented with the caveat that your onSelect() method is clearly relying on code not shown. As is, the above code won't compile. I left onSelect() in there just to show that I'm essentially just replacing the anonymous class you originally had, and not doing anything different in your onSelect() method, as it's not directly related to the placeholder text issue.
Related
I would like to match color search results text with the checkbox text after clicking search button. See pic.
Currently I can view the Search Results text color but it doesn't match the checkbox text color after clicking search button. The code below is only for the Search Car Results text area and a class named CarBrand that matches the key of this HashMap carDetails. I am not sure how to compare and match its color using HashMap. Any suggestions would be great!
import java.awt.Color;
import java.swing.tree.DefaultTreeCellRenderer
public final class CarDetails extends DefaultTreeCellRenderer
{
private final Color defaultColor;
private final HashMap<String, Color> carDetails = new HashMap<>();
public CarDetails()
{
int i = 0;
defaultColor = getBackground(); //default color
int [][] rgb = {
{ 200, 000, 200 },
{ 000, 140, 000 },
{ 000, 200, 200 }
};
for (CarBrand car: CarModel.getCarBrandDetails()) {
carDetails.put(car.getCarBrand(), new Color(
rgb[i][0], rgb[i][1], rgb[i][2]));
i++;
// TODO this part is what I am not sure.
if (carDetails.containsKey(car.getCarBrand()) && carTable != null) {
for (Component c : carTable.getComponents()) {
if (c.getName().equals(car.getCarBrand())) {
c.setForeground(carDetails.containsObject(new
Color(rgb[i][0], rgb[i][1], rgb[i][2])));
}
}
}
}
}
}
I expect the output to match the color coding of the Search Car Results with the CheckBox text (Honda, Hundai, BMW) like the pic below.
I agree with Andrew Thompson a lot of code is missing to be able to reproduce your problem.
You should check if your conditions are correct (if statements).
The declaration of carTable is missing so we can't verify how it is build up. You use Component.getName(), are you sure this contains the value you expect ?
see what-is-java-awt-component-getname-and-setname-used-for
It is not filled by default so if you fill it before with the expected values, than it's fine to use.
Assuming c.getName() returns the name of the carBrand, you can do the following:
for (CarBrand car: CarModel.getCarBrandDetails()) {
carDetails.put(car.getCarBrand(), new Color(
rgb[i][0], rgb[i][1], rgb[i][2]));
i++;
}
if (carTable != null) {
for (Component c : carTable.getComponents()) {
if(carDetails.containsKey(c.getName()) {
c.setForeground(carDetails.get(c.getName())));
}
}
}
In your code you are not using the values from carDetails map you filled before. By calling ´carDetails.get()` method you reuse the created Color object.
The project I am working on requires a text field for the user to enter the width of an ellipse. When the user clicks somewhere on a panel, it draws an ellipse with the specified width. When I ran it, the width never changed.
This is in initialize():
tTextWidth = new JTextField();
tTextWidth.setBounds(42, 457, 86, 20);
frame.getContentPane().add(tTextWidth);
tTextWidth.setColumns(10);JButton tSetWidth = new JButton("Set Width");
tSetWidth.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SetTextToWidth(tTextWidth.getText());
}
});
This is right after initialize():
public void SetTextToWidth(String tWidth)
{
if(tWidth == null)
{
tWidth = "50";
}
int tIntWidth = Integer.parseInt(tWidth);
if(tIntWidth == 0)
{
tIntWidth = 50;
}
RoundSprite tSpriteWidth = new RoundSprite();
tSpriteWidth.SetSpriteWidth(tIntWidth);
}
This is in the class RoundSprite:
private float mX;
private float mY;
int mWidth;
int mHeight;
Color mColor;
void DrawSprite(Graphics2D g2)
{
AffineTransform tOldTransform = g2.getTransform();
g2.setColor(mColor);
g2.translate(mX, mY);
g2.draw(new Ellipse2D.Double(0, 0, mWidth, mHeight));
g2.setTransform(tOldTransform);
g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
}
public void SetSpriteWidth(int tWidth)
{
mWidth = tWidth;
}
So two main things...
One...
ActionListener will only be triggered when the user presses the action key for the platform, in most cases the Enter key, just so you know ;)
Two...
In your SetTextToWidth is creating a new instance of RoundSprite which has no context to what is been displayed on the screen...
I you thinking, this would mean that ALL instances RoundSprite should be changed, which is not what you want.
As discussed in this simular question, you first need to define which sprite you are actually trying to change and then apply the change you want to that specific instance (and repaint the output)...
Side Notes
You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
So, I have this JTexrtArea which is almost perfect for my needs. The only thing wrong with it is the line spacing. I can't set it. (Why not JTextPane? Because spacing CAN be changed in JTextArea and JTextArea is way lighter thatn JTextPane, and I have a bunch of those in my program).
I have asked this question before, and this is the answer that I got from user StanislavL:
To override JTextArea's line spacing take a look at the PlainView (used to render PLainDocument).
There are following lines in the public void paint(Graphics g, Shape a) method
drawLine(line, g, x, y);
y += fontHeight;
So you can adapt the rendering fixing y offset.
In the BasicTextAreaUI method to create view. Replace it with your own implementation of the PlainView
public View create(Element elem) {
Document doc = elem.getDocument();
Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
// build a view that support bidi
return createI18N(elem);
} else {
JTextComponent c = getComponent();
if (c instanceof JTextArea) {
JTextArea area = (JTextArea) c;
View v;
if (area.getLineWrap()) {
v = new WrappedPlainView(elem, area.getWrapStyleWord());
} else {
v = new PlainView(elem);
}
return v;
}
}
return null;
}
I grasp the general idea of what he's telling me to do, but I don't know how to do it.
Also, I wouldn't like to override the default JTextArea "property", I'd like to have a choice - to use the default one or to use a custom one.
Only change in JTextArea code would be from
y += fontHeight,
to
y+= (fontHeight +(or -) additionalSpacing).
How do I achieve this?
Which classes do I use/copy?
Where do I put them?
How do I make them usable?
How do I get the whole thing working?
If you think this is too specific to be useful, maybe someone could write a general tutorial on how to create a custom swing component based 100% on an existing one. Then someone could easely change some values to better adjust it to it's needs.
I am simply going to copy-paste my answer from your other question.
I'd like to change the spacing inbetweem the rows of a JTextArea
My first thought was that overriding javax.swing.JTextArea#getRowHeight would be sufficient. The javadoc clearly states
Defines the meaning of the height of a row. This defaults to the height of the font.
So I was hoping that by overriding this method, you would adjust the definition and you would get more spacing between the rows. Bummer, didn't work. A quick search on the usages of that method in the JDK revealed the same. It is mainly used to calculate some sizes, but certainly not used when painting text inside the component.
By looking at the source code of the javax.swing.text.PlainView#paint method, I saw that the FontMetrics are used, and those you can easily override in the JTextArea. So second approach was to extend the JTextArea (bwah, extending Swing components but it is for a proof-of-concept)
private static class JTextAreaWithExtendedRowHeight extends JTextArea{
private JTextAreaWithExtendedRowHeight( int rows, int columns ) {
super( rows, columns );
}
#Override
public FontMetrics getFontMetrics( Font font ) {
FontMetrics fontMetrics = super.getFontMetrics( font );
return new FontMetricsWrapper( font, fontMetrics );
}
}
The FontMetricsWrapper class basically delegates everything, except the getHeight method. In that method I added 10 to the result of the delegate
#Override
public int getHeight() {
//use +10 to make the difference obvious
return delegate.getHeight() + 10;
}
And this results in more row spacing (and a caret which is way too long, but that can probably be adjusted).
A little screenshot to illustrate this (not as nice as some of the other ones, but it shows that this approach might work):
Small disclaimer: this feels like an ugly hack and might result in unexpected issues. I do hope somebody comes with a better solution.
I personally prefer the solution StanislavL is proposing, but this gives you an alternative
That's a piece of code. It's not finished. Line spacing between wrapped lines is not implemented. You can get full source of WrappedPlainView or PlainView and add your code there to achieve desired line spacing
import javax.swing.*;
import javax.swing.plaf.basic.BasicTextAreaUI;
import javax.swing.text.*;
public class LineSpacingTextArea {
public static void main(String[] args) {
JTextArea ta=new JTextArea();
JFrame fr=new JFrame("Custom line spacing in JTextArea");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ta.setText("Line 1\nLine 2\nLong text to show how line spacing works");
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setUI(new CustomTextAreaUI());
fr.add(new JScrollPane(ta));
fr.setSize(100,200);
fr.setLocationRelativeTo(null);
fr.setVisible(true);
}
static class CustomTextAreaUI extends BasicTextAreaUI {
public View create(Element elem) {
Document doc = elem.getDocument();
Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
// build a view that support bidi
return super.create(elem);
} else {
JTextComponent c = getComponent();
if (c instanceof JTextArea) {
JTextArea area = (JTextArea) c;
View v;
if (area.getLineWrap()) {
v = new CustomWrappedPlainView(elem, area.getWrapStyleWord());
} else {
v = new PlainView(elem);
}
return v;
}
}
return null;
}
}
static class CustomWrappedPlainView extends WrappedPlainView {
public CustomWrappedPlainView(Element elem, boolean wordWrap) {
super(elem, wordWrap);
}
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
super.layoutMajorAxis(targetSpan, axis, offsets, spans);
int ls=spans[0];
for (int i=0; i<offsets.length; i++) {
offsets[i]+=i*ls;
}
}
}
}
I want to reduce the height of my autocomplete field.How to go about it
heres my code
HorizontalFieldManager hfm = new HorizontalFieldManager();
LabelField lbl = new LabelField(" Name: ");
final AutoCompleteField TextField1 = new AutoCompleteField(filterLst)
{
public int getPreferredWidth()
{
return Display.getWidth()/2;
}
public void sublayout(int maxWidth, int maxheight)
{
super.sublayout(getPreferredWidth(), getPreferredHeight());
setExtent(getPreferredWidth(), getPreferredHeight());
}
};
hfm.add(lbl);
hfm.add(TextField1);
add(hfm);
The picture below is how it looks. I want it to look the same size as my editfields that have been used for other labels.
Here's my code for editfield
//Add box next to field for containing input
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
LabelField lbl1 = new LabelField(" Amount: ");
final EditField TextField2 = new EditField()
{
boolean _drawFocus = false;
protected void layout(int maxWidth, int maxHeight)
{
super.layout(Math.min(maxWidth, 300), Math.min(maxHeight, 30));
}
protected boolean keyChar(char ch, int status, int time)
{
if (CharacterUtilities.isDigit(ch) || (ch == Characters.BACKSPACE))
{
return super.keyChar(ch, status, time);
}
return true;
}
protected void drawFocus(Graphics graphics,boolean on)
{
_drawFocus = on;
super.drawFocus(graphics, on);
_drawFocus = false;
}
protected void paint(Graphics g)
{
if ( _drawFocus )
{
super.paint(g);
return;
}
g.clear();
g.drawRect(0,0, 50, 50);
int oldColor = g.getColor();
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
g.setColor(oldColor);
g.drawRect(100, 100, 50, 50);
super.paint(g);
}
};
TextField2.setBorder(BorderFactory.createRoundedBorder(new XYEdges(6,6,6,6)));
hfm1.add(lbl1);
hfm1.add(TextField2);
add(hfm1);
I would like to have the size of autocompletefield used for name same as other fields.Please help.
Thanks
One thing that's probably causing you trouble is that EditField is what people normally think of as a Field. AutoCompleteField, however, is handled more like a Manager. It wants to be a Manager, of course, because it (probably) contains an EditField, but then will also contain another child Field which shows the autocomplete options dynamically.
(Non-Manager) Field and Manager subclasses handle layout a little differently, so I found that trying to fix this with the normal getPreferredHeight() and layout() and sublayout() didn't work that well for me.
So, what I did was twofold:
First, I tried to mimic the decoration of the default AutoCompleteField with your EditField subclasses. It's not perfect. If you want them to look exactly alike, you might need to write custom paint() methods for both. You stated the problem as simply wanting to resize the fields, so I thought that was good enough.
Second, since the AutoCompleteField seems to contain a child EditField (at least logically ... I'm not sure if it's implemented that way), I decided to try to get that EditField to choose its own size, in a way that matched the normal EditFields. To do that, I controlled all fields' height by simply setting the same Font on each.
My changes to your code:
final int fontSize = 24; // pick whatever you like here
final int pad = 2;
final int margin = 2;
I removed the code in your edit field's layout() method that was attempting to control height, since ... as I said, I didn't have success setting the autocomplete field's height in a similar way. I just changed your edit field's layout() to a very standard implementation:
final EditField TextField2 = new EditField()
{
boolean _drawFocus = false;
protected void layout(int maxWidth, int maxHeight)
{
super.layout(getPreferredWidth(), getPreferredHeight());
}
Then, I set the padding on your edit field, because it looks to me like AutoCompleteField uses a pad of 2 pixels (on a 5.0 Storm2). You may not care, for this UI, but it also appears to have a margin of 2 pixels.
TextField2.setPadding(pad, pad, pad, pad);
TextField2.setMargin(margin, margin, margin, margin);
Then, I set the same font for all your fields. It doesn't have to be the default font. Just make them all the same. This step is what seemed to dictate the visible size of the rounded rectangle field drawn by AutoCompleteField ... setting the font.
Font font = Font.getDefault().derive(Font.PLAIN, fontSize);
TextField1.setFont(font);
TextField2.setFont(font);
After that, the edit fields and autocomplete fields should be a consistent height. I tested this on a 5.0 9550. Since the pad and margin values were determined experimentally, I can totally believe that those values (e.g. 2 pixels) could change on different devices. You may have to experiment a bit.
I made some menu and it is to update conmmon variables (for text on grid) then the out-of-focus dialog must repaint the grid. Here is the screenshot:
The main control panel is always at top position and 'Data Display' panel is always sitting behind it. When press a button on front panel, Data Display must update its grid. Currently, the common variable 0.4 on the grid is updated by adding listener and works fine. But the grid itself is not repainting anymore. How can I repaint the out-of-focus dialog in real time?
Here is the code of the front panel:
public class MainDisplayForm extends javax.swing.JFrame {
Storage st = new Storage();
DisplayForm dF = new DisplayForm();
....
public MainDisplayForm() {
initComponents();
Btn_IncreaseGain.addActionListener(new ButtonListener_IncreaseGain());
}
....
} //MainDisplayForm ends here.
class ButtonListener_IncreaseGain implements ActionListener {
DisplayForm dF = new DisplayForm();
Storage st = new Storage();
ButtonListener_IncreaseGain()
{
}
public void actionPerformed(ActionEvent e) {
st.iGain = 20;
dF.revalidate();
dF.repaint();
System.out.println("Testing");
}
}//Listener ends here.
Here is code of Data Display:
public void paint(Graphics g)
{
g2 = (Graphics2D) g;
paintComponents(g2);
//added numbers are for adjustment.
int x = this.jPanel1.getX()+8;
int y = this.jPanel1.getY()+30;
int width = this.jPanel1.getWidth()+19;
int height = this.jPanel1.getHeight()+40;
//labelling voltages
label0.setText(st.zero);
label1.setText(st.v1);
label2.setText(st.v2);
label3.setText(st.v3);
label4.setText(st.v4);
label5.setText(st.v3);
label6.setText(st.v4);
g2.setColor(Color.darkGray);
for(int i=x; i<width; i=i+80)
{
g2.drawLine(i, y, i, height);
}
int j = 0;
for(int i=y; i<height; i=i+80)
{
j++;
//st.iGain
g2.setColor(Color.orange);
if(j==1)
{
double k1 = st.iGain * 0.4;
st.v1 = Double.toString(k1);
g2.drawString(st.v1, x+5, y+10);
}
if(j==2)
{
double k2 = st.iGain * 0.3;
st.v2 = Double.toString(k2);
g2.drawString(st.v2, x+5, y+90);
}
g2.setColor(Color.DARK_GRAY);
g2.drawLine(x, i, width, i);
....
} //grid info is not completed yet.
Thanks,
Focus isn't the issue and has nothing to do with your current problem. The solution is to change the properties of the data grid by updating fields it contains via setter methods and calling repaint on the JComponent (perhaps a JPanel, or some other component that derives ultimately from JComponent) held by the data grid. The paintComponent method of this component should use its class fields to update what it draws.
You almost never paint in the paint method of a JComponent and certainly you don't want to draw directly into a top-level window. You also probably don't want to set text of JLabels, JTextFields, or any other JTextComponent. from within paint/paintComponent.
I can't see why your code is not working and can only guess that the likely cause of your problem is in code not shown.
Edit 1:
Just guessing, but you may have a problem of references. I notice that your listener class creates new DisplayForm and Storage objects:
DisplayForm dF = new DisplayForm();
Storage st = new Storage();
There's a good possibility that these objects are not the ones being displayed, especially if you create these objects elsewhere and display them. Again I'm just guessing since I don't see the rest of your code, but perhaps you should to pass references for these objects into the DisplayForm via constructor or setter method parameters.
Edit 2:
e.g.,
public void setDisplayForm(DisplayForm dF) {
this.dF = dF;
}
// same for Storage
And in the main program:
public MainDisplayForm() {
initComponents();
ButtonListener_IncreaseGain btnListenerIncreaseGain = new ButtonListener_IncreaseGain();
btnListenerIncreaseGain.setDisplayForm(....);
btnListenerIncreaseGain.setStorage(....);
Btn_IncreaseGain.addActionListener(btnListenerIncreaseGain);
}