How can you display text inside a JProgressBar? i.e. "000/100"
progPanel.setBorder(BorderFactory. createEmptyBorder(10,10,10,10));
timerBar = new JProgressBar(0,100);
timerBar.setOrientation(JProgressBar.VERTICAL);
timerBarLabel = new JLabel("Play");
timerBarLabel.setForeground(Color.white);
progPanel.add(timerBarLabel);
progPanel.add(timerBar);
This is my code for my progress bar.
As noted the documentation for JProgressBar (I'm assuming you are using Java 6), you can use the getValue() method to retrieve the progress bar's current value from the BoundedRangeModel.
So,
int maximum = timerBar.getMaximum();
int value = timerBar.getValue(); // This will be the value from 0 to 100 inclusive
String text = String.format("%d/%d", value, maximum);
The above would result in text containing the string "x/y", where x is the current value of the JProgressBar and y is the maximum value of the same.
If you want to draw this inside the progress bar, you may be interested in setString(String s) and setStringPainted(boolean b),
timerBar.setStringPainted(true);
timerBar.setString(text);
And since the value of the progress bar will be changing, you will want to update the text each time the value changes.
If I understand your question correctly, I would think something as simple this should do the job:
progressBar.setString("Text Here!");
Related
I use JSlider in my project. And in somewhere I need to get the value of slider but getValue() method does not return the current slider value. It always returns the default value.
Here is the related part of my code:
this.slider.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
// This two lines below are about Cytoscape framework. I am taking a column of a table and convert it to List<String>. There are not doing anything about slider.
CyColumn timeColumn = table.getColumn("startTime"); // Getting start time column
List<String> timeList = filter.getTimeFromColumn(timeColumn); // Gets value of start time column without null values
// I printed out slider.getValue() value and it returns default slider value all the time.
sliderLabel.setText(timeList.get(slider.getValue()));
}
});
In another part of my code, I need to change the min and max values of the slider. For that, I use the code below:
public void reCreateSlider(){
// Activity size returns correct.
ArrayList<CyNode> activities = filter.FilterRowByNodeType("activity", "nodeType");
this.slider = new JSlider(0, activities.size());
}
Why slider.getValue() returns default value all the time? Thanks a lot,
Actually I just change the reCreateSlider() method like below and it worked.
public void reCreateSlider(){
// Activity size returns correct.
ArrayList<CyNode> activities = filter.FilterRowByNodeType("activity", "nodeType");
slider.setMinimum(0);
slider.setMaximum(activities.size());
}
If anyone has the same problem, can do this way.
Eclipse 3.6.2 has this bug documented here
https://bugs.eclipse.org/bugs/show_bug.cgi?id=358183
There are a couple of suggested workarounds. The first one did not work for me, but the second one did.
combo.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(final Event e) {
//We need the original text, not the displayed substring which would return methods such as combo.getText() or combo.getItem(combo.getSelectionIndex())
String text = combo.getItem((int) combo.getData("selectionIndex"));
//reset text limit
combo.setTextLimit(text.length());
GC gc = new GC(combo);
//exact dimensions of selected text
int textWidth = gc.stringExtent(text).x;
int magicConst = 14;
int comboWidth = combo.getClientArea().width - magicConst;
//In case the text is wider then the area on which it's displayed, we need to set a textLimit
if (textWidth > comboWidth) {
//find text limit - first we set it according to average char width of our text
int averageCharWidth = textWidth / text.length();
int tempLimit = comboWidth / averageCharWidth;
//sometimes on resize it can happen that computed tempLimit is greater than text length
if (tempLimit >= text.length()) {
tempLimit = text.length() - 1;
}
//then we fine-tune the limit - it must be as precise as possible
while (tempLimit > 0 && (comboWidth < gc.stringExtent(text.substring(0, tempLimit + 1)).x)) {
tempLimit--;
}
//textLimit must not be zero
if (tempLimit == 0) {
tempLimit++;
}
combo.setTextLimit(tempLimit);
}
combo.setText(text);
gc.dispose();
}
});
However, when I implement this, the widget thinks that user has changed some data (state change). This may be because of the call above to
combo.setText(text);
As our system is set up, there is a call to
org.eclipse.ui.forms.ManagedForm.isDirty()
which results in a prompt to the user to save the data every time user exits the form.
I am not familiar at all with SWT or jFace. Can anyone tell me
Is there any other way to get around the Eclipse 3.6 bug?
If not, is there a way for me to clear the dirty state of the Combo box so that the user is not prompted to save?
Thanks
Just setting the text of a Combo won't automatically set a ManagedForm to be dirty. So you must be adding a modify listener to the combo in order to do the set dirty.
You can remove the modify listener from the combo just before you do the setText and then add it back after the setText. This should stop the dirty flag from being set.
Each row contains three cells with RGB values. I use these RGB values to set the background of another cell in the same row. On the cells that need to have the background colored I have a callback that picks up the RGB values and sets the background perfectly. So the whole TableView looks exactly as I needed. I have a color picker and this picker needs to update the selected row containing the three RGB values. I'm able to set the new three RGB values but I also need the cell with the colored background to updated itself to the new RGB values. In the code below I have found a way to do this but I believe this is rather ugly.
#FXML void handleColorPicker(ActionEvent event)
{
int r = (int) (comColorPicker.getValue().getRed()*255);
int g = (int) (comColorPicker.getValue().getGreen()*255);
int b = (int) (comColorPicker.getValue().getBlue()*255);
ComTableView.getSelectionModel().getSelectedItem().setRCom(r);
ComTableView.getSelectionModel().getSelectedItem().setGCom(g);
ComTableView.getSelectionModel().getSelectedItem().setBCom(b);
// we need to kick the cell value so it updates also the background color so we clear and rewrite the text string
String currentName = ComTableView.getSelectionModel().getSelectedItem().getCommodityName();
ComTableView.getSelectionModel().getSelectedItem().setCommodityName(" ");
ComTableView.getSelectionModel().getSelectedItem().setCommodityName(currentName);
}
The above code's last three lines trigger the cell's updateItem but I think I'm doing this in an ugly way. I'm wondering, is there a better way to do it?
There are two ways of doing this:
Use JavaFX property value extractors
Partially rewrite your data class
If you are going down route 2:
Lets assume your data class in the table is ColorData` and has the three attributes:
int RCom
int GCom
int BCom
String CommodityName
Now if you change your CommodityName from String to StringProperty and provide this to the TableView via
commodityNameColumn.setCellValueFactory(cellData -> cellData.getValue().commodityNameProperty());
Where commodityNameColumn is the TableColumn of your TableView displaying the commodityNameProperty() a new method of ColorData giving access to the new StringProperty.
Now if you change the StringProperty via its setter, and the value actually changes, the TableCell will update accordingly.
If there is still something unclear how to link a data class to the JavaFX TableView I recommend this tutorial.
i have done a small test on LibGdx, on Multi-line Label, it seems that i cant get the wrapped line's height. Following is the code. Theoretically, height for aLebel should be > bLabel. But the result appear the same.
code:
aLabel.setText("this is a super long long long text that need wrapping."); // line wrapped into 3 lines
aLabel.setWrap(true);
aLabel.setWidth(470);
doLog("aLabel.getHeight(): " + aLabel.getHeight());
bLabel.setText("this is short."); // unwrapped line
bLabel.setWrap(true);
bLabel.setWidth(470);
doLog("bLabel.getHeight(): " + bLabel.getHeight());
result:
aLabel.getHeight(): 45.0
bLabel.getHeight(): 45.0
Do anyone have any idea how to get the actual multi-line height in LibGdx? Thanks in advance.
I had this issue for years and accidentally solved it by setting the width and packing the label twice. Note that multiline labels were never intended to figure out their own width, so you have to set them externally, preferably from it's parent.
public Label createLabel() {
// Create label and set wrap
Label label = new Label("Some long string here...", skin);
label.setWrap(true);
// Pack label
label.pack(); // This might not be necessary, unless you're changing other attributes such as font scale.
// Manual sizing
label.setWidth(textWidth); // Set the width directly
label.pack(); // Label calculates it's height here, but resets width to 0 (bug?)
label.setWidth(textWidth); // Set width again
return label;
}
LibGDX version used: 1.6.4
Pack sizes the widget to its pref size, nothing more. Pref width of a label with wrapping is 0.
Label label = new Label(...);
label.setWrap(true);
label.setWidth(123);
label.setHeight(label.getPrefHeight());
I had the same issue and it seems there doesn't exist a method in Label class to solve this. Also, I agree with you, the getHeight() method should return the real height of the Actor, so I don't know if that's a bug or there is a reasoning behind that behaviour.
Anyways, how I solved the issue is by using BitmapFont's getWrappedBounds method. It's not short, but for your example it would be the following:
doLog("aLabel.getHeight(): " + aLabel.getStyle().font.getWrappedBounds(aLabel.getText(), aLabel.getWidth()).height);
This could be done by adding a restriction to the cell that contains the Label in the Table:
Label label = new Label("Example", new Label.LabelStyle(font, Color.WHITE));
label.setWrap(true);
Table table = new Table();
table.add(label).width(WITH);
For more information about how to use Table go to: https://github.com/libgdx/libgdx/wiki/Table
I am working on an android app and using AChartEngine for Charting. The Bar Chart is drawn on the basis of the dynamic data coming from a server.
Th Y-Axis labels are set to be shown from 0 to 100 and no of labels are 11 s it shows
0..10..20..30..40..60..70..80..90..100 as Y-Axis Labels. Is it possible to set custom Y-Axis labels such that it adds '%' sign after the Y-Axis title value so that it shows,
0%..10%..20%..30%..40%..60%..70%..80%..90%..100% as Y-Axis label values.
How to do it??
All what you need to do is: Enjoy ;)
renderer.addXTextLabel(0, "0");
renderer.addYTextLabel(10, "10%");
renderer.addYTextLabel(20, "20%");
renderer.addYTextLabel(30, "30%");
renderer.addYTextLabel(40, "40%");
renderer.addYTextLabel(50, "50%");
renderer.addYTextLabel(60, "60%");
renderer.addYTextLabel(70, "70%");
renderer.addYTextLabel(80, "80%");
renderer.addYTextLabel(90, "90%");
renderer.addYTextLabel(100, "100%");
renderer.setYLabels(0);
In order to set custom labels on the Y axis, you just need to use the following method:
mRenderer.addYTextLabel(10, "10%");
mRenderer.addYTextLabel(20, "20%");
...
Also, if you want to hide the default labels, do this:
mRenderer.setYLabels(0);
I haven't actually done a lot with AChartEngine, but a quick glance at the source code suggests you could extend BarChart to accomplish what you're after.
Have a look at the getLabel(double label) method located in AbstractChart (BarChart extends XYChart, which on its turn extends AbstractChart).
/**
* Makes sure the fraction digit is not displayed, if not needed.
*
* #param label the input label value
* #return the label without the useless fraction digit
*/
protected String getLabel(double label) {
String text = "";
if (label == Math.round(label)) {
text = Math.round(label) + "";
} else {
text = label + "";
}
return text;
}
I would start with something naive to see how that works out; e.g. simply append "%" on the result of above:
#Override protected String getLabel(double label) {
return super.getLabel(label) + "%";
}
Only thing I'm not too sure about is whether the same method is used to generate labels for the X-axis. If that's the case, you'll probably need to do something slightly smarter to enable it just for the axis you're interested in.