I have a Tabbed Layout in my application
Please see this Image
So as you can see in the above Image I've 2 Tables in 2-different Tabs in first tab (SALON STOCK tab)
i want to remove the selected row
what did is : removebutton's actionPerformed Method
int row_num =jTable4.getSelectedRow();
try{dtm_stock.removeRow(row_num);}
catch(ArrayIndexOutOfBoundsException e){
JOptionPane.showMessageDialog(this,"Please select a Product");
}
this works fine for the current tab(SALON TAB)
but how to implement same for the other Tab (Stock for Sale).
Further Details :
I have 2-table in the 2-diff tabs and bothe have 2-different TableModel (Default)
at initialization the data is set to null
after that the Data is retrieved from database and set to the corresponding TableModel.
now there is a remove button which will remove the selected row from table
i want to remove the selected row from tables irrespective of any Tab
CODE : initialization
jTable4 = new javax.swing.JTable();
dtm_stock = new DefaultTableModel(new Object [][] {
{null,null, null, null},
{null,null, null, null}
},
new String [] {
"ID","NAME", "PRICE", "QUANTITY"
});
jTable4.setModel(dtm_stock
);
ADDING DATA :
//-----ADD STOCK TO THE STOCK TABLE --------------//
try {
ResultSet r7 = con.createStatement().executeQuery("select * from stock");
while(r7.next()){
dtm_stock.insertRow(dtm_stock.getRowCount(),new Object[]{r7.getString("id"),r7.getString("p_name"),r7.getString("price"),r7.getString("qty")});
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this,e.toString());
}
if i'm adding the same TableModel to the other Table then the data goes to that table and the current goes blank:
this seems to have did the trick :`
int selectedIndex = jTabbedPane2.getSelectedIndex();
if(selectedIndex == 0 ){
int row_num =stock_table.getSelectedRow();
try{
dtm_stock.removeRow(row_num);
}
catch(ArrayIndexOutOfBoundsException/*|SQLException*/ e){
JOptionPane.showMessageDialog(this,"Please select a Product");
}
}//if
if(selectedIndex == 1){
int row_num =sale_Stock_table.getSelectedRow();
try{
dtm_sale_stock.removeRow(row_num);
}
catch(ArrayIndexOutOfBoundsException/*|SQLException*/ e){
JOptionPane.showMessageDialog(this,"Please select a Product");
}
}
from here :
enter link description here
if they share same model how can they have different data, please explain
You can control which columns to display in the view (JTable).
Look at the removeColumn(...) method of JTable. It removes a column from the view. However the data is still in the TableModel.
If I understand corectly you can put some flag which will hold value for current visible tab and use if statment to remove row from correct Model. On the other hand better solution would be creating some kind of controller. The controller would store active tab, and would have removeRow method. Switching tab would trigger event so controller would know from which tab delete row.
Related
I am currently reworking an ADF Fusion Web application using Jdev v12.2.1.4.0 (Oracle 12c).
On one of the jsf pages I have a SelectOneChoice inside a table column. The jsf-implementation looks like this:
<af:column
headerText="#{ManagedBean.column5HeaderText}"
sortable="false"
visible="true"
id="c5">
<af:selectOneChoice
binding="#{ManagedBean.bindingErrorCaseSelectOneChoice}"
label="error case"
unselectedLabel="---"
autoSubmit="true"
id="soc1">
<f:selectItems value="#{ManagedBean.errorCases}" id="si1"/>
</af:selectOneChoice>
</af:column>
I left out the required attribute because it is not necessary for the process to select a value here.
The coherent parts of my ManagedBean.java are as following:
//declaring
private RichSelectOneChoice bindingErrorCasesSelectOneChoice;
private List<SelectItem> errorCases = new ArrayList<SelectItem>();
//...
//populating errorCases List from a database
public void getErrorCasesFromDB() {
errorCases= new ArrayList<SelectItem>();
try {
//HC is a helper class to connect to a specific database
Conection conn = HC.getConn();
PreparedStatement pstmt = conn.prepareStatement("some SQL");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
errorCases.add(new SelectItem("i"+ rs.getRow(), rs.getString(1)));
}
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
As I run the jsf page the SelectOneChoices inside the table get rendered and all the expected items are enlisted. I am facing a problem whenever i try to access the selected item of the SelectOneChoice.
I want to read the value of the selectedItem when I hit a button on the page, so I figured I could leave out having to deal with that in a valueChangeListener, and did the following in my button action:
public void buttonSaveReceivedResults(ActionEvent actionEvent) {
//...
if (bindingErrorCaseSelectOneChoice.getValue != null) {
//... insert the selected value into an SQL statement
//in the case the unselected label is selected, skip
System.out.println(bindingErrorCasesSelectOneChoice.getValue().toString())
}
}
This block always gets skipped. Also when i inspected the process, the getValue() call always returned null, even if i select an item from the list.
Now i'm asking you guys, where is the missing part in the chain? Did I do the data bindings correctly. Do I access the elements in the wrong way? Thanks in advance.
The value attribute stores the output of af:selectOneChoice component. Since you have not added it, value returned was null.
I want to display the set of records in rows and columns. Am getting output but the thing is, it is getting overlapped. should i modify the loop can someone pls suggest.
ArrayList<ResultRecord> Records = new ArrayList<ResultRecord>(MainRestClient.fetchResultRecords(this.savedMainLog));
for(j=0; j<Records.size(); j++)
{
Row<PDPage> row4 = table.createRow(100.0f);
Cell<PDPage> cell10 = row4.createCell(15.0f, temp.getNumber());
Cell<PDPage> cell11 = row4.createCell(45.0f, temp.getDescription());
Cell<PDPage> cell12 = row4.createCell(15.0f, temp.getStatus());
Cell<PDPage> cell13 = row4.createCell(25.0f, temp.getRemarks());
}
The below is the full code for opening a PDF file. I want to retreive set of records in the row4 in the corresponding cells. But the is over written one above the another.
Expected output:
IT should display one below the another.
Is the overlapping reason,is it because of defining the row as row4.
try {
//table.draw();
cell.setFontSize(12);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
First of all, you should clarify the table drawing library you use. PDFBox only is the underlying PDF library. Considering the classes used I would assume you are using Boxable on top of it.
Furthermore, the reason why all the tables are printed over each other is that you start each table at the same position on the same page, you use
BaseTable table = new BaseTable(yPosition, yStartNewPage,
bottomMargin, tableWidth, margin, document, page, true, drawContent);
without ever changing yPosition or page.
To get one table after the other, you have to update yPosition and page accordingly, e.g. by using the return value of table.draw() and the state of table then, i.e. by replacing
table.draw();
by
yPosition = table.draw();
page = table.getCurrentPage();
I have created a JDropDown menu. I need a submenu in a different place on JFrame, basically, it's another JDropDown menu. and It shows with a condition like No dropdown menu should show at the start but after a click on any dropdown option the other dropdown menu shows, and options of that 2nd dropdown based on the main dropdown.
I have tried that code. It shows 2nd dropdown on click on any option of 1st dropdown. but If I add one more same thing with cb.setSelectedIndex(1) it can't work.
String[] choices = {"Redeem","Prepaid", "FRC","Offices","Individual Bags","Gift Subscription","E-Gift Card","My Account","Wp-Admin","Shipstaion","Any date changes"};
final JComboBox cb = new JComboBox(choices); cb.setBounds(134,200, 170,30);
cb.setSelectedIndex(0);
cb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
String[ ] choices1 = {"Redeem Remaining & Redeem Gift Card","Redeem of Tasting Kit Sub","US & NON-US Redeem","Manually Created Vouchers"};
final JComboBox cb1 = new JComboBox(choices1);
cb1.setBounds(100,250, 250,30);
JF.add(cb1);
} catch (Exception e1) { e1.printStackTrace(); }
}
});
expected result should be 2nd dropdown options shows based on the main dropdown option.
Logic: Display drop down (data from TABLE_ONE) in page1.amx and based on the selection it needs to get data from TABLE_TWO, then render the retrieved data into page2.amx. Below is the sample which I have tried,
I have created service class ServiceClass.java in my service package and created DataControl (ServiceClassDC) for that class.
From my FirstPage.amx I'm calling service class method using valueChangeListener in drop down (Drop down value will be populated from DB, let's take TABLE_ONE ID). Below is the piece of code for this logic,
FirstPage.amx
<amx:selectOneChoice value="#{bindings.selectId.inputValue}" label="Select Id" id="soc1"
valueChangeListener="#{ServiceClass.callThisMethod}">
<amx:selectItems value="#{bindings.selectId.items}" id="si1"/>
</amx:selectOneChoice>
Based on the selection using WHERE condition, I got the list of objects in productList which is having the result set data.
ServiceClass.java
public void callThisMethod(ValueChangeEvent valueChangeEvent) {
System.out.println("Selected Value: "+valueChangeEvent.getNewValue());
String selectedValue = valueChangeEvent.getNewValue().toString();
ClassMappingDescriptor descriptor = ClassMappingDescriptor.getInstance(TableTwo.class);
DBPersistenceManager pm = getLocalPersistenceManager();
try{
StringBuffer sql = pm.getSqlSelectFromPart(descriptor);
sql.append(" WHERE ID='"+selectedValue+"'");
sql = pm.constructOrderByClause(sql, descriptor);
ResultSet set = pm.executeSqlSelect(sql.toString(), new ArrayList());
System.out.println("Result set >> "+set);
List productList = pm.createEntitiesFromResultSet(set, (List) descriptor.getAttributeMappingsDirect());
System.out.println("productList "+productList);
} catch(Exception exp){
System.out.println("Exception : "+exp);
}
}
Now, I want to display the List object data (productList) into SecondPage.amx screen. How to do this?
Please comment below, if you want any more details regarding this.
You need to expose your List productList in a public method (so provide a get method) and expose that method in a Data Control. You can then drag and drop it on your page.
Example:
public Product[] getProductArray() {
return (Product[]) productList.toArray();
}
Note that this is an example from a MAF version where Java 1.4 was used!
In my table I render rows contains components. After loading data source to table sometimes there are empty fields (components aren't visible, text - yes). I have not any exceptions.
This happens when I scrolling table content. After refreshing table data source everything is ok.
What's wrong?
In attachement there is an example.
I found solution. I think so that might be an bug.
INVALID SOLUTION:
Load data to table
Scroll table with data to eg. 50% of table content
Load new data to table
Table doesn't display all components
CORRECT SOLUTION:
Load data to table
Scroll table with data to eg. 50% of table content
Scroll table to top, to the first table item
Load new data to table
Table display all components
In Vaadin 7.4.1 there is that same bug. Maybe recreating table should help?
OK. Now it works. This is temporary solution. I re-create table and load data to new table:
private void createTable() {
pzbs = (FilterTable) buildTable();
h.addComponent(pzbs);
h.setExpandRatio(pzbs, 1.0f);
}
private void recreateTable() {
h.removeComponent(pzbs);
createTable();
}
private Component buildRoutes() {
routes = new ListSelect() {
{
setNullSelectionAllowed(false);
setSizeUndefined();
setWidth(100.0f, Sizeable.Unit.PERCENTAGE);
setHeight(100.0f, Sizeable.Unit.PERCENTAGE);
setImmediate(true);
}
};
routes.addValueChangeListener((Property.ValueChangeEvent event) -> {
if (routes.getValue() != null) {
recreateTable();
countAndUpdatePercentage(countCorrectReadouts());
sendedToPSG.select(null);
selectedRoute = (KTREntity) routes.getValue();
updateTable();
}
});
return routes;
}