简体   繁体   中英

How to make jtable on full jframe window

i am trying to create a jtable using java swing and little bit i am able to do this but problem is this that i want to create that jtable on full jframe window how can i do this here is my code given below

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

public class ScrollableJTable{
  public static void main(String[] args) {
  new ScrollableJTable();
  }
  public ScrollableJTable(){
  JFrame frame = new JFrame("Creating a Scrollable JTable!");
  JPanel panel = new JPanel();
  String data[][] = {{"001"},
};
  String col[] = {"Roll"};
  JTable table = new JTable(data,col);
  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
    Toolkit tk = Toolkit.getDefaultToolkit(); 
  int xSize = ((int) tk.getScreenSize().getWidth());  
  int ySize = ((int) tk.getScreenSize().getHeight()); 
  JScrollPane pane = new JScrollPane(table);
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  panel.add(pane);
  frame.add(panel);
 frame.setSize(xSize,ySize);  

  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
}

How can i achieve my desired output?

hanks in advance

Simple, set the layout manager for panel as BorderLayout or simple add the JScrollPane ( pane ) directly to the frame, which uses a BorderLayout by default

Take a look at Laying Out Components Within a Container for more details

cool..you do not need to write so many lines of code to display a file.Just download the jar file rs2xml and add it to your project library.

Note : This will be useful if you are using netbeans

How to use it?

  • create the table in backend
  • download rs2xml jar file
  • Add it to the project library
  • drag the jtable from the swing controls to your jframe

Now write the following code as follows:

write ur query:

        ps=con.prepareStatement("select * from register");       
        ResultSet r=ps.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(r)); //display the register table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM