Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable not visible when adding with JScrollPane

i am working on a gui based project and i need to add a jtable in a jpanel.But i am not getting why the table is not being displayed when adding with a scrollpane.Also when adding without scrollpane,the table header is not displayed. Thanks for any help... Following is the code i am using..

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class FrmAddEditDN extends JDialog{
    JButton JBUpdate = new JButton(new ImageIcon("images/save.png"));
    JButton JBReset = new JButton("Reset",new ImageIcon("images/reset.png"));
    JButton JBCancel = new JButton("Cancel",new ImageIcon("images/cancel.png"));

    JLabel JLPic1 = new JLabel();
    JLabel JLBanner = new JLabel("Please fill-up all the required fields.");

public FrmAddEditDN(boolean ADD_STATE,JFrame OwnerForm,Connection srcCN,String srcSQL){
        super(OwnerForm,true);
        cnAEDN = srcCN;
        ADDING_STATE = ADD_STATE;
        JPanel JPContainer = new JPanel();
        JPContainer.setLayout(null);
                String[] columnNames = {
                    "Sr No","Invoice No","Invoice Date","Consignee","Description","Basic Amount","Invoice Amount","Payment Received",
                "EFT Date","Payment Earlier Received","Comm. @ %","Comm.Claim @ %","Comm. Received","Date","Bank","Remarks"};
                String[][] data = {
                    {"","","","","","","","","","","","","","","",""}
   };
                DefaultTableModel DTModel = new DefaultTableModel(data,columnNames);
                JTable table = new JTable(5,16);
                table.setBounds(15,295,screen.width-40,150);
                table.setRowHeight(30);
                table.setForeground(Color.black);
                table.setBackground(Color.white);
                JTableHeader header = table.getTableHeader();
                header.setForeground(Color.red);
                header.setBackground(Color.green);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                table.getColumnModel().getColumn(0).setPreferredWidth(2);
                table.getColumnModel().getColumn(1).setPreferredWidth(50);
                JScrollPane tableContainer = new JScrollPane();
                tableContainer.setViewportView(table);

                JPContainer.add(tableContainer);

                getContentPane().add(JPContainer);
        setSize(screen.width-5,screen.height-45);
        setResizable(false);
        setLocation(0,0);
    }
}
like image 433
user1718945 Avatar asked Oct 04 '12 05:10

user1718945


People also ask

How do I find my JTable model?

All you will need to have the model will be: DefaultTableModel model = (DefaultTableModel) table. getModel();

How do I add JScrollPane?

Create the panel and scrollpane like: JPanel panel = new JPanel(); JScrollPane scrollPane = new JScrollPane( panel );

How do I make JScrollPane transparent?

You need to use setOpaque(false) to make it transparent. Call that both on the JScrollPane, and on it's ViewPort. sp. setOpaque(false); sp.


1 Answers

You need to pass Component while creating a JScrollPane.

JScrollPane scrollPane = new JScrollPane(table);
JPContainer.add(scrollPane);
like image 109
KV Prajapati Avatar answered Nov 14 '22 21:11

KV Prajapati