I'm creating a GUI interface to interact with the database of a warehouse. The application needs to add items to the database, update them and show them. I have some tables in the database and for each table, I want to create a JPanel, and put them in a cardlayout, so I can navigate between them with JMenu items. Each JPanel has the same form. In the top, there is a box with textfields, comboboxes etc. to add an item in the table. Under the box, I have a JTable with 1 row and under that, I have a JTable in a JScrollPane to show the content of the table. Each column needs to have a width of 150, except the last one (width=100, it will contain a JButton for modifications). I use the first JTable as a filter (for example if the first column contains '1', then the second JTable will only show items with an ID starting by '1'). I don't know how to choose correct layouts for different JPanels. For the moment, each JPanel has a BorderLayout and each component is placed in the center. But the problem is that I can't choose the width of each column.
Well, finally I've solved my own problem. Actually, I need to put the JTable in a JPanel, and the JPanel in a JScrollPane. Here is the code :
public class Brouillon extends JFrame {
public Brouillon() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpContent = new JPanel();
JPanel jpCards = new JPanel();
jpContent.setLayout(new BoxLayout(jpContent, BoxLayout.PAGE_AXIS));
CardLayout clSelect = new CardLayout();
jpCards.setLayout(clSelect);
JButton jbTest = new JButton();
jbTest.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
clSelect.next(jpCards);
}
});
jpContent.add(jbTest);
JPanel jpContainer1 = new JPanel();
JTable jtData1 = new JTable(new Object[2][3], new String[] {"1", "2", "3"});
jtData1.getColumnModel().getColumn(0).setPreferredWidth(150);
jtData1.getColumnModel().getColumn(1).setPreferredWidth(150);
jtData1.getColumnModel().getColumn(2).setPreferredWidth(50);
JPanel jpTemp1 = new JPanel();
jpTemp1.setLayout(new BorderLayout());
jpTemp1.add(jtData1.getTableHeader(), BorderLayout.NORTH);
jpTemp1.add(jtData1, BorderLayout.CENTER);
JScrollPane jspData1 = new JScrollPane(jpTemp1);
jpContainer1.add(jspData1);
JPanel jpContainer2 = new JPanel();
JTable jtData2 = new JTable(new Object[2][7], new String[] {"1", "2", "3", "4", "5", "6", "7"});
jtData2.getColumnModel().getColumn(0).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(1).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(2).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(3).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(4).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(5).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(6).setPreferredWidth(50);
JPanel jpTemp2 = new JPanel();
jpTemp2.setLayout(new BorderLayout());
jpTemp2.add(jtData2, BorderLayout.CENTER);
jpTemp2.add(jtData2.getTableHeader(), BorderLayout.NORTH);
JScrollPane jspData2 = new JScrollPane(jpTemp2);
jpContainer2.add(jspData2);
jpCards.add(jpContainer2);
jpCards.add(jpContainer1);
jpContent.add(jpCards);
this.getContentPane().add(jpContent);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new Brouillon();
}
}
For jTable, you can set the column size.
Possibly the below example may help. Please let me know if this is what you are looking for!
TableColumn column = null;
for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 2) {
column.setPreferredWidth(100); //sport column is bigger
} else {
column.setPreferredWidth(50);
}
}
Related
I have created a MigLayout, which looks like that:
As you can see the table does not resize correctly.
I am creating my layout like that:
public JScrollPane createLayout() {
JPanel panel = new JPanel(new MigLayout("debug 400"));
JScrollPane sp;
JLabel lab = new JLabel(labelValue);
lab.setFont(new Font("Tahoma", Font.BOLD, 15));
panel.add(lab, "wrap");
panel.add(resultsTable(), "growx, wrap");
panel.add(resultsButtons(), "wrap");
//set table properties
tableProperties(resultTable);
updateResultsTable();
sp = new JScrollPane(panel);
sp.repaint();
sp.validate();
return sp;
}
My table is created like that:
private JPanel resultsTable() {
JPanel panel = new JPanel(new MigLayout(""));
JScrollPane scrollTablePane;
rtm = new ResultTableModel(resultList);
resultTable = new JTable(rtm);
scrollTablePane = new JScrollPane(resultTable);
sorter = new TableRowSorter<TableModel>(resultTable.getModel());
resultTable.setRowSorter(sorter);
scrollTablePane.repaint();
scrollTablePane.validate();
//add to panel
panel.add(scrollTablePane);
panel.repaint();
panel.validate();
return panel;
}
Furthermore, I set the table properties in the following method:
public void tableProperties(JTable table) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}
Any recommendations why my columns of my table do not stretch?
I appreciate your answer!
If you want your table to be auto resizable try to use inside createLayout method the following constructor JPanel panel = new JPanel(new MigLayout("debug 400,wrap 1","[grow,fill]","[grow,fill]")) and inside resultsTable method the following constructor JPanel panel = new JPanel(new MigLayout("","[grow,fill]","[grow,fill]"))
I just started to learn swing by myself, I'm little bit confused why my event does not work here:
1.I'm trying to delete everything from my panel if the user click menu bar -> load but it force me to change the panel to final because i'm using it inside the event!
2.I have defined new panel in my event and defined two more container to add to that panel and then add it to the main frame but it seems nothing happening!
Please help me if you can find out what is wrong.
Sorry in advance for messy code.
I appreciate any hints.
public class SimpleBorder {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
myFrame frame = new myFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MyFrame extends JFrame {
public MyFrame()
{
setSize(500,500);
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel label = new JLabel("my name is bernard...");
Color myColor = new Color(10, 150, 80);
panel.setBackground(myColor);
label.setFont(new Font("Serif", Font.PLAIN, 25));
Dimension size = label.getPreferredSize();
Insets insets = label.getInsets();
label.setBounds(85+insets.left, 120+insets.top , size.width, size.height);
panel.add(label);
JMenuBar menu = new JMenuBar();
setJMenuBar(menu);
JMenu col = new JMenu("Collection");
menu.add(col);
JMenu help = new JMenu("Help");
menu.add(help);
Action loadAction = new AbstractAction("Load")//menu item exit goes here
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent event)
{
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol1 = new JScrollPane(text);
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel2 = new JPanel(new BorderLayout());
panel2 = new JPanel(new GridLayout(1, 2 ));
panel2.add(scrol1,BorderLayout.WEST);
panel2.add(scrol2,BorderLayout.EAST);
add(panel2);
}
};
JMenuItem load = new JMenuItem(loadAction);
col.add(load);
add(panel);
}
}
Call revalidate()/repaint() on your JFrame instance after adding the new panel:
JPanel panel2 = new JPanel(new BorderLayout());
// panel2 = new JPanel(new GridLayout(1, 2 ));//why this it will overwrite the above layout
panel2.add(scrol1,BorderLayout.WEST);
panel2.add(scrol2,BorderLayout.EAST);
add(panel2);
revalidate();
repaint();
Also call pack() on you JFrame instance so all components are spaced by the layoutmanager. As said in a comment dont extend the JFrame class, create a variable of the frame and initiate all that you need on the frames instance, and dont set a layout to null, unless you love hard work :P
Alternatively as mentioned by mKorbel, a CardLayout may be more what you want, it will allow you to use a single JPanel and switch between others/new ones:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
...
JPanel card2 = new JPanel();
...
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
//add card panel to frame
frame.add(cards);
//swap cards
CardLayout cl = (CardLayout)(cards.getLayout());//get layout of cards from card panel
cl.show(cards, TEXTPANEL);//show another card
This is my first time working with GUI, so I'm not exactly sure what is causing the problem. I have an assignment from my Uni. The project is to make a sort of "Product management" program for various purposes. The whole thing is done except the GUI, and this is where I just don't get it why this JTable won't display column titles. Here's the code (btw, using MIGLayout)
package sepm.s2012.e0727422.gui;
import sepm.s2012.e0727422.service.*;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class MainFrame {
/** Start all services **/
// IProductService ps = new ProductService();
// IInvoiceService is = new InvoiceService();
// IOrderService os = new OrderService();
/** Frame **/
private JFrame frame;
private JTabbedPane tab;
public static void main(String[] args) {
new MainFrame();
}
public MainFrame() {
frame = new JFrame("Product control and management system");
frame.setVisible(true);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/** TABBED PANE options and parameters **/
tab = new JTabbedPane();
ImageIcon icon = null; // TODO
tab.addTab("Products", icon, tabProducts(), "Add/Update/Delete products");
tab.addTab("Invoices", icon, tabInvoices(), "Overview of invoices");
tab.addTab("Cart", icon, tabCart(), "Order new products");
tab.setSelectedIndex(0);
frame.add(tab, BorderLayout.CENTER);
}
/**
* Products Panel tab
* #return panel
*/
public JPanel tabProducts() {
JPanel panel = new JPanel(new MigLayout("","20 [] 20", "10 [] 10 [] 10 [] 10"));
JLabel label = new JLabel("List of all available products");
JButton add = new JButton("Add product");
JButton update = new JButton("Update product");
// Below is a test table, will be replace by products in DB
String[] tableTitle = new String[] {"ID", "Name", "Type", "Price", "In stock"};
String[][] tableData = new String[][] {{"1", "Item 1", "Type 1", "0.00", "0"}, {"2", "Item 2", "Type 2", "0.00", "0"},
{"3", "Item 3", "Type 3", "0.00", "0"}, {"4", "Item 4", "Type 4", "0.00", "0"}};
JTable table = new JTable(tableData, tableTitle);
panel.add(label, "wrap, span");
panel.add(table, "wrap, span");
panel.add(add);
panel.add(update);
return panel;
}
public JPanel tabInvoices() {
JPanel panel = new JPanel(new MigLayout());
return panel;
}
public JPanel tabCart() {
JPanel panel = new JPanel(new MigLayout());
return panel;
}
}
Add the table to a JScrollPane
The docs for JTable state:
.. Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane) and want the header displayed, you can get it using getTableHeader() and display it separately.
At first add your table to a JScrollPane:
JScrollPane scrollpane = new JScrollPane(table);
then add the scrollpane to your layout :
panel.add(scrollpane, "wrap, span");
Secondly, add all your components to the frame and make it visible at the end of your method:
//...
frame.add(tab, BorderLayout.CENTER);
frame.validate();
frame.setVisible(true);
For miglayout I use this:
JPanel panel = new JPanel(new MigLayout("wrap 1, gapy 0"));
table = CreateParametersTable();
panel.add(table.getTableHeader(), "width 100%");
panel.add(table, "width 100%");
For removing vertical gaps read this How to remove vertical gap between two cells in MigLayout?
I have a JTable that is placed inside a JPanel which is then placed in a JFrame that has another JPanel that has a ScrollPane. The general idea can be seen below if the explanation is confusing. I have set my JTable to auto resize the last column, but it never auto sizes. What is the problem?
JFrame -> JPanel -> JTable
-> JPanel -> Scroll Pane
My code:
this refers to my class which extends JFrame
this.setLayout(new BorderLayout());
JTextArea log = new JTextArea(20, 50);
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
String[] configRow = {"Config File", "Not Loaded"};
String[] logRow = {"Log File" , "Not Loaded"};
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("");
dtm.addColumn("");
dtm.addRow(configRow);
dtm.addRow(logRow);
JTable status = new JTable(dtm);
status.setTableHeader(null);
status.setShowGrid(false);
status.setEnabled(false);
status.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(status, BorderLayout.LINE_START);
JPanel logPanel = new JPanel();
logPanel.add(logScrollPane);
this.add(tablePanel, BorderLayout.NORTH);
this.add(logPanel, BorderLayout.SOUTH);
Your JTable must be placed inside a JScrollPane in order for the resizing to work properly. You are placing the JTable directly on a JPanel.
tablePanel.add(new JScrollPane(status), BorderLayout.LINE_START):
I want to add JTable into JPanel whose layout is null. JPanel contains other components. I have to add JTable at proper position.
Nested/Combination Layout Example
The Java Tutorial has comprehensive information on using layout managers. See the Laying Out Components Within a Container lesson for further details.
One aspect of layouts that is not covered well by the tutorial is that of nested layouts, putting one layout inside another to get complex effects.
The following code puts a variety of components into a frame to demonstrate how to use nested layouts. All the layouts that are explicitly set are shown as a titled-border for the panel on which they are used.
Notable aspects of the code are:
There is a combo-box to change PLAF (Pluggable Look and Feel) at run-time.
The GUI is expandable to the user's need.
The image in the bottom of the split-pane is centered in the scroll-pane.
The label instances on the left are dynamically added using the button.
Nimbus PLAF
NestedLayoutExample.java
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.TitledBorder;
/** A short example of a nested layout that can change PLAF at runtime.
The TitledBorder of each JPanel shows the layouts explicitly set.
#author Andrew Thompson
#version 2011-04-12 */
class NestedLayoutExample {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
//JToolBar tb = new JToolBar();
JPanel plafComponents = new JPanel(
new FlowLayout(FlowLayout.RIGHT, 3,3));
plafComponents.setBorder(
new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)") );
final UIManager.LookAndFeelInfo[] plafInfos =
UIManager.getInstalledLookAndFeels();
String[] plafNames = new String[plafInfos.length];
for (int ii=0; ii<plafInfos.length; ii++) {
plafNames[ii] = plafInfos[ii].getName();
}
final JComboBox plafChooser = new JComboBox(plafNames);
plafComponents.add(plafChooser);
final JCheckBox pack = new JCheckBox("Pack on PLAF change", true);
plafComponents.add(pack);
plafChooser.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int index = plafChooser.getSelectedIndex();
try {
UIManager.setLookAndFeel(
plafInfos[index].getClassName() );
SwingUtilities.updateComponentTreeUI(frame);
if (pack.isSelected()) {
frame.pack();
frame.setMinimumSize(frame.getSize());
}
} catch(Exception e) {
e.printStackTrace();
}
}
} );
gui.add(plafComponents, BorderLayout.NORTH);
JPanel dynamicLabels = new JPanel(new BorderLayout(4,4));
dynamicLabels.setBorder(
new TitledBorder("BorderLayout(4,4)") );
gui.add(dynamicLabels, BorderLayout.WEST);
final JPanel labels = new JPanel(new GridLayout(0,2,3,3));
labels.setBorder(
new TitledBorder("GridLayout(0,2,3,3)") );
JButton addNew = new JButton("Add Another Label");
dynamicLabels.add( addNew, BorderLayout.NORTH );
addNew.addActionListener( new ActionListener(){
private int labelCount = 0;
public void actionPerformed(ActionEvent ae) {
labels.add( new JLabel("Label " + ++labelCount) );
frame.validate();
}
} );
dynamicLabels.add( new JScrollPane(labels), BorderLayout.CENTER );
String[] header = {"Name", "Value"};
String[] a = new String[0];
String[] names = System.getProperties().
stringPropertyNames().toArray(a);
String[][] data = new String[names.length][2];
for (int ii=0; ii<names.length; ii++) {
data[ii][0] = names[ii];
data[ii][1] = System.getProperty(names[ii]);
}
DefaultTableModel model = new DefaultTableModel(data, header);
JTable table = new JTable(model);
try {
// 1.6+
table.setAutoCreateRowSorter(true);
} catch(Exception continuewithNoSort) {
}
JScrollPane tableScroll = new JScrollPane(table);
Dimension tablePreferred = tableScroll.getPreferredSize();
tableScroll.setPreferredSize(
new Dimension(tablePreferred.width, tablePreferred.height/3) );
JPanel imagePanel = new JPanel(new GridBagLayout());
imagePanel.setBorder(
new TitledBorder("GridBagLayout()") );
BufferedImage bi = new BufferedImage(
200,200,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
GradientPaint gp = new GradientPaint(
20f,20f,Color.red, 180f,180f,Color.yellow);
g.setPaint(gp);
g.fillRect(0,0,200,200);
ImageIcon ii = new ImageIcon(bi);
JLabel imageLabel = new JLabel(ii);
imagePanel.add( imageLabel, null );
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
tableScroll,
new JScrollPane(imagePanel));
gui.add( splitPane, BorderLayout.CENTER );
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Other Screen Shots
Windows PLAF
Mac OS X Aqua PLAF
Ubuntu GTK+ PLAF
Don't use a null layout. Learn to use LayoutManagers:
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
LayoutManagers allow you to properly handle things window resizing or dynamic component counts. They might seem intimidating at first, but they are worth the effort to learn.
As I can remember, the null layout means an absolute position so it will be pretty hard you to count the X point for your JTable left upper corner location. But if you just want to have all panel components one by one you can use FlowLayout() manager as
JPanel panel=new JPanel(new FlowLayout());
panel.add(new aComponent());
panel.add(new bComponent());
panel.add(new JTable());
or if you need to fill the panel you should use GridLayout() as...
int x=2,y=2;
JPanel panel=new JPanel(new GridLayout(y,x));
panel.add(new aComponent());
panel.add(new bComponent());
panel.add(new JTable());
Good luck
If you are using null layout manager you always need to set the bounds of a component.
That is the problem in your case.
You should do what everyone suggest here and go and use some layout manager believe they save time.
Go and check out the tutorial in #jzd's post.
Enjoy, Boro.
JTable should be added into the JScrollPane which actually should be added into the JPanel.
The JPanel should have some layout manager.
If you don't care about the precision of components size you can use pure BorderLayout and combine it with FlowLayout and GridLayout. if you need precision - use jgoodies FormLayout.
The FormLayout is really tricky one, but you can play a little with WindowBuilder (which is embedded into Eclipse) and a look at the code it generates. It may look complicated but it is just an ignorance.
Good luck.
First, you should seriously consider other Layout managers, for example the BorderLayoutManager (new JPanel(new BorderLayout())) is a good start.
Also when designing your dialog, remember that you can and should nest your layouts: one JPanel inside another JPanel (e.g. a GridLayout inside a BorderLayout). Please note: a 'good' dialog should resize properly, so that if the user resizes your Frame, you want to automatically extend your information objects such as your table, and not show large areas of JPanel background. That's something you cannot achieve with a NullLayout.
But there are probably cases - somewhere in this big world - where a NullLayout is just the thing. So here's an example:
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 JTableInNullLayout
{
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new DefaultTableModel(
new String[][] { { "a", "123"} , {"b", "456"} },
new String[] { "name", "value" } );
JTable t = new JTable(model);
JPanel panel = new JPanel(null);
JScrollPane scroll = new JScrollPane(t);
scroll.setBounds( 0, 20, 150, 100 ); // x, y, width, height
panel.add(scroll);
JFrame frame = new JFrame();
frame.add(panel);
frame.setPreferredSize( new Dimension(200,200));
frame.pack();
frame.setVisible(true);
}
}
When a component have a "null" layout, you have to manage the layout by yourself, that means you have to calculate the dimensions and locations for the children of the component to decide where they are drawn. Quite tedious unless it is absolutely necessary.
If you really want that fine-grained control, maybe try GridBagLayout first before going mudding with the UI arrangement.
JPanel panel = new JPanel();
JTable table = new JTable(rowData, colData);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
panel.setSize(800, 150);
panel.add(table);
panel.setLocationRelativeTo(null);
panel.setVisible(true);
Hope this helps.
JFrame frame = new JFrame("Sample Frame");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
DefaultTableModel dfm = new DefaultTableModel(data, columnNames);
JTable table = new JTable(dfm);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
frame.add(panel);
frame.setVisible(true);
table model depends on your requirement
this.setTitle("Sample");
JPanel p = new JPanel(new BorderLayout());
WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
// Create columns names
String columnNames[] = { "FirstCol", "SecondCol",
"ThirdCol", "FourthCol" };
dataModel = new DefaultTableModel();
for (int col = 0; col < columnNames.length; col++) {
dataModel.addColumn(columnNames[col]);
}
// Create a new table instance
table = new JTable(dataModel);
table.setPreferredScrollableViewportSize(new Dimension(200, 120));
table.setFillsViewportHeight(true);
table.setShowGrid(true);
table.setAutoscrolls(true);
// Add the table to a scrolling pane
scrollPane = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(700, 700));
JPanel jpResultPanel = new JPanel();
jpResultPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Result",
TitledBorder.CENTER, TitledBorder.TOP));
jpResultPanel.add(scrollPane);
add(jpResultPanel);
pack();
setSize(720, 720);
setVisible(true);
Try this.
You can make use of the following code. To add JTable to JPanel.
JPanel panel = new JPanel();
this.setContentPane(panel);
panel.setLayout(null);
String data[][] = {{"1.", "ABC"}, {"2.", "DEF"}, {"3.", "GHI" }};
String col[] = {"Sr. No", "Name"};
JTable table = new JTable(data,col);
table.setBounds(100, 100, 100, 80);
panel.add(table);
setVisible(true);
setSize(300,300);