Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Table header and Scrollbar in jtable. java

I am trying to add a Table header and Scrollbar in JTABLE, but I'm not getting what I want. I have tried with 3 different procedures, but did not come up with the right one. This code is for Scrollbar.

Can any one tell me how to add header. I can't add it just as model.addRow() because after that I have to get the values from the table and this method create problems with that. and How to add Scroll bar code too.

This is my Code

DefaultTableModel model = new DefaultTableModel();      
JTable table = new JTable(model);
table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
//JScrollPane scrollPane = new JScrollPane(table);   //Try one
//new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); // Try 2


model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("MODEL");
model.addColumn("P_Price");
model.addColumn("S_Price");
model.addColumn("Quantity");

table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);    
table.setBounds(10, 0, 457, 103);

//panel_1.add(new JScrollPane(table)); Try 3
//panel_1.add(scrollPane); //link to Try one
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // link 2 Try 2

panel_1.add(table);
like image 711
Adnan Ali Avatar asked Jan 24 '15 17:01

Adnan Ali


4 Answers

The only reason for the stuff was that I wasn't adding setBounds(10, 0, 457, 103) method in JScrollpane. Thanks everyone for your answers. Credits to @singakash

Here is the complete working code.

    DefaultTableModel model = new DefaultTableModel();      
    JTable table = new JTable(model);
    table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
    model.addColumn("ID");
    model.addColumn("NAME");
    model.addColumn("MODEL");
    model.addColumn("P_Price");
    model.addColumn("S_Price");
    model.addColumn("Quantity");
    table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);


    table.setBounds(10, 0, 457, 103);   

    JScrollPane jp=new JScrollPane(table);
    jp.setBounds(10, 0, 457, 103);
    jp.setVisible(true);
    add(jp);
    panel_1.add(jp);
like image 163
Adnan Ali Avatar answered Oct 25 '22 06:10

Adnan Ali


To add headers do

String [] header={"name","age"};
String [][] data={{"akash","20"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"}};
DefaultTableModel model = new DefaultTableModel(data,header);  

To add scrollbar do

JScrollPane js=new JScrollPane(table);
js.setVisible(true);
add(js);

Full Code

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Table extends JPanel{

    JTable jt;
    public Table(){

        String [] header={"name","age"};
        String [][] data={{"akash","20"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"},{"pankaj","24"}};


        DefaultTableModel model = new DefaultTableModel(data,header);

        JTable table = new JTable(model);

        table.setPreferredScrollableViewportSize(new Dimension(450,63));
        table.setFillsViewportHeight(true);

        JScrollPane js=new JScrollPane(table);
        js.setVisible(true);
        add(js);

    }

    public static void main(String [] a) {

        JFrame jf=new JFrame();
        Table tab= new Table();
        jf.setTitle("Table");
        jf.setSize(500, 500);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(tab);




    }

}

Output

enter image description here

like image 40
singhakash Avatar answered Oct 25 '22 07:10

singhakash


I fear that you may be messing yourself up by use of null layouts and setBounds(...) something that does not work well with most Swing GUI's and in particular JTables displayed in JScrollPanes.

Suggestions:

  • Don't use null layouts, don't call setBounds(...) especially on something that you want to display inside of scrollpanes.
  • Add the JTable to the JScrollPane via the JScrollPane's constructor.
  • Add the JScrollPane thus created to a JPanel that uses BorderLayout in the BorderLayout.CENTER position.

For example:

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.*;
import javax.swing.table.*;

public class Foo {
   private JPanel panel_1 = new JPanel();

   public Foo() {
      DefaultTableModel model = new DefaultTableModel();
      JTable table = new JTable(model);
      table.setFont(new Font("Times New Roman", Font.PLAIN, 13));
      JScrollPane scrollPane = new JScrollPane(table); 

      model.addColumn("ID");
      model.addColumn("NAME");
      model.addColumn("MODEL");
      model.addColumn("P_Price");
      model.addColumn("S_Price");
      model.addColumn("Quantity");

      panel_1.setLayout(new BorderLayout());
      panel_1.add(scrollPane, BorderLayout.CENTER);
   }

   public JPanel getPanel1() {
      return panel_1;
   }

   private static void createAndShowGui() {
      Foo mainPanel = new Foo();

      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel.getPanel1());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Don't even use setBounds(...) on your JScrollPane. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms different from the original one.

like image 41
Hovercraft Full Of Eels Avatar answered Oct 25 '22 08:10

Hovercraft Full Of Eels


Headers :

JTable table = new JTable(model);
String cols [] = {"ID","NAME","MODEL","P_Price","S_Price","Quantity"};
model.setColumnIdentifiers(cols );

Scrollpane :

JScrollPane jsp = new JScrollPane(table);
panel1.add(jsp);

//....
add(jsp);// add scroll to frame not the panel

Note :

But remember that null layout is not considered as a good program design, especially when setting up non top level container with .setBounds( ) since it won't guarantee a consistency on other platform regarding to the position of the component.

like image 33
Fevly Pallar Avatar answered Oct 25 '22 07:10

Fevly Pallar