I have a Jpanel that contain a JTabel and JTextField that display the value of the Jtable row when the row is clicked twice. the user can edit the JTextField value than click a button to update once the button is click the updateUser method will be called. I am trying to write a JUnit test to test that the updateUser is updating user.
How can I perform double click on a specific table row to get the data displayed in the specified JTextField?
public void updateUser(){
String id= txtid.getText();
String name =txtName.getText();
String q = "UPDATE `user` SET `name`=? WHERE `id`=?";
try{
PreparedStatement ps = Login.con.prepareStatement(q);
ps.setString(2, id);
ps.setString(1, name);
ps.executeUpdate();
JOptionPane.showConfirmDialog(null, "User Updated");
}catch(Exception eu){
}
}
Unittest should test the Unit, specefically the logical unit, in your case i would change the updateUser() method to:
bool updateUser(String id, String name)
which you can test then undependant from the GUI elements.
The showConfirmDialog can then be shown according to the return value of the updateUser method.
Related
I wants to mock the view result in junit which is from repository class.
#Modifying
#Query(value = "Select * from v_students", nativeQuery = true)
public String studentsDetails();
Inside view (v_students) have a query, (select name, id, roll from students);
Now I wants to write a test case for this view in junit, by inserting dummy value for name, id, roll. How we can achieve this in JUNIT for view with out manual DB insert.
I have the following properties to display in the Vaadin Table
public class Item
{
String itemName;
String itemSource;
String itemStatus;
...
}
Next, I retrieve my data using the ItemContainer, and connect it to the table:
ItemContainer container = new ItemContainer(Item.class, app);
table.setContainerDataSource(container);
The table will display the data based on Item class.
The question: can I change "somehow" the datatype of the fields on the "fly" ? So I want change the "itemStatus" to be Label since display HTML tags in correct styles.
For example the text in "itemStatus" is: <b>Status is:</b><i>Completed</i>. I want to see in the table formatted string Status is:Completed
You can override a container property in Table by adding a generated column with the same id:
tablet.addGeneratedColumn("itemStatus", new ColumnGenerator() {
#Override
public Object generateCell(Table source, Object itemId, Object columnId) {
Label label = new Label("" + source.getContainerProperty(itemId, columnId).getValue());
label.setContentMode(ContentMode.HTML);
label.setSizeUndefined();
return label;
}
});
Mojarra 2.1.5 / Java
I have two entities : i.e.
class Primary { String name; String age }
class Second { String dept; String hour }
...
In my managed bean I developed a function to generate PDF regarding my front-end prime-faces radio button (Primary or Second).
If I select in radio button the Primary option, the managed bean method will fire generatePDF() and inside the generatePDF I have :
Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
...
...
But how can I do to re-utilize the same method generatePDF for both entities (Primary and Second ? I need to access both entity properties regarding my radio selection.
I need to instantiate the entities dynamically (Or I instantiate Primary or I intantiaty Second at a time)
What about do something like this.
interface Pdfeable{ String writeToPDF();}
class Primary implements Pdfeable { String name; String age }
class Second impleaments Pdfeable { String dept; String hour }
Just override with the statements you want to send data to the PDF.
class Primary implements Pdfeable {
String name; String age;
public String writeToPDF(){
return getName() + "" + getAge();
}
}
And write your code using the interface definition not concrete classes.
Assuming as per your question you need one class instantiation at a time as per radio button selection ,I would suggest you should create a valueChangeListener to your radio button.I have hardcoded the values of radio selectItem you can use any way either by bean-binding or hardcoded.
<h:selectOneRadio value="#{myBean.myRadioValue}" ... onclick="this.form.submit();" valueChangeListener="#{myBean.generatePDF}">
<f:selectItem itemValue="Radio1" itemLabel="Primary-radio" />
<f:selectItem itemValue="Radio2" itemLabel="Secondary-radio" />
</h:selectOneRadio>
This code will submit the form where the radio button are contained when the onclick Javascript event is detected. On the server side, the action generatePDF will be executed. In this method, you can do your requiste as on Submit action getter and setter will be called and you can check which radio is selected by comparing using if () and do your stuff:
public void generatePDF(ValueChangeEvent evt) {
if(getMyRadioValue.equals(Radio1)){
Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
}
else if(getMyRadioValue.equals(Radio2)){
Secondary s = new Secondary();
s.dept = xxx;
s.hour = yyyy;}
...
...
}
I use Dbutils to call elements in one table of database
private void showEvent(){
try {
String query="SELECT EventID, MemberID,Description, Date,
Time, Venue, EventTypes FROM formal UNION SELECT EventID,
MemberID,Description, Date, Time, Venue, EventTypes FROM social";
ps = c.prepareStatement(query);
rs=ps.executeQuery();
tbShowEvent.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException ex) {
Logger.getLogger(ShowEvent.class.getName()).log(Level.SEVERE, null, ex);
}
}
How to delete one element in table when I click the mouse to that element
Illustration: http://i.stack.imgur.com/ib77V.png
add an event listener on your delete button
create your stored procedure code for the delete in the event listener.
after a succesful delete in the DB delete the row in your table in the table Model
refresh you table. Call fireTableDataChanged() on your tableModel
I'm creating a program that uses several different interacting menus to allow the user to access different parts of the system. I'm using Netbeans to help with the coding.
At the moment I'm stuck on a task.
When a user logs into the system through a "login" form, the system validates the details, and that user is redirected to either a "user_details" or an "admin_menu" depending on the credentials. That much works fine. From there, the user is able to access a form that allows them to update their details which are already saved in the database.
The only way I've found to limit the simple user to update their details is to ask them to login again, and from there retrieve their details so that they can be updated. This process is messy, but it works.
with that in mind how do i retrieve whatever was imputed in the textfield "Username" that is located in the jform Login from another jform (User_details), the User_details jform only opens once the login is successful (once that occurs login is discarded and user_details is opened).
by the way i've done a lot of research but cant seem to find an answer to my problem.
here is part of my log in code :
String sql = "select * from user where Username =? and password=?";
try {
pst = con.prepareStatement(sql);
pst.setString(1, username.getText());
pst.setString(2, password.getText());
rs = pst.executeQuery();
int count = 0;
while (rs.next()) {
count = count + 1;
}
if (count == 1) {
JOptionPane.showMessageDialog(null, "Access Granted");
if ("manager#manage.com".equals(t.getText())) {
rs.close();
pst.close();
close();
Admin_menu am = new Admin_menu();
am.setVisible(true);
} else {
rs.close();
pst.close();
close();
User_details M = new User_details();
M.setVisible(true);
}
} else {
JOptionPane.showMessageDialog(null, "Incorrect Username or Password");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
} finally {
try {
rs.close();
pst.close();
} catch (Exception ex) {
}
}
how can i make the User_details Jform get what ever was imputed in the textfield username?
You will probably want to create some class which represents the state of everything a logged in user does in your application. Traditionally, you would call it a Session. The logged in user is a property of that session, so you keep it in a field there. You pass the session into every frame you make, so that they all have access to it.
The session becomes invalid when the user logs out.
Then from your frames, components, whatever, you can get the current user from the session.
But please, try to separate front-end from back-end code here. Look into the MVC pattern.
try to declare the variables public and initiailize them with the other jframes where you want to use them for example
public your class{
initComponents();
String a;
}
hope this helps :)
You could create a class "BasicView" in wich a class variable exists where you store your information, so every instance of this class can use ist. Then you just need to extend this basic class, or just use BasicView.value or BasicView.getValue().
Also you can use a synchronized method to change this var.
public class BasicView {
private static Object value;
public synchronized void setValue(Object value) {
this.value = value;
}
public Oject getValue() {
return value;
}
.
.
.
}
I use this to init an ServicesLocator to get servies in all my beans. :)
EDIT: Also take a look at the MVC pattern and use this in the controller (Basic Controller) or something like that.
1. Use Composition, and also make sure that your GUI class always returns the same Object when referenced from different JFrames.
2. This can be achieved using Singleton Principle.
Create a class called session like this:
public class Session {
public static String userName = "";
}
When login happens set the value of Session.userName to the given userName and when the user logs out set Session.userName to "". You can access Session.userName simply by typing Session.userName. Other session variables should be defined in the Session class too.