开发者

Open new tab in frame other than the frame where button is clicked

I have 2 frames. One contains buttons that do appropriate actions on data. And secnd frame which contains data in tab view. Now I have to add one button in first frame by clicking 开发者_开发问答on this button a new tab should be added on second frame. How can I do this?


Make the JTabbedPane of the second frame accessible in first frame and then just simply call add() method of JTabbedPane to add a new tab in it.

Following is a sample code for that:

First frame:-

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(250,250);
JButton button = new JButton("Add tab to another frame.");
button.addActionListener(this);
frame.add(button);
frame.setVisible(true);

Second frame:-

Decalre a global variable of tabbedPane

JTabbedPane tabs;
int i = 0; // just a tab counter. You might not need this.

Initializaition code

JFrame frame2 = new JFrame("Demo 2");
frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame2.setSize(250,250);
tabs = new JTabbedPane();
frame2.add(tabs);
frame2.setVisible(true);

Action on button click in first frame:-

public void actionPerformed(ActionEvent e) {
    JPanel panel = new JPanel();
    panel.add(new JLabel("tab number "+i));
    tabs.add((i++)+"",panel); //--access tabbedPane of second frame here.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