Get up to 80 % extra points for free! More info:

Discussion: using jTable to create preview

In the previous quiz, Online Java Quiz, we tested our experience gained from the course.

Activities
Avatar
ATUAHENE OPPONG JONES:1/4/2017 5:36

Please I want to use the jTable in Netbeans to generate records like the image i attached.
thanks for your help.
from Jones.

Reply
1/4/2017 5:36
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/4/2017 9:36

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.

Up Reply
1/4/2017 9:36
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/4/2017 11:10

please am using Swing and not good at JavaFX.
thanks for the help if you can direct the code on swing for me

Up Reply
1/4/2017 11:10
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/4/2017 15:31

This is a code for Swing :)

Edited 1/4/2017 16:31
Up Reply
1/4/2017 15:31
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/5/2017 16:36

please this is the preview i want to show to respect to the image i sent you

private void cashKeyReleased(ja­va.awt.event.Ke­yEvent evt) {
double GetTotal = Double.parseDou­ble(totlw.get­Text().trim());
double CheckCash = Double.parseDou­ble(cash.getTex­t().trim());

if (CheckCash >= GetTotal) {
PREV fs=new PREV();

fs.setVisible(tru­e);
fs. printfrm.setTex­t(null);
fs.printfrm.set­Text("MED ID \tMEDICINE NAME\tQUANTITY\tCOST \n"
+"===========­========================­========================­========================­=========\n");

for (int i = 0; i < jTable1.getRow­Count(); i++) {

fs.printfrm.ap­pend(jTable1.get­ValueAt(i, 0).toString() + " \t" + jTable1.getVa­lueAt(i, 1).toString() +"\t " + jTable1.getVa­lueAt(i, 3).toString() + "\t " + jTable1.getVa­lueAt(i, 4).toString() + "\n"
);

}
fs.printfrm.ap­pend("\t ========\n"
+ " TOTAL COST =GH¢ "+ totlw.getText()­.toString());

} else {
PREV fs=new PREV();
fs.printfrm.set­Text(null);
}PREV fs=new PREV();

Double ans;
Double cas = Double.parseDou­ble(cash.getTex­t().trim());
Double total = Double.parseDou­ble(totlw.get­Text().trim());
ans = cas - total;
chg.setText(Strin­g.valueOf(ans));

// fs.printfrm.ap­pend("\t TOTAL COST =GH¢ "+ totlw.getText()­.toString());
}

Up Reply
1/5/2017 16:36
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/6/2017 4:49

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.

Up Reply
1/6/2017 4:49
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/6/2017 5:42

yes to show and print to file.

Up Reply
1/6/2017 5:42
Technology For Life
Avatar
ATUAHENE OPPONG JONES:1/6/2017 10:58

Please help me to print to file

Up Reply
1/6/2017 10:58
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/6/2017 13:29

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 :)

Edited 1/6/2017 13:29
Up Reply
1/6/2017 13:29
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/10/2017 5:27

Please the first line, second line and third line in your code means what.
Or should I insert my columns there

Up Reply
1/10/2017 5:27
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/10/2017 5:35

Yes, you insert your columns there, it's just a sample how to write text into a file.

Up Reply
1/10/2017 5:35
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
User has been banned:5/26/2021 10:51

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.getPrin­table() 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.

 
Up Reply
5/26/2021 10:51
Avatar
EmilyResnick
Member
Avatar
EmilyResnick:2/16/2023 14:54

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!

 
Up Reply
2/16/2023 14:54
Avatar
Joy Bristol
Member
Avatar
Joy Bristol:2/19/2023 8:55

If you are busy writing text that conveys your attitude towards it as accurately as possible, there are several options, one of them is to stick to the correct use of the adjective as well as choosing the correct tone. To understand where you might be wrong with this, you can use our online adjective finder, which is completely free and easy to use.

 
Up Reply
2/19/2023 8:55
Avatar
EllaBlackburn
Member
Avatar
EllaBlackburn:2/20/2023 8:33

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!

 
Up Reply
2/20/2023 8:33
Avatar
JohnRay
Member
Avatar
JohnRay:2/20/2023 22:36

A great text corrector in just a couple of clicks! By going to this site correct sentence online, you can correct your text quickly and efficiently! What do you require? All you need to do is paste the already prepared text into the site line, and get the finished result! Test your writing skills here!

 
Up Reply
2/20/2023 22:36
Avatar
Zain Al-Hashimi:17. October 2:58

If you're looking to create a preview using JTable, it's a great choice for displaying tabular data in Java. You can populate the table with a model and update it dynamically based on user inputs to show a real-time preview.

For clarity and peace of mind during coding challenges, take a moment to reflect by reading Surah Al-Qalam.

 
Up Reply
17. October 2:58
Avatar
Shalamar Hospital:30. October 10:45

With a focus on accurate diagnosis, the thyroid specialist at Shalamar Hospital uses advanced techniques to manage conditions like hypothyroidism and hyperthyroidism

 
Up Reply
30. October 10:45
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

18 messages from 18 displayed.