How to add table title and scroll bar in jtable. Java

I am trying to add a table title and Scrollbar to JTABLE , but I am not getting what I want. I tried with 3 different procedures, but did not come up with the correct one. This code is for scrollbars .

Can someone tell me how to add a title. I cannot add it in the same way as model.addRow() , because after that I need to get the values ​​from the table, and this method creates problems with this. and How to add a scroll code.

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); 
+7
java swing scrollbar jtable
source share
4 answers

To add titles, 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 a do scroll bar

 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); } } 

Exit

enter image description here

+11
source share

I'm afraid that you might mess yourself up with the null and setBounds(...) layouts of something that doesn't work with most of the Swing GUIs and, in particular, JTables displayed in JScrollPanes.

Suggestions:

  • Do not use null layouts, do not call setBounds(...) especially on what you want to display inside the scroll.
  • Add JTable to JScrollPane using the JScrollPane constructor.
  • Add the created JScrollPane to the JPanel, which uses the BorderLayout at 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(); } }); } } 

Do not use setBounds(...) on your JScrollPane. Although null layouts and setBounds() may seem like new to Swing as the easiest and best way to create sophisticated GUIs, the more Swing GUIs you create the more serious difficulties you will encounter when using them. They will not resize your components when resizing the graphical interface, they are a royal witch for improvement or support, they are not fully executed when placed in scrollpanes, they look terribly awful when viewed on all platforms other than the original.

+6
source share

Headings:

 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 not considered a good program design, especially when setting up a top-level container with .setBounds( ) , since it does not guarantee consistency on another platform regarding the position of the component.

+4
source share

The only reason is that I did not add the setBounds(10, 0, 457, 103) JScrollpane. to JScrollpane. Thank you all for your answers. Credits for @singakash

Here is the full 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); 
+2
source share

All Articles