How to add tabs on JPanels
I have a JPanel with a table on it. On that same JPanel, I want to add another table but as a totally separate table. So I want two tabs button somewhere on the top to interchange between the two tables. (first tab to stay on default table, clicking on second tab will go to the other table).
How do I go about in my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.awt.Font;
import javax.swing.plaf.FontUIResource;
import java.util.Calendar;
import java.awt.Color;
import javax.swing.table.*;
class table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private static JFrame openFrame;
public tabl开发者_如何学Pythone ()
{
//.....{some code work deleted to simplify}....
String columnNames[] = {"Time","Volume","Rate","CPP"};
String dataValues[][]= new String [counter][4];
for (int i= 0; i<counter; i++){
dataValues[i][0] =dataValues_timemap[i];}
for (int j = 0; j<counter; j++){
dataValues[j][1] = dataValues_vol[j]; }
for (int k = 0; k<counter; k++){
dataValues[k][2] = dataValues_rate[k]; }
for (int l = 0; l<countery; l++){
dataValues[l][3] = dataValues_cpp[l]; }
setTitle("Table Summary");
setSize(280,300);
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocation(1300,280);
table = new JTable( dataValues, columnNames );
scrollPane = new JScrollPane(table);
topPanel.add( scrollPane, BorderLayout.CENTER );
}
}
public static void main( String args[] )
{
// Create an instance of the test application
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );
}
}
Use a JTabbedPane
. It solves exactly this problem.
The JTabbedPane
can be added as a child of your JPanel
.
精彩评论