954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Insert a JFrame and a JTable in a JSplitPane

Dear Friends,

I have a frame which is a JSplitPane which is split horizontally into two. On the upper half I want a JFrame to appear and on the lower half I want a JTable to appear. I tired but couldn't get the answer correctly. Will anyone be able to help me out.

Regards

Sincerelibran

sincerelibran
Light Poster
41 posts since Mar 2009
Reputation Points: 8
Solved Threads: 15
 

The easiest way will be use the NetBeans IDE and do the same in the Design Area.

vealparry
Light Poster
27 posts since Jan 2011
Reputation Points: 10
Solved Threads: 5
 

You can't have a Frame within a Frame. You can have a JInternalFrame within a JFrame, but not another JFrame. Why do want a JFrame inside of a JFrame? If it's because you want to reuse a class that currently extends JFrame, then, well, now you see one of the problems with extending top-level window classes. Rewrite it to extend JPanel (and it's main method can still create a JFrame to put itself inside of so it can still be used stand-alone, which is how it should be done).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Dear Friends,

I'm using the following code. The table that appears doesn't cover the entire area of the second half of the screen. It comes on one side (left side). I want it to cover the entire area. Kindly help me out in this. I'm posting both the programs that I use.
LibraryMainMenu.java

// Main Menu
import java.awt.*;
import java.sql.*;
import javax.swing.*;

public class LibraryMainMenu{

	static JPanel createPanel(String[] sa) {

		JPanel p = new JPanel(new GridLayout(0,1));

		for(int i=0;i<sa.length;++i) p.add(new JLabel(sa[i]));
      		return p;
   	}

	public static void main(String[] args) {

		
		JPanel p1 = createPanel(new String[]{
         		"James Dean","Johnny Depp","Scarlett Johansson",
         		"Keira Knightley","Robert Carlyle","Ewan McGregor",
         		"Sean Connery","Leonardo di Caprio"}
      		);
		p1.setMinimumSize(new Dimension(1151,380));

      		JPanel p2 = new JPanel();
		TableTest tt = new TableTest();
		p2.setLayout(new BorderLayout());
		p2.add(tt, BorderLayout.LINE_START);
		p2.setBackground(new Color(0,120,120));
		
      		final JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1, p2);
      		jSplitPane.setContinuousLayout(true);

      		JFrame f = new JFrame("SABS - Library Management System");
      		f.getContentPane().add(jSplitPane);
      		f.setSize(1151, 860);
      		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      		f.setVisible(true);
   	}

}


TableTest.java

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

public class TableTest extends JPanel{

	
	DefaultTableModel dtm;
	JTableHeader header;
	JScrollPane pane;
	JTable t;
	JPanel panel;

	public TableTest(){


			panel = new JPanel();
			panel.setLayout(new BorderLayout());
			dtm = new DefaultTableModel(6,9);
			t = new JTable(dtm);
			t.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

      			dtm.addColumn("Marks");
			dtm.addColumn("Gender");

      			//add column at third position
	      		positionColumn(t,0);
      			positionColumn(t,1);

			header = t.getTableHeader();
			//t.setLocation(5,5);
      			header.setBackground(new Color(0,75,200));
      			header.setForeground(new Color(255,255,255));
			pane = new JScrollPane(t);
			panel.add(pane, BorderLayout.CENTER);
			add(panel);
		setSize(1150,430);
	}
  	public void positionColumn(JTable table,int col_Index) {

        	table.moveColumn(table.getColumnCount()-1, col_Index);
  	}
}
sincerelibran
Light Poster
41 posts since Mar 2009
Reputation Points: 8
Solved Threads: 15
 

just add setPreferredSize to your scrollPane

pane.setPreferredSize(new Dimension(1100,420));
vealparry
Light Poster
27 posts since Jan 2011
Reputation Points: 10
Solved Threads: 5
 

hello guys i'm new to java programming . i want to know if i can create a table not necessary SQL tabel a simple table .
in a JSplitPane (like innerSplitPane) if any one can help .

jouj
Newbie Poster
7 posts since Mar 2012
Reputation Points: 13
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: