UI Java: Button which clears the data of a JTable - java

I am quite new to the creation of user interface in Java. I have been using the WindowBuilder to create an interface. I have some data from a table and I would like
The user to check if they are double or not and hit the right button (Double, not double)
Then, the variable double or the notdouble I would like to increase
And in the end, to have the table cleared from the data and display the next data from the table.
In the end, I would like to give the results of the variables (double, notdouble)
I have written a code for action listeners of the buttons, so far in
order to diplay some messages
// DOUBLE BUTTON
JButton btnDouble = new JButton("DOUBLE");
btnDouble.setFont(new Font("Calibri", Font.PLAIN, 12));
btnDouble.addActionListener(new ActionListener() { // For event handling
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the DOUBLE button");}
});
I am just writing here in order to give me some guidelines or where I should look for. Any tutorial available would be very helpful.

How to use Button and
How to use Table

Related

Issue with numbers not appearing in box when respective button is pressed

I'm making a simple calculator in GUI and I have all the code typed and ready, but I'm having trouble with making it so that when the user presses a number button, the respective number appears in the text box above. Do I need to use a radio button? Thanks in advance
I've tried action listeners but they didn't work (or I'm probably using them incorrectly). I've put the code for the 1 button.
JButton num1 = new JButton("1");
num1 = b1;
num1.setSize(50,50);
num1.setLocation(20,200);
num1.setBackground(Color.WHITE);
num1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Just append the number to the JTextField(using setText(String) and getText()) in the actionPerformed function.

how to identify a button in a group of loop generated buttons?

I have a group of loop generated buttons made with this code
this.panelCuerpo.setLayout(new GridLayout(4,5));
for(int i = 1; i<=20; i++){
final JToggleButton b = new JToggleButton(new ImageIcon("/images/available.png"));
panelCuerpo.add(b);
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/available1.png")));
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt){
if(b.isSelected()){
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/busy1.png")));
cantidadBoletas++;
}else{
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/available1.png")));
cantidadBoletas--;
}
System.out.println(cantidadBoletas);
}
});
}
The problem here is that I can't use setText() to compare later cause there's no property to hide that text. How can I compare it later?
PS. Each button has a consecutive number, it's easy to assign that number. The real problem lies in where to put it.
You could:
Use the Action API, which lets you trigger the selected state of the associated button. This allows you to de-couple the button from the underlying "action" it should take. Take a look at How to Use ActionsHow to Use Actions for more details
Use the actionCommand property of the JButton. This allows you to have some kind of "identifier" associated with the button which is independent of the text
Use an array or List to maintain a reference to the buttons
You can maintain a List<JToggleButton> of JToggleButton and fetch element later by the index. Apart from that instead of adding ActionListener in loop you can implement ActionListener which can be used for all buttons and you just need to write b.addActionListener(this); in loop.
NOTE : better to start from i = 0 instead of 1

Is there a listener for a cursor being put into JTextField?

Here is my application. It's a wallet to update my money when I spend or get profit. Look it up on image hosting here http://tinypic.com/r/687bdk/8
Is there a way to detect that a cursor has been put into one of the JTextFields? If there is, then could I dispatch a method that would delete whatever is in the other JTextField? There should only be one JTextField with input, it is unacceptable to have inputs in both text fields.
You can add a FocusListener to each textfield i.e.
JTextField myTextField = new JTextField();
myTextField.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
//when selected...
}
#Override
public void focusLost(FocusEvent e) {
//when not selected..
}
});
Is there a way to detect that a cursor has been put into one of the JTextfields? If there is, than I could dispatch a method that would delete whatever is in the other JTextField.
As a user I'm not too crazy about that design. I've used accounting type applications before where you have two columns (debit/credit) and a number can only be entered into one.
In those applications the number is not removed on focus, it is removed if a value is entered in the other field. This allows for tabbing between fields on the forum without data disappearing just because focus changes.
To implement this type of functionality you would add a DocumentListener to the Document of the text field. Then whenever text is entered into the Document the listener is invoked and you can clear the text from the other text field.
Check out the section from the Swing tutorial on How to Write a DocumentListener for more information and examples.
It called: CaretListener
jTextArea=new JTextArea();
jTextArea.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e){
//your code
}
}
);
It mainly used when you need to know that your caret position is changed.

