change JPanel after clicking on a button - java

I'm building simple GUI for my app. I have couple of JPanels. I want to display them depending on action that was performed by clicking on a JButton. How can I disable one JPanel and enable another one ?
Couple of details. I have a class with JFrame where I'm building starting gui. Where I have buttons and some text. Clicking on one of the buttons should change the view in this JFrame
my button definition
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnStart.setBounds(10, 11, 110, 23);
contentPane.add(btnStart);
// edit
I've found the problem. buttons were in static method

Simple as:
jframe.setContentPane(your_new_panel);
jframe.invalidate();
jframe.validate();

You may want to use CardLayout.
Or you can simple remove the oldpanel and add new panel:
contentPane.remove(oldPanel);
contentPane.add(newPanel);

First remove the jPanel and add the new jPanel. Then use validate to perform relayout.
jFrame.remove(jPanelOld);
jFrame.add(jPanelNew);
jFrame.validate();

Related

Using JButtons as Tabs

Could I use JButtons as tabs? Since the JTabbedPane cannot hold the same component in multiple tabs, would there be a way for a JButton to be a tab? I know it looks like tiDE(Website) uses the JButtons as a tab. How would I do that?
I could make something like this
JButton newTab = new JButton("New Tab");
newTab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JButton tab = new JButton("Tab 1");
JToolBar.add(tab)
tab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeTextAreaTab();
}
}
);
}
}
);
But, how would I make the method makeTextAreaTab()? It would have to be the same component as my other editor(JTextArea), and have the same functionality as a JTabbedPane.
You state in a comment:
I would like to be able to have buttons serve the functionality of tabs. Click on one button, it moves to one editor. Click on another button, it moves to another editor.
Consider using a CardLayout for this where your JButtons (or perhaps better, a JComboBox) tells the CardLayout-using container which "card" (which component -- here a JScrollPane/JTextArea combination) to display.

performing multiple actions on one button in java swing

I am trying to create a wizard in java swing programatically.
On the wizard pane I have a next button and it has to perform multiple actions according to which panel is displayed on the wizard.
Is it possible to use java command pattern? may I know how?
thanks in advance.
the code i used for the wizard is
this.mainPanel.add(fileSelectionPane,"SELECT FILE");
this.mainPanel.add(sqlConnectionPane,"SQL CONNECTION");
this.mainPanel.add(devicePane,"PARSER");
this.mainPanel.add(detailsPane,"DISPLAY");
thisLayout.show(this.mainPanel,"SELECT FILE");
this.finishButton.setEnabled(false);
this.backButton.setEnabled(false);
if(newValue==1) {
this.thisLayout.show(this.mainPanel, "SQL CONNECTION");
this.nextButton.setEnabled(true);
this.nextButton.setText("Connect..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
if(newValue==2) {
this.thisLayout.show(this.mainPanel, "PARSER");
this.nextButton.setEnabled(true);
this.nextButton.setText("Parse..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
i want the next button to perform specific actions on SELECT FILE and SQL CONNECTION.
is it possible to use command patterns?
Ok, so, You add action listeners to buttons. These action listeners do something when an event occurs.
You want to change the functionality of the button depending on which panel is being displayed? Why not set a instance variable which reflects the state of the Wizard?
For example (roughly),
int state = 0; // home panel
change panel to help page, event listener is fire, set 'state' to 1. You are now tracking which panel is being displayed.
Now, in your original problem, when the button (the one you want multiple functionality with) fires, you can choose the action it will take based on the 'state' var.
have look at CardLayout
those cards put to the JDialog (JDialog has preimplemented by default BorderLayout) to the CENTER area
create a new JPanel and place there JButtons
JPanel with JButtons put to the SOUTH area
search here, on this forum, there are a few excelent examples for wizard or image previue based on CardLayout
try the following code for button:
JButton btn1;
btn1= new javax.swing.JButton();
btn1.setToolTipText("Submit");
btn1.setContentAreaFilled(false);
btn1.setBorderPainted(false);
btn1.setMargin(new java.awt.Insets(2, 2, 2, 2));
btn1.addActionListener(this);
btn1.setIcon(this.getIcons()[21]);
add(btn1); // add to Jpanel
btn1.setBounds(250,10, 12, 12);
public void actionPerformed(java.awt.event.ActionEvent evt) {
Object obj = evt.getSource();
if (obj == btn1) {
// your function on on click of button
return;
}

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

adding components dynamically in a JPanel

how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});

Rich swing radiobutton

Developing a desktop application based on Java + Swing I faced the problem of creating a radio button which instead of text next to it, should have and image or, say, another widget like a spinner.
Clicking on the image or the spinner should select also the corresponding radioButton.
Is it possible? if so, how?
To me, the JRadioButton with icon given for constructor doesn't seem to work; it replaces the "native radio button icon" with given icon. I think to original asked wanted for radio button with icon in addition to the "radio button icon".
There has been some debate on the behaviour at Sun bug database with Bug #4177248 but no changes have been made.
Instead, one could try JRadioButtonMenuItem, even though there will probably be some non-wanted behaviour with that?
Short evaluation for both JRadioButton and JRadioButtonMenuItem:
public class IconRadioButtonEval {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
// Use some arbitrary working URL to an icon
URL url =
new URL(
"http://mikeo.co.uk/demo/sqlspatial/Images/RSS_Icon.png");
Icon icon = new ImageIcon(url);
JRadioButton button = new JRadioButton(icon);
panel.add(new JLabel("RadioButton with icon:"));
panel.add(button);
panel.add(new JLabel("RadioButtonMenuItem with icon:"));
panel.add(new JRadioButtonMenuItem(icon));
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Justin's suggestion for another component next to JRadioButton with empty string should probably work well in most cases.
Well, there is a constructor for JRadioButton that takes an Icon. You can use that to make it have an icon next to it.
JRadioButton(Icon icon)
-- Creates an initially unselected radio button with the specified image but no text.
Otherwise, it is fairly easy to make the JRadioButtons text empty, and place another component next to it. Then you will need to add a Listener to that component, so that the JRadioButton gets clicked if the other component gets clicked.

Categories