I am using a jprogressbar to indicate the availability status.
i want to display a text of 40%[assumption] inside the progressbar.
how to do it? the text was changed according to the availability value
You can use:
Initialising:
progressBar.setStringPainted(true);
Updating:
progressBar.setValue(newValue);
Use setStringPainted(true) to show the Percentage of work completed.
Use setValue() which will help setting the incremental value and setString() to display the end message when done...
Here is an example from my code base :
final JProgressBar bar = new JProgressBar(0 , 100); // 0 - min , 100 - max
bar.setStringPainted(true);
panel.add(bar); // panel is a JPanel's Obj reference variable
JButton butt = new JButton("start");
butt.addActionListener(){
public void actionPerformed(){
new Thread(new Runnable(){
public void run(){
int x = 0;
while(x<=100) {
x++;
bar.setValue(x); // Setting incremental values
if (x == 100 ){
bar.setString("Its Done"); // End message
try{
Thread.sleep(200);
}catch(Exception ex){ }
}
}).start();
}
});
I am using a jprogressbar to indicate the availability status.
please read tutorial about JProgressBar
i want to display a text of 40%[assumption] inside the progressbar.
Using Determinate Progress Bars in the JProgressBar tutorial
how to do it? the text was changed according to the availability value
more in the SwingWorker tutorial
This will show the progress inside the bar
progressBar.setStringPainted(true);
This shows the progress percentage inside the progress bar
progressBar.setStringPainted(true);
I'm unclear if your [assumption] is part of the string you want displayed. If so, the complete solution would be something like:
private static final String PROGRESS_MASK = "%d%% [assumption]";
public void someMethod() {
...
progressBar.addChangeListener(new ChangeListener() {
#Override
void stateChanged(ChangeEvent e) {
progressBar.setString(String.format(PROGRESS_MASK,
progressBar.getValue()));
}
}
progressBar.setStringPainted(true);
}
... as you would be unable to rely on the default string which merely displays the percentage.
Two things you should notice here. Those are,
1) You have to set paintString variable of JProgressBar using setStringPainted method. You can do that like
jprogressBar.setStringPainted(true)
you have to do this because,
isStringPainted()
method should return true, if the progress bar has to show the values or percentage of progress on it.
2) Now to customize with your custom value, set the your custom on jprogressBar instance using
jprogressBar.setString(customString)
then it should work fine.
Here is the tutorial link which shows how to set the value (i.e. 10% or 40%...) according to the status of the progress bar http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html
Related
I am trying to make a simple dialog box in Beanshell - it should read the contents of three editable text fields and, on button press, carry out a simple task accordingly. I am totally stumped by an error that I am getting where I cannot read the text in some of the fields.
Here’s the code:
// Set up the text fields
textField_Y= new JFormattedTextField();
textField_Y.setValue(150);
textField_Y.setColumns(4);
textField_Y.setEditable(true);
textField_X= new JFormattedTextField();
textField_X.setValue(0);
textField_X.setColumns(4);
textField_X.setEditable(true);
textField_n= new JFormattedTextField();
textField_n.setValue(20);
textField_n.setColumns(4);
textField_n.setEditable(true);
button = new JButton("Generate Stage Position List");
// some Code here to arrange the items within a GUI window
// Try to read the values
button.addActionListener(new ActionListener() {
actionPerformed(ActionEvent eText) {
//Get info from dialog
yShift = textField_Y.getText();
xShift = textField_X.getText();
nPos = Integer.parseInt(textField_n.getText());
print(xshift+" "+yshift+" "+nPos);
});
I run this and the dialog box displays correctly. I don’t change any values, just click the button, and it should print “150 0 20”. Instead it prints “void void 20”. I don’t have the faintest clue why one field is returning a correct number and the other two are returning void. They should all be identical! Can anyone help?
First, looking at this code...
button.addActionListener(new ActionListener() {
actionPerformed(ActionEvent eText) {
//Get info from dialog
yShift = textField_Y.getText();
xShift = textField_X.getText();
nPos = Integer.parseInt(textField_n.getText());
print(xshift+" "+yshift+" "+nPos);
});
yShift != yshift and xShift != xshift. Remember, Java is case sensitive.
I would also recommend making use of getValue instead of getText
I am working on a javafx project in which I'm providing a textbox to be filled in.I want to calculate % of textbox filled in..say for eg 100 characters is a limit and 50 are filled in so 50 should be the % value but it should change automatically as i keep typing .I don't know exactly how to do that (specially the Automatic thing). I want to show that % value on progressbar like this :
(Ignore buttons)
Need help! Thank you in advance
You can define yourself a DoubleBinding which is bound to the textProperty and on each change revaluates it's value.
final double max = 100;
TextField text = new TextField();
DoubleBinding percentage = new DoubleBinding() {
{
super.bind(text.textProperty());
}
#Override
protected double computeValue() {
return text.getText().length() / max;
}
};
In the static initializer block of the DoubleBinding you bind the textProperty of your TextField. This will cause the reevaluation of the binding through the computeValue method. Then you can bind it to the textProperty of a Label:
Label lbl = new Label();
lbl.textProperty().bind(percentage.asString());
Of course you can also bind it to other controls than a Label like a ProgressBar or ProgressIndicator:
ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(percentage);
ProgressIndicator indicator = new ProgressIndicator();
indicator.progressProperty().bind(percentage);
This binding then can be used to display the percentage already filled in. You might also take a look at this documentation by Oracle. The type of this binding is a low-level binding.
I implemented a JFrame that contains some JLable's. I would like to change their appearance once they are clicked. The appended code should do so. In fact: It does not. Taking the same code and putting it into the run of an inner Thread-class does the job. The inner Thread-instance inverts the clicked JLable twice.
Can anybody give me a hint why the mouseClicked-method seems not to be able to affect the clicked JLable's appearance?
#Override
public void mouseClicked(MouseEvent e) {
if (clickable) {
for (Position p : positions.keySet()) {
JLabel lable = positions.get(p);
if (lable == e.getComponent()) {
pickedPosition = p;
LOGGER.info(pickedPosition + " pressed");
synchronized (lable) {
// store old colors
Color obg = lable.getBackground();
Color ofg = lable.getForeground();
// invert them
Color nbg = new Color(255 - obg.getRed(), 255 - obg.getGreen(), 255 - obg.getBlue());
Color nfg = new Color(255 - ofg.getRed(), 255 - ofg.getGreen(), 255 - ofg.getBlue());
// set them
lable.setOpaque(true);
lable.setForeground(nfg);
lable.setBackground(nbg);
// wait a while
try {
lable.wait(WAIT_WHILE_INVERTING_MS);
}
catch (InterruptedException i) {
LOGGER.warn(i.getMessage());
}
// switch back to initial
lable.setBackground(obg);
lable.setForeground(ofg);
}
e.consume();
}
}
}
}
There is no need for the synchronized block of code. All code executed from the event code will execute on the Event Dispatch Thread (EDT). Since you should always update the properties of components on the EDT you don't need to worry about other threads updating the component.
It looks like you want to temporarily change the Color of the label. The problem is the wait() method will block the EDT and prevent the GUI from repainting itself.
You can either:
Use a SwingWorker to start a Thread and then sleep for a period of time. Then when the worker is finished you can restore the color of the label. See Concurrency for more information and examples.
Use a Swing Timer to schedule the changes. See How to Use Swing Timers for more information.
I have a GUI setup with with buttons on them and a JTextArea.
I also have an array of Strings with say size of 3.
What I want to do is use an action listener in a way that when the button called "next" is pressed, the JTextArea will then show the next cell in the array. The only problem is it displays the array at the same time. I need it to display the next cell when the button is hit
Can anyone help me with the code? Please and thank you.
final ActionListener m2 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
arr = new String[3];
arr[0]= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
arr[1]= "sssssssssssssssssssssss";
arr[2]= "xxxxxxxxxxxxxxxxxxxxx";
for (int i = 0; i<arr.length; i++){
text.append(arr[i]);
}
}
};
next.addActionListener(m2);
So the basic concept is. You need a index value to maintain the current index of the array that is being displayed.
From there, each time the user clicks next, you would increment the index and display the next value in the String
public void actionPerformed(ActionEvent e) {
currentIndex++;
// You need to decide what to do when we reach the end of the array...
String value = myStrings[currentIndex];
textArea.setText(value);
}
To create the button, use the JButton class. To respond to events, use the JButton#addActionListener() method. If you are having trouble, post what you have tried. Good luck!
I have an application which opens multiple JIFs, But I only want to create a single instance of the JIF, so I use these function to check that, and I use dispose to close the JIF after a key is pressed(JDesktopPane.getSelectedFrame().dispose()). However after 2-3 successive disposes, it doesn't create a new JIF? Am I doing anything wrong over here?
public static void setInternalFrame(final JInternalFrame internalFrame) {
log.debug("CurActiveInternalFrame " + ShoppyPOSApp.getCurrentActiveInternalFrame(), null);
log.debug("Incoming internalFrame " + internalFrame, null);
boolean isFrameFound = false;
try {
// Have a check whether the DesktopPane contains the internal Frame
// If yes bring it to front and set the focus
for (int i = 0; i < ShoppyPOSApp.frame.mainDesktopPane.getAllFrames().length; i++) {
if (ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i].getClass() == internalFrame.getClass()) {
isFrameFound = true;
}
}
if (!isFrameFound) {
internalFrame.setVisible(true);
internalFrame.setLocation(
ShoppyPOSApp.frame.mainDesktopPane.getWidth()/ 2 - internalFrame.getWidth() / 2,
ShoppyPOSApp.frame.mainDesktopPane.getHeight() / 2 - internalFrame.getHeight() / 2
);
ShoppyPOSApp.frame.mainDesktopPane.add(internalFrame);
}
internalFrame.setSelected(true);
} catch (Exception e) {
log.debug(e.toString(), null);
}
}
You are comparing the classes of your input parameter and your desktops internal frames in your for loop. This will always be true as your parameter is an instance of JInternalFrame and the getAllFrames method returns an array of JInternalFrames. Why not just do a regular comparison? :
ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i] == internalFrame
I would recommend using HIDE_ON_CLOSE as your default close operation on the frames and using setVisible(false) in your key listener instead of dispose(). When frames are disposed they are closed and you should not try and reuse a frame after is has been closed. If you just hide the frame it will still be a child of the desktop pane so you shoud add a call to setVisible(true) when you find a frame in your setInternalFrame method.
It sounds like you're getting inconsistent behaviour (you say it fails after two or three disposes). This suggests to me that you have an event thread problem. Is your setInternalFrame being called on the event thread? Are you familiar with the Event Dispatch Thread and are you using it correctly?
I don't think dispose is doing what you mean for it to do. dispose gets rid of the operating system "peer" of your frame. But if you intend to show that frame again, then you shouldn't throw away its underpinnings!
I'd go with setVisible(false) on the JIF to hide it. Then you can re-activate it with setVisible(true).