How to reach the panel of a certain JSlider? - java

I am trying the build a GUI, I have a set of radio buttons and I want a slider to show only when a specific radio button is pressed
I have set the visibility of the panel which contains the slider to false, but I can't reach that panel outside of AddVehicleDialog class (because it is a local variable of that class) and I need access for it in MyActionListener class.
I tried using getRootPane() to get the panel which the slider was added to, but it returned null.
//AddVehicleDialog.java class
public class AddVehicleDialog extends JDialog {
// All of the addVehicleDialog buttons, radio buttons and sliders
JButton okButton, cancelButton;
static JRadioButton redButton, grnButton, whtButton, svrButton;
static JRadioButton benzineCar, solarCar, bikeRadButton, carriageRadButton;
JSlider gearsSlider;
public AddVehicleDialog() {
setTitle("Add a vehicle to the city");
setLayout(new BorderLayout());
setSize(550, 300);
setResizable(false);
setLocationRelativeTo(null);
setModal(true);
// OK & Cancel buttons & radio buttons
addVehicleDialogButtons();
}
//bike's gears slider - part of addVehicleDialogButtons() method;
gearsSlider = new JSlider(0, 10);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
JLabel gearsLabel = new JLabel("Choose bike's gears");
gearsLabel.setHorizontalAlignment(JLabel.CENTER);
gearsLabel.setVerticalAlignment(JLabel.CENTER);
centerPanel.add(gearsLabel);
gearsSlider.setMajorTickSpacing(2);
gearsSlider.setMinorTickSpacing(1);
gearsSlider.setPaintTicks(true);
gearsSlider.setPaintLabels(true);
gearsSlider.setExtent(0);
centerPanel.add(gearsSlider,BorderLayout.SOUTH);
centerPanel.setVisible(false);
this.add(centerPanel,BorderLayout.CENTER);
//MyActionListener class is in a different file from centerPanel component
public class MyActionListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
.
.
.
case "OK":
if (AddVehicleDialog.bikeRadButton.isSelected()) {
//this is what i want to do
centerPanel.setVisible(true);
}
I know I can't reach centerPanel, because it is a local variable of AddVehicleDialog class.
I've made the gearsSlider static so I could reach him in MyActionListener class
I don't think it is a good practice to put centerPanel as static, but I can't think of other way to reach this specific panel outside of the class.
I hope it is understandable enough. If more clarification is needed, please tell and I'll provide.
The panel when visibility set to false:
The way I want the panel to look when I select Bike radio button.

Related

Why is JPanel not adding any component at Runtime?

public class Create_JFrame extends JFrame{
public Create_JFrame(){
//Create a Frame
JFrame Frame = new JFrame("Bla-Bla");
JPanel Panel_1 = new JPanel();
JPanel Panel_2 = new JPanel();
JButton Option_1 = new JButton("Option-1");
//Layout management for Panels
Frame.getContentPane().add(BorderLayout.WEST, Panel_1);
//Add button to Panel
Panel_1.add(Option_1);
//Registering Listeners for all my buttons
Option_1.addActionListener(new ListenerForRadioButton(Panel_2));
//Make the frame visible
Frame.setSize(500, 300);
Frame.setVisible(true);
}//end of Main
}//end of Class
public class ListenerForRadioButton implements ActionListener{
JPanel Panel_2;
JButton browse = new JButton("Browse");
//Constructor, will be used to get parameters from Parent methods
public ListenerForRadioButton(JPanel Panel){
Panel_2 = Panel;
}
//Overridden function, will be used for calling my 'core code' when user clicks on button.
public void actionPerformed(ActionEvent event){
Panel_2.add(browse);
System.out.println("My listener is called");
}//end of method
}//end of class
Problem Statement:
I have 2 JPanel components in a a given JFrame. Panel_1 is having a Option_1 JButton. When user clicks on that I am expecting my code to add a JButton 'browse' in Panel_2 at runtime.
Runtime Output:
System is not adding any JButton in Panel_2. However, I see my debug message in output, indicating that system was successful in identifying user's click action on 'option-1'.
Question:
Why is JPanel not adding any component at Runtime?
Panel_2.add(browse);
Panel_2.revalidate();
adding a 'revalidate' will solve the problem.
There are some reasons. but:
usually it's because of using unsuitable LayoutManager.
sometimes it's because of adding the JPanel to it's root component in worng way. which any operation (add, remove,...) works but is not visible.
you must refresh the view when you make some changes on it, like adding or removing components to/from it.
try to use Panel_2.revalidate() to refresh.
if it doesn't work properly use it with Panel_2.repaint() method.
see Java Swing revalidate() vs repaint()
see Difference between validate(), revalidate() and invalidate() in Swing GUI
using setSize twice for your jframe is another way.
Frame.setSize(498, 300); then Frame.setSize(500, 300);

Java Swing JButton Actionlistener to change another package

So I have a code and my problem is toggling visibility of JPanel with JButton. Here are my Packages - Classes:
start - Main
panels - Panels
labels - Labels
I am creating the button in package labels class Labels with this
public static JButton ScriptDLBotton(JPanel navPanel) {
JButton contactsButton = new JButton("Show");
contactsButton.setVisible(true);
contactsButton.setBounds(250,82,70, 20);
navPanel.add(contactsButton);
return contactsButton;
}
I am calling it from start.Main with this
JButton ScriptDLbutton = Labels.ScriptDLBotton(AppsPanel)
And I want when I press the button it go to package panels class Panels and change
public static JPanel createAppDiscriptionPanel(JFrame frame) {
JPanel navDiscPanel = new JPanel ();
navDiscPanel.setVisible(false);
navDiscPanel.setBounds(350,100,640, 380);
navDiscPanel.setBackground(Color.yellow);
navDiscPanel.setLayout(null);
frame.add(navDiscPanel);
return navDiscPanel;}
from visible false to true. Thx in advance for any help.

Java Swing: Access panel components from another class

Hi i basically have two classes,
one main and one just to separate the panels, just for code readability really.
i have :
public class Main{
public static void main (String args[]) {
JFrame mainJFrame;
mainJFrame = new JFrame();
//some other code here
CenterPanel centerPanel = new CenterPanel();
centerPanel.renderPanel();
mainFrame.add(centerPanel.getGUI());
}
}
class CenterPanel{
JPanel center = new JPanel();
public void renderPanel(){
JButton enterButton = new JButton("enter");
JButton exitButton = new JButton("exit");
center.add(exitButton);
center.add(enterButton);
}
public JComponent getGUI(){
return center;
}
}
Above code works perfectly. It renders the centerPanel which contains the buttons enter and exit. My question is:
I still need to manipulate the buttons in the main, like change color, add some action listener and the likes. But i cannot access them anymore in the main because technically they are from a different class, and therefore in the main, centerPanel is another object.
How do I access the buttons and use it (sets, actionlisteners, etc)? even if they came from another class and i still wish to use it inside the main?Many Thanks!
Make the buttons members of CenterPanel
class CenterPanel{
JPanel center = new JPanel();
JButton enterButton;
JButton exitButton;
public void renderPanel(){
enterButton = new JButton("enter");
exitButton = new JButton("exit");
center.add(exitButton);
center.add(enterButton);
}
public JButton getEnterButton()
{
return enterButton;
}
public JButton getExitButton()
{
return exitButton;
}
public JComponent getGUI(){
return center;
}
}
You have reference to centerPanel in your main method. After you invoke centerPanel.renderPanel();
the buttons will be added to the 'center' reference of type JPanel in CenterPanel instance. You can get the reference of 'center' by invoking centerPanel.getGUI(); in main method.This will return the center reference of type JComponent. JComponent is a awt container.so, you can call center.getComponents(). This will return all the components in an array present in the JPanel. i.e. center reference. You can iterate them, get their type and do whatever you want.

is it possible to point to a particular JPanel

I have this program in which I'm using CardLayout. I have different panels with different attributes. I have a button called "Enter" that I decided to reuse on every panel, however each panel performs a different operation when the button is clicked. Is there a way to say, when button is clicked but I am at a specific panel, then do this. How can I point directly to a panel?
First thing you must consider is: You can't add one button to many panels, every panel should have it's own component(s).
If you add one button to many panels say :
JButton b = new JButton("Button");
//....
pan1.add(b);
pan2.add(b);
pan3.add(b);
In such case, the button will be added to the last panel means pan3, the other won't show the button.
Second, I would like to mention a #trashgod's good example from comments, and also in case confusing, look at this example:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class CardLayoutDemo extends JFrame implements ActionListener {
private CardLayout cardLayout;
private JButton pan1,pan2;
private JPanel mainPanel;
public CardLayoutDemo(){
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
pan1 = new JButton("To Second Panel");
pan2= new JButton ("To First Panel");
pan1.addActionListener(this);
pan2.addActionListener(this);
p1.setBackground(Color.green);
p2.setBackground(Color.BLUE.brighter());
p1.add(pan1);
p2.add(pan2);
mainPanel.add(p1,"1");
mainPanel.add(p2,"2");
cardLayout.show(mainPanel, "1");
add(mainPanel);
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
setVisible(true);
pack();
}
#Override
public void actionPerformed(ActionEvent ev){
if(ev.getSource()==pan1)
cardLayout.show(mainPanel, "2");
else if(ev.getSource()==pan2)
cardLayout.show(mainPanel, "1");
}
public static void main(String...args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CardLayoutDemo().setVisible(true);
}
});
}
}
You can let the panel assign an ActionListener to the button each time the card is created. That way the constructor for a specific panel can determine what the functionality of the button will be.

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();
}
});

Categories