i have to write a Cashier's program. Assuming that, if i have a JFrame
that includes 2 JTextField
s, to add the item kind and price. I have one JButton
, to create if i want that item which is added by the user to be like JButton
in new JFrame
called Sell when i pressed that JButton
will pass the value of price and item kind to JTable
. Is there a way to achieve this!!!!
The code for getting the item from database and but the result in JTable
but i dont know the code to create JButton
try {
String host = "jdbc:derby://localhost:1527/PROCAT";
String uName = "zain";
String uPass = "zain";
Connection con = DriverManager.getConnection( host, uName, uPass);
String sql = "Select * from ITEMB where ITEM =2";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String myNamatxt = rs.getString("PRISE");
String myHargatxt = rs.getString("ITEMNAME");
String satuan = rs.getString("ITEM");
String[] data = {myNamatxt, myHargatxt, satuan,};
tabMode.addRow(data);
double price = Double.parseDouble(rs.getString("prise"));
totalpay = price + totalpay;
++rowCount;
}
} catch (Exception e) {
//ignore
}
jTextField3.setText(String.valueOf(totalpay));
The requirements of the question is still unclear, though if you be able to put some more light on it, then many of us be able to help you on the topic. Take the help of Google Translate, just in case English is not the language you comfortable with.
If I understood the question in the right sense, you want to enter two values in two fields, ITEM and PRICE. Now at the click of a JButton
, you wanted to pass those items to the JTable
. If that be the case, a small program for your help is as below:
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class JTableExample {
private JFrame frame;
private JTextField itemTField;
private JFormattedTextField priceFTField;
private NumberFormat priceFormat;
private JButton submitButton;
private Vector<String> columnNames = new Vector<String>();
private JDialog sellingDialog;
private JTable table;
private DefaultTableModel model;
private ActionListener buttonAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (!sellingDialog.isShowing()) {
model.setRowCount(0);
sellingDialog.setVisible(true);
}
Vector<Object> rowData = new Vector<Object>(2);
String item = itemTField.getText().trim();
double price = Double.parseDouble(
priceFTField.getText().trim());
rowData.add(item);
rowData.add(new Double(price));
model.addRow(rowData);
}
};
public JTableExample() {
columnNames.add("Item");
columnNames.add("Price");
}
private void displayGUI() {
frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridLayout(2, 1, 5, 5));
JPanel headerPanel = new JPanel();
itemTField = new JTextField(10);
priceFormat = NumberFormat.getInstance();
priceFormat.setMaximumFractionDigits(2);
priceFTField = new JFormattedTextField(priceFormat);
priceFTField.setColumns(10);
headerPanel.add(itemTField);
headerPanel.add(priceFTField);
JPanel footerPanel = new JPanel();
submitButton = new JButton("SUBMIT");
submitButton.addActionListener(buttonAction);
footerPanel.add(submitButton);
contentPane.add(headerPanel);
contentPane.add(footerPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
createDialog();
}
private void createDialog() {
sellingDialog = new JDialog(frame, "Sell Items: ", false);
JPanel contentPane = new JPanel(new BorderLayout(5, 5));
model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
table = new JTable(model);
table.setPreferredScrollableViewportSize(
new Dimension(200, 200));
table.setFillsViewportHeight(true);
JScrollPane itemScroller = new JScrollPane();
itemScroller.setViewportView(table);
contentPane.add(itemScroller);
sellingDialog.setContentPane(contentPane);
sellingDialog.pack();
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new JTableExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Though on a side note, restrain yourself from using multiple JFrame
in a single application, do consider going through the link as already posted in the comments.