November Black Friday C# week
Black friday is here! Get up to 80 % extra points for free! More info
Only this week up to 80 % off on C# courses. More info

Discussion: using jTable to create preview

Activities (2)
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
David Capka
ICT.social team
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka: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
David Capka
ICT.social team
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka: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
David Capka
ICT.social team
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka: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
David Capka
ICT.social team
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka: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
David Capka
ICT.social team
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka: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.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

11 messages from 11 displayed.