1

Neither JTable is showing column names as title nor I have scrollBar when I add data manually.

code for JTable and JScrollPane:

 private JPanel contentPane = new JPanel();
    Object[] title = {"ISBN", "Name", "Author", "Shelf No", "Row No", "Col No"};
    DefaultTableModel dtm = new DefaultTableModel();
    dtm.setColumnIdentifiers(title);
    table = new JTable(dtm);
    table.setBounds(23, 55, 435, 217);
    table.setModel(dtm);
    JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    table.setForeground(Color.gray);
    table.setRowHeight(30);
    contentPane.add(scroll);
    contentPane.add(table);
    Object[] row = {"hi", "2", "3", "5", "r", "we" };

ActionListener to add data in table:

    btnSearch.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    dtm.addRow(row);

                }
            });

As you can see in image below, I don't have those columns(title) and scrollbar.

picture

nix
  • 165
  • 2
  • 4
  • 19

3 Answers3

1

You are not adding your JTable to the scrollpane but to your contentpane. Try this instead:

contentPane.add(scroll);
scroll.add(table); //instead of contentPane.add(table)
f1sh
  • 11,489
  • 3
  • 25
  • 51
1

The JTable column headers are displayed in the JScrollPane, so this is correct:

contentPane.add(scroll);

Don't immediately then replace the scroll pane with the table:

//contentPane.add(table);

Instead of setBounds(), override getPreferredScrollableViewportSize(), as suggested here. Because the default layout of JPanel is FlowLayout, the table will remain a fixed size. Consider GridLayout, which will allow the display to reflect changes as the enclosing frame is resized.

JPanel contentPane = new JPanel(new GridLayout());
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • it did not work. here is my complete code for this class: http://pastebin.com/xcC42H7k – nix Apr 06 '16 at 13:32
1

Do following changes into your code,

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.my.classes.Validation;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class SearchBooks extends JFrame {

    private JPanel contentPane;
    private JTextField txtIsbn;
    private Validation v = new Validation();
    private JTable table;
    DefaultTableModel dtm ;
    Object[]  row = {"hi", "2", "3", "5", "r", "we" };


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SearchBooks frame = new SearchBooks();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SearchBooks() {
        setTitle("Search Books from Database");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //setBounds(100, 100, 502, 341); instead of it use pack() see below it's uses at right place not here...
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //contentPane.setLayout(null);

        JLabel lblBookName = new JLabel("Book Name: ");
        lblBookName.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblBookName.setBounds(23, 11, 80, 14);
        contentPane.add(lblBookName);

        txtIsbn = new JTextField();
        txtIsbn.setBounds(113, 8, 202, 20);
        contentPane.add(txtIsbn);
        txtIsbn.setColumns(10);

        JButton btnSearch = new JButton("Search");
        btnSearch.setFont(new Font("Tahoma", Font.BOLD, 11));
        btnSearch.setBounds(338, 7, 103, 23);
        contentPane.add(btnSearch);

        Object[] title = {"ISBN", "Name", "Author", "Shelf No", "Row No", "Col No"};
        dtm = new DefaultTableModel(); 
        dtm.setColumnIdentifiers(title);
        table = new JTable(dtm);
        table.setBounds(23, 55, 435, 217);
        table.setModel(dtm);
        //JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        JScrollPane scroll = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setForeground(Color.RED);
        table.setRowHeight(30);
        contentPane.add(scroll);

        // contentPane.add(table); //comment it...

        //dtm.addRow(title); doesn't required because already header is there...
        pack();

        txtIsbn.addKeyListener(
                new KeyAdapter() {
                    public void keyPressed(KeyEvent ev) {
                        if(ev.getKeyCode() == KeyEvent.VK_ENTER) {

                            if(v.validateIsbn(txtIsbn.getText())  || v.validateAddress(txtIsbn.getText())) {

                            }else {
                                JOptionPane.showMessageDialog(null, "Error! Please check your input");
                            }
                        }
                    }
                });

        btnSearch.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        table.setForeground(Color.BLACK);
                        dtm.addRow(row);

                    }
                });
    }
}

Otu-Put :

enter image description here

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • thank you. its solved. can you please explain why we commented out `contentPane.setLayout()`. – nix Apr 06 '16 at 16:10