How to add set of integers in a JTextArea

I have added set of integers to a JTextArea for each button click.
what exactly I want is that I want to add all the integers and display in a separate JTextArea,Also I want to ask whether we can access the value of a variable within an action listener outside the action listener.
Here is the code:
private ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
if(evt.getActionCommand().equals(t.getText()))
{
onec=one.calone(n);
td.append(Double.toString(onec));
td.append("\n");
}
res=Integer.parseInt(td.getText());
}
};
When the user presses button 't' It will keep on adding the integer 'onec' to
textarea 'td' using append method.And I have stored the result from the action
listener into a variable 'res' of double datatype.
private ActionListener listener2 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(tot.getText()))
{
totd.setText(Double.toString(res));
}
}
};
When the user clicks the button 'tot',It should add all the integers in the
textarea 'td' and display it in the textarea 'totd'.
This code is not working.
Please help me this is the last part of my project.
As I don't know what is not working - it would of been good if you explained more clearly - my guess is...
Instead of Double.toString(onec)
Use String.valueOf(onec)
EDIT: If this is not the case, please elaborate on what your problem is, and a fuller code listing.
converting the contents of the textArea to double does not calculate sum. Try looping throught the first textArea reading each value while calculating the sum

If a new Frame show up setEditable(false) , If the user close it setEditable(true)

I want to create this code :
The user enter a numerical value , if he entered character it will throw exception
the field will stop working then another frame show up and display error message
after the user close the new frame , everything return to the way it is
that means the field will work again !
I managed to make the field stop working but I didn't know if the user closed the new frame or not !
here is my try
public void keyReleased(KeyEvent event) {
try{
double l,h,w;
l=Double.parseDouble(input_length.getText());
w=Double.parseDouble("0"+input_width.getText());
h=Double.parseDouble("0"+input_width.getText());
}
catch(NumberFormatException a){
input_length.setEditable(false);
input_height.setEditable(false);
input_width.setEditable(false);
JFrame ErrorFrame = new JFrame("Error");
JPanel content = new JPanel(); ;
ErrorFrame.setContentPane(content);
ErrorFrame.setSize (350, 150);
ErrorFrame.setResizable (false);
ErrorFrame.setLocation (FRAME_X_ORIGIN, 250);
content.setLayout(new FlowLayout());
JLabel text = new JLabel(" ERROR ! please Enter number only ",JLabel.CENTER);
text.setFont(new Font("Arial", Font.PLAIN, 20));
text.setForeground(Color.red);
content.add(text);
ErrorFrame.setVisible(true);
setDefaultCloseOperation(ErrorFrame.EXIT_ON_CLOSE);
int op = ErrorFrame.getDefaultCloseOperation();
if(op == 1 ){
input_length.setEditable(true);
input_height.setEditable(true);
input_width.setEditable(true);}
}
}
1). Do not use new JFrame for error message - use JDialog Here is how
2). h=Double.parseDouble("0"+input_width.getText()); i think that you meant input_height.getText() here, not input_width.getText()
3). After showing your error dialog just clear your text fields - it is ok. When user will close it - he will see them empty.
If you would opt for a modal dialog to show the error message, there is no need to change the editable state of your fields.
Personally as a user I would become quite irritated if a dialog was shown each time I made a typo. For example changing the background color of the text field to red on invalid input, and disabling the OK button (or whatever mechanism you have as user to indicate you are finished editing) is more user-friendly IMO. You can even show a label indicating the errors in your panel, or a tooltip, ... .
I would also recommend a DocumentListener instead of a KeyListener if you want to react on updates of the text in the text field
An example on why I propose to opt for another mechanism to inform the user of the error:
I paste an invalid value in the textfield (e.g. 3x456) and a dialog pops up. Now I want to use my arrow keys to navigate to the error and correct it. This means I have to navigate 3 positions to the left to delete the x. If I use my arrow keys (which are keys as well) I will see this dialog 3 more times during the navigation.

Categories

Resources