Hello, I'm here again with another issue. :)

I have written almost every part of my program what I'm gonna need (I did it as separate projects).
But now I need to create "main program". And I don't know exactly what is the best way to add these "parts" of program to main window when I click to menu in menu bar.
So far I have two parts- first one with drawing, and second one with a table which shows the records from database.

Recommended Answers

All 7 Replies

what you want is that they become visible when you click a button? add them all when initiating your application, but hide and show them when the buttons are clicked.

Under your main method (public static void main(String[] args)) call a createAndShowGUI() method that constucts and sets visible the main frame you want to use. Then under your public method you can use the JMenubar package to create menu items that open your other frames. Using this you can create menu items and add them to the menu using methods menuItem = new JMenuItem("part 1") and menu.add(menuItem).

To see the full example, go to:

http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html

Or you can simply constuct, add, set action listeners of buttons using the:

button = new JButton("Button"),
addActionListener(this) and
add(button) methods, as well as many other things.

Set visible the frame/ "part" you want to appear under each menu button.

Go to the Oracle docs to see these examples:

http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

so I should initialise them when program starts. And in case I have 3 "parts": when I click first, second and third become visible(false), when I click second one, first and third become visible(false) and so on...

Hm, I tried something right now, but I'm confused: I add a table in design window. Then I tried to set visible(false) for jScrollPane as program initialise and set it to visible when menu is clicked, nothing happened.

You don't have to keep setting the frames visibility as true and false, you can use the frame.dispose() method and frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) method. But either way works.

If you've added the menu item to the menu, remember to implement ActionListener for your class and use the addactionlistener method:

menuItem.addActionListener(this);

Check out the links I gave you, it's all there :)

Actually I don't have a problem with menu or action listener. It works fine.

Could anyone post some working example?? Just main form with menu, and click to some menu will call something (table or something) to that main form... Click to other menu will change it again and so on...

Could anyone post some working example??

why not, tutorials lined by @StephNicolaou contains a few examples,

Yes, few examples, but they weren't very useful for me, and I still didn't get it.

Anyway, I did it some other way...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.