0

I have a problem with my data view, when showing my data from database to Jtable. it showing like this, The pesanan column is fully horizontal

what I want is the result showing like this So the pesanan column is show vertical with create new line using \n

this is my code to showing and get data to the table from phpmyadmin.

private void load_table() {
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("pembeli");
    model.addColumn("pegawai");
    model.addColumn("pesanan");
    model.addColumn("harga");
    model.addColumn("pembayaran");
    model.addColumn("waktu");

    //owing data from mysql to Jtable
    try {
        String sql = "select * from kopitable";
        java.sql.Connection conn = (Connection) config.configDB();
        java.sql.Statement stm = conn.createStatement();
        java.sql.ResultSet res = stm.executeQuery(sql);
        while (res.next()) {
            model.addRow(new Object[]{
                res.getString(1), res.getString(2), res.getString(3), res.getInt(4), res.getString(5), res.getDate(6)
            });
        }
        tabelnya.setModel(model);
    } catch (Exception e) {

    }
}

And this is my code when i insert data to phpmyadmin.

        pesanandata.setText("\nAmericano : "+americano+"\nCapuccino : "+cappuccino+"\nmocha : "+mocha+"\nTopping : "+topping);
    try{
        String sql = " INSERT INTO kopitable VALUE ('"+namapembeli+"','"+pegawai+"','"+pesanandata.getText()+"','"+totalharga+"','"+pembayarannya.getText()+"','"+java.time.LocalDate.now()+"')";
        java.sql.Connection conn=(Connection) config.configDB();
        java.sql.PreparedStatement pst=conn.prepareStatement(sql);
        pst.execute();

    }catch (Exception e){
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nevada
  • 71
  • 7
  • 2
    Format the text using ``, for [example](https://stackoverflow.com/questions/31912426/vertical-and-horizontal-allignment-in-jtextpane/31912962#31912962) and [example](https://stackoverflow.com/questions/31042605/java-swing-adding-multiple-lines-in-jtables-cell/31043203#31043203) – MadProgrammer Jul 15 '21 at 01:54
  • We are not interested in your application. We are only interested in the stated problem. Where you get the data from is irrelevant to your question. You question is about formatting the data in the table. Get rid of all references to the database. You can easily add hard coded data to the TableModel with `\n` characters to demonstrate the problem. – camickr Jul 15 '21 at 01:54
  • @camickr No worries, was searching about for some examples ;) – MadProgrammer Jul 15 '21 at 02:06
  • Check out: https://stackoverflow.com/a/19289635/131872 for some old code that provides a custom renderer and resizes the row height whenever the column width changes. – camickr Jul 15 '21 at 02:17

0 Answers0