I have written some code which is designed to create a table and display the clipboard data (which will be copied from excel) in that table.
So i have one menu item in the menu bar "Import from Excel", which when clicked will take whatever excel data is in the clipboard and display a table in the panel below.
Also, I've written it so that the panel (in which the table is displayed) is capable of being switched depending on any future menu buttons which could lead to a completely different panel being displayed below.
So I have this DisplayExcelData
class which implements a MainPage
interface (which contains an init method). When the menu item is clicked, it fires an action event in the extended menubar class, which feeds the content pane the MainPage
interface associated with that menu item (in this case DisplayExcelData
is fed to the content pane when "Import from Excel is clicked), then init is called and it is added to the content pane.
When the program launches and nothing has been selected, it defaults to a InitialScreen
class.
The DisplayExcelData class looks like this:
public class DisplayExcelData implements MainPageIF
{
private ExcelTableModel tm = null;
private Table table = new Table();
private JPanel thisPanel = new Panel();
public DisplayExcelData ()
{
super();
thisPanel.setLayout(new BorderLayout());
}
@Override
public void init()
{
//the test data I have been using is only 2 columns, so I'm just using
//these as test columns at the moment
tm = new ExcelTableModel("First, Second");
table.setModel(tm);
thisPanel.add(table, BorderLayout.CENTER);
}
@Override
public JPanel getPanel()
{
return thisPanel;
}
}
the ExcelTableModel takes the data in the clipboard and turns it into a TableModel
Then, in the contentPane, I have
public MainFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 800, 500);
compInit();
}
private void compInit()
{
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.setBackground(new Color(0,0,0));
setContentPane(contentPane);
//mf is the menu bar which is a class variable
MenuBar menuBar = mf.getMenuBar();
setPanelFromMenuBar();
setMenuBar(menuBar);
}
private void setPanelFromMenuBar()
{
MainPageIF page = mf.getPage();
if (page == null)
{
page = new InitialScreen();
}
page.init();
//mainPanel is a class variable
mainPanel = page.getPanel();
mainPanel.setVisible(true);
contentPane.add(mainPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source instanceof MenuItem)
{
contentPane.removeAll();
setPanelFromMenuBar();
getContentPane().doLayout();
update(getGraphics());
}
}
If I change the code so I boot the program with the DisplayExcelData
class as the initial screen, it works...it takes the excel data in the clipboard and displays it wonderfully in a table on the screen...if however, i insist that this table be displayed when i click on the MenuItem
, it just displays a blank screen. When I debug, and check what mainPanel
has in it...it contains the correct TableModel
with the Excel data, contained in a JTable
...but it wont display...I'm ripping my hair out as to why...
Can anyone help?
JFrame
classActionListener
(unless the ActionListener
must be accessible by other classes)As for your actual problem:
Try calling revalidate()
and repaint()
on your JFrame
instance after removing components and adding the new component:
contentPane.removeAll();//remove
setPanelFromMenuBar();//add
revalidate();//refresh ui and layout
repaint();
Or alternatively have a look at the CardLayout which allows flipping between JComponent
s like JPanel
s via CardLayout#show(...)