JFrame with JTabbedPane & JButton
I have a Jframe declared and adda JTabbedPane to it.
There are 4 tabbedPane and each has a table content.
Now, I need to add a refresh button to each tab, how can I do this?开发者_JAVA百科
This is how I'm doing:
frmSql = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Events", retrieveData("events"));
tabbedPane.addTab("Completed Events", retrieveData("completed"));
frmSql.add(tabbedPane);
Any suggestions?
static void createAndShowGui() {
JFrame frmSql = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane();
Action refreshEvents = null, refreshCompletedEvents = null;
tabbedPane.addTab("Events", createTab(retrieveData("events"), refreshEvents));
// more tabs
tabbedPane.addTab("Completed Events", createTab(retrieveData("completed"), refreshCompletedEvents));
frmSql.setContentPane(tabbedPane);
}
static JComponent createTab(JComponent content, Action refreshAction) {
JPanel p = new JPanel(new BorderLayout());
p.add(content, BorderLayout.CENTER);
JPanel btns = new JPanel();
BoxLayout layout = new BoxLayout(btns, BoxLayout.LINE_AXIS);
btns.setLayout(layout);
JButton refreshBtn = new JButton(refreshAction);
btns.add(refreshBtn);
btns.add(Box.createHorizontalGlue());
p.add(btns, BorderLayout.PAGE_END);
return p;
}
Of course, if retriveData
is time-consuming, it shouldn't be called from EDT
精彩评论