Discussion: using jTable to create preview
In the previous quiz, Online Java Quiz, we tested our experience gained from the course.

Member

14 messages from 14 displayed.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
In the previous quiz, Online Java Quiz, we tested our experience gained from the course.
Hello Atuahene, do you mean jTable from Java Swing?
The Swing jTable is a little bit tricky, you need to create a model:
ArrayList<Drug> drugs = ... // Your collection of objects
yourJTable.setModel(new AbstractTableModel() {
String[] columnNames = {"Drug", "Unit cost", "Quantity", "Total"};
@Override
public String getColumnName(int col) { return columnNames[col]; }
@Override
public int getRowCount() { return drugs.size(); }
@Override
public int getColumnCount() { return columnNames.length; }
@Override
public Object getValueAt(int row, int col) {
Drug drug = drugs.get(row);
switch (col)
{
case 0:
return drug.getName();
case 1:
return drug.getPrice();
case 2:
return drug.getQuantity();
case 3:
return drug.getPrice() * drug.getQuantity();
}
return drug;
}
@Override
public boolean isCellEditable(int row, int col) { return false; }
};
);
However, you should be using JavaFX instead, Swing is an old framework.
please am using Swing and not good at JavaFX.
thanks for the help if you can direct the code on swing for me
This is a code for Swing
please this is the preview i want to show to respect to the image i sent you
private void cashKeyReleased(java.awt.event.KeyEvent evt) {
double GetTotal = Double.parseDouble(totlw.getText().trim());
double CheckCash = Double.parseDouble(cash.getText().trim());
if (CheckCash >= GetTotal) {
PREV fs=new PREV();
fs.setVisible(true);
fs. printfrm.setText(null);
fs.printfrm.setText("MED ID \tMEDICINE NAME\tQUANTITY\tCOST \n"
+"============================================================================================\n");
for (int i = 0; i < jTable1.getRowCount(); i++) {
fs.printfrm.append(jTable1.getValueAt(i, 0).toString() + " \t" +
jTable1.getValueAt(i, 1).toString() +"\t " + jTable1.getValueAt(i,
3).toString() + "\t " + jTable1.getValueAt(i, 4).toString() + "\n"
);
}
fs.printfrm.append("\t ========\n"
+ " TOTAL COST =GH¢ "+ totlw.getText().toString());
} else {
PREV fs=new PREV();
fs.printfrm.setText(null);
}PREV fs=new PREV();
Double ans;
Double cas = Double.parseDouble(cash.getText().trim());
Double total = Double.parseDouble(totlw.getText().trim());
ans = cas - total;
chg.setText(String.valueOf(ans));
// fs.printfrm.append("\t TOTAL COST =GH¢ "+
totlw.getText().toString());
}
And you want to print the table to the console? Or into the file? I thought you just want to fill a jTable with your data.
yes to show and print to file.
Please help me to print to file
It's easy. Use the BufferedWriter class to do it:
try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt")))
{
bw.write("First line");
bw.newLine();
bw.write("Second line");
bw.newLine();
bw.write("The last line.");
bw.newLine();
bw.flush();
}
catch (Exception e)
{
System.err.println("Unable to write to the file."); // Or any other error message handling
}
Don't forget to add imports, NetBeans will add them for you if you click on
the light-bulb icon
Please the first line, second line and third line in your code means
what.
Or should I insert my columns there
Yes, you insert your columns there, it's just a sample how to write text into a file.
HI,
Doubt if it's still of interest, but the problem is that TablePrintable is
single-use. There's no way to reset it for a second pass.
Calling jTable.getPrintable() a second time gets a fresh TablePrintable, so you need to use the first for counting pages, and the second for actual printing. JAVA Training "JAVA Training ":https://www.sevenmentor.com/…-in-pune.php
Actually, you don't actually need to count the pages. getNumberofPages() can return zero; just end the print loop for when it runs out of pages.
An alternative workaround would be to copy the source of TablePrintable and add a reset method for variable 'last' and the Rectangle values.
mHello! To make your text impeccable and of high quality, you need to make a lot of effort and also carefully monitor the correct use of the verb tense in the text. Our present tense generator will help you with this! This is the best tool not only for checking the verb tense, but also for complex editing of your text!
Hello! For a couple of years now I have been checking my texts with this passive voice fixer and I want to say that it saves me a lot of time and also greatly improves the quality of my text! So I am happy to share this recommendation with you because it will qualitatively improve your texts!
14 messages from 14 displayed.