I have a menu-bar and a tabbed pane in a frame, and i want that if i select a menuitem, then the requested tab will open. Please help me with this, thanks!!!
In the ActionListener of the JMenuItem, you can call JTabbedPane#setSelectedIndex.
like SoboLAN said:
final JTabbedPane tabs = new JTabbedPane();
JPanel panel = new JPanel();
tabs.add("title", panel);
//add more tabs...
// here the important part starts
JMenuItem item = new JMenuItem("open tab 1");
item.addActionListener(new ActionListener() {
//this function get called when you click the item.
#Override
public void actionPerformed(ActionEvent e) {
//insert the index you want to select
tabs.setSelectedIndex(0);
}
});
Related
I want to have a Button Group in which either only one option is selected or none of them are. At the moment, I can get it to have no options ticked by default, and then if one of them is ticked only one of them can be ticked, but I also want to be able to untick the button that was selected. Is this possible?
EDIT: Ideally without having a clear all button, as it would ruin the symmetry of my GUI. Also, here is my code thus far:
ButtonGroup option = new ButtonGroup();
for(int i = 0; i < n; i++) {
JCheckBox check = new JCheckBox("", false);
option.add(check);
row3b.add(check);
}
Just use the clearSelection() method of ButtonGroup :
ButtonGroup.clearSelection()
Clears the selection such that none of the buttons in the ButtonGroup
are selected.
This snippet demonstrates how you can clear selections using ButtonGroup.clearSelection():
//The buttons
JFrame frame = new JFrame("Button test");
JPanel panel = new JPanel();
JRadioButton btn1 = new JRadioButton("Button1");
JRadioButton btn2 = new JRadioButton("Button2");
JButton clearbutton = new JButton("Clear");
panel.add(btn1);
panel.add(btn2);
panel.add(clearbutton);
frame.add(panel);
frame.setVisible(true);
frame.pack();
//The Group, make sure only one button is selected at a time in the group
ButtonGroup btngroup = new ButtonGroup();
btngroup.add(btn1);
btngroup.add(btn2);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
clearbutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Clear all selections
btngroup.clearSelection();
}
});
As you can see, this creates two JRadioButtons and adds them to group then makes a button that clears selections. Really simple. Or you could create your own radio button class that allows for the unchecking of the button, which is also doable relatively easily.
I have a Swing application using Card Layout which basically changes the displayed panel depending on what the user selects from a drop-down menu.
One of my panels has a form. I would need for when the submit buton is pressed for all the inputs to be collected and the Panel to be switched to another one. (This second panel is defined in a separate class) I would also need for all the input to be somehow passed to a method in the new panel.
Any suggestions?
Dario
If you look at the <--s in the following code, each should solve each different question you have in your post. I figured you should know how to make a submit button, so I didn't include that. (Note: this is not running code, just suggestions);
public class MainPanel entends JPanel {
CardLayout layout = new CardLayout(); <-- card layout
JPanel panel = new JPanel(layout); <-- set layout to main panel
NewPanel newPanel = new NewPanel(); <-- you new panel
JPanel p1 = new JPanel(); <-- random panel
JTextField text = new JTextField() <-- text field in form
JButton button = new JButton();
JComboBox cbox = new JComboBox(new String[] {"newPanel", "p1"}); <-- hold panel names
public MainPanel(){
panel.add(newPanel, "newPanel"); <-- name associated with panel
panel.add(p1, "p1");
...
cbox.addAItemListener(new ItemListener(){
public void itemStateChnaged(ItemEvent e){
layout.show(panel, (string).getItem()); <-- show Panel from combobox
}
});
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String txt = text.getText();
newPanel.printText(txt); <-- Using method from other class
}
});
}
}
public class NewPanel extends JPanel {
public void printText(String text){ <-- method from other class
System.out.println(text);
}
}
I am currently trying to make a GUI with a menu that has 2 options you can select from. One being "Default Settings" and one being "Custom Settings." When you click on either one, it will take you to the new jPanel that will display the proper windows, text boxes, etc for that panel. However, I cannot seem to get the mouseClicked action to actually switch between the panels. As a test, I have a simple jLabel on each panel that says "Default" for the default panel and "custom" for the custom panel, and each menu item, when clicked respectively, should switch between them. Here is my current code:
frmLegitServerAdder = new JFrame();
frmLegitServerAdder.setTitle("Legit Server Adder 5 Million");
frmLegitServerAdder.setBounds(100, 100, 546, 468);
frmLegitServerAdder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmLegitServerAdder.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Settings");
menuBar.add(mnNewMenu);
JMenuItem menuItemDefaultSettings = new JMenuItem("Default Settings");
mnNewMenu.add(menuItemDefaultSettings);
JMenuItem menuItemCustomSettings = new JMenuItem("Custom Logon Settings");
mnNewMenu.add(menuItemCustomSettings);
frmLegitServerAdder.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel defaultSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(defaultSettingsPanel, "name_416522810155567");
defaultSettingsPanel.setLayout(null);
JLabel lblDefaultArea = new JLabel("Default Area");
lblDefaultArea.setBounds(217, 11, 90, 14);
defaultSettingsPanel.add(lblDefaultArea);
final JPanel customSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(customSettingsPanel, "name_416549691176064");
customSettingsPanel.setLayout(null);
JLabel lblCustomArea = new JLabel("Custom Area");
lblCustomArea.setBounds(235, 21, 46, 14);
customSettingsPanel.add(lblCustomArea);
menuItemDefaultSettings.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
defaultSettingsPanel.setVisible(true);
customSettingsPanel.setVisible(false);
}
});
menuItemCustomSettings.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
defaultSettingsPanel.setVisible(false);
customSettingsPanel.setVisible(true);
}
});
The code runs and the GUI displays just fine, but nothing actually happens when I click on either menu items, as it should. Any ideas?
You should NOT be using a MouseListener. Instead you should be adding an ActionListener to the menu item. Read the section from the Swing tutorial on How to Use Menus for more information.
You should be using a CardLayout when you want to swap components. See How to Use Card Layout from the same tutorial.
You need ActionListener
menuItemDefaultSettings.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
defaultSettingsPanel.setVisible(true);
customSettingsPanel.setVisible(false);
}
});
Hope this helps.
I have 1 JFrame and 10 JPanel components as seperate classes. There is also JMenuBar on jframe. When a menu item clicked, i remove all content of contentPane of jframe (removeAll) and add one of my jpanels.
Here is my code;
// this function changes panel
public static void SwitchPanel(Component comp)
{
Container panel = getContentPane();
panel.removeAll();
panel.add(comp);
panel.revalidate();
panel.repaint();
}
// this function defines menu items and their listeners
public JMenuItem AddMenuItem(JMenu menu, String name, final JPanel toPanel) {
JMenuItem menuItem = new JMenuItem(name);
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwitchPanel(toPanel);
}
});
return menuItem;
}
and i add menu items like that;
AddMenuItem(menu1, "some menu item", new MyPersonalJPanel());
Everything works. BUT i want to create new jpanel when a related menu item clicked. I mean create jpanel if only it is necessary. Current code creates all jpanels first. When i clicked a menu item, shows me jpanel created before.
I think it can be done with Class.forName method, but i couldn't figure it out. Any help?
You should do the new MyPersonalJPanel() in the public void actionPerformed(ActionEvent e) method. That way the panel would be created each time the user click on the menu.
Your code would then be:
// this function defines menu items and their listeners
public JMenuItem AddMenuItem(JMenu menu, String name) {
JMenuItem menuItem = new JMenuItem(name);
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwitchPanel(new MyPersonalJPanel());
}
});
return menuItem;
}
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.
}