updating a panel - java

I have a panel on my frame .and by clicking on a button I want to delete the old panel and make the other panel and add that panel to my frame.(also I use netbeans)
would you please help me that how can i do that?thanks

JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);
MouseListener ml = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// Mouse clicked on panel so remove existing panel and add a new one.
frame.remove(origPanel);
frame.add(createNewPanel(), BorderLayout.CENTER);
// Revalidate frame to cause it to layout the new panel correctly.
frame.revalidate();
// Stop listening to origPanel (prevent dangling reference).
origPanel.removeMouseListener(this);
}
}
origPanel.addMouseListener(ml);

This way:
final JFrame frame = new JFrame();
frame.setSize(200, 200);
final JPanel panelA = new JPanel();
final JPanel panelB = new JPanel();
JButton button = new JButton("Switch");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(panelA);
frame.add(panelB);
frame.show();
}
});
JLabel label = new JLabel("This is panel B. Panel A is gone!");
panelB.add(label);
panelA.add(button);
frame.add(panelB);
frame.add(panelA);
frame.show();

Related

Java GUI switching between panels on button click

I am learning Java and I have to develop an application using a GUI. I have the application working in command line already, but the GUI is driving me insane and costing me in lost hours of head banging and research which is leading nowhere. Can you please help me get the basics working so that i can develop further from there. I want to have a single frame application that can switch between frames on a button click. I created a frame and added three panels P1-P3. These are set as Card Layout (from what i read from forums). Then I added additional panels to these to which i have set colour and buttons.
'''
public class MyMainForm extends JFrame{
private JPanel P1;
private JPanel P2;
private JPanel P3;
private JButton btnFrame1;
private JButton btnFrame2;
private JButton button1;
private JTextField thisIsPanel3TextField;
private JButton btn2Frame1;
private final JFrame frame = new JFrame("MyMain Frame");
public MyMainForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(P1);
pack();
setSize(1000,800);
//setLocation(null);
btnFrame1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
P1.setVisible(false);
setContentPane(new MyMainForm().P2);
}
});
btnFrame2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
P2.setVisible(false);
setContentPane(new MyMainForm().P3);
}
});
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
P3.setVisible(false);
setContentPane(new MyMainForm().P2);
}
});
btn2Frame1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
P1.setVisible(false);
setContentPane(new MyMainForm().P3);
}
});
}
public static void main(String[] args) {
MyMainForm MyMainForm = new MyMainForm();
MyMainForm.setVisible(true);
}
}
'''
I can display P2 or P3 with this new code example above. When i try to go from P2 or P3 back to P1 the content pane doesn't show? Do i need to revalidate the content pane for this to work? I really need to be able to go from P1 to P2
The easiest way to do this is to use a CardLayout. Just follow this example:
JFrame frame = new JFrame();
JPanel p1 = new JPanel();
p1.setBackground(Color.RED);
JPanel p2 = new JPanel();
p2.setBackground(Color.WHITE);
JPanel p3 = new JPanel();
p3.setBackground(Color.BLUE);
//Create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
cards.add(p1, "Panel 1");
cards.add(p2, "Panel 2");
cards.add(p3, "Panel 3");
// Add your card container to the frame
Container pane = frame.getContentPane();
pane.add(cards, BorderLayout.CENTER);
JButton btn = new JButton("Click me!");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed (ActionEvent e) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.next(cards);
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(btn);
pane.add(btnPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
Alternatively, you can switch to a specific panel by calling cl.show(cards, "Panel X") where X is the number of the panel. This is because the swing argument is the name I assigned to each "card" and the show method recalls panels added to CardLayout by name. For your example, each button should have a listener that uses this method to "show" its assigned panel.

How to have a JButton that moves as I scroll on the frame?

I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

Change color of JButton without changing its shape

I want to change the color of JButton by:
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
In order for change to occur, I have to add:
button.setOpaque(true);
button.setBorderPainted(false);
However, this remove the curves around the edges and thus changes the shape of the button. Is there a way to just simply change to color and keep the other properties? Another example is the changing of color (getting darker) when you press a button without having changed its color.
Here is some code that illustrates the difference between the two buttons:
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button1 = new JButton();
JButton button2 = new JButton();
button1.setPreferredSize(new Dimension(100,100));
button2.setPreferredSize(new Dimension(100,100));
button2.setBackground(Color.decode("#00a5ff"));
button2.setBorderPainted(false);
button2.setOpaque(true);
JPanel pane1 = new JPanel(new FlowLayout());
JPanel pane2 = new JPanel(new FlowLayout());
pane1.add(button1);
pane2.add(button2);
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Thanks
I literally just tested the following:
JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button = new JButton();
button.setBackground(Color.decode("#00a5ff"));
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
It seemed to work, but I am working on Ubuntu Studio 16.04. If you notice that it does not work, then let me know. Could you please show the result of your white button or your failed button (if it still doesn't work)?
Perhaps you have something else going on in your code. I altered a small example I have and the color changes with only setBackground(Color) using regular Button and using JButton. See the following...
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
Button closeButton = new Button("Close");
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
frame.dispose();
}
});
closeButton.setBackground(Color.decode("#00a5ff"));
panel.add(closeButton);
frame.add(panel);
frame.setSize(200, 100);
frame.setLocation(200, 50);
frame.setVisible(true);
}

Content is being add horizontally to JScrollPane

im starting to work with java Swing and i was trying to make a system to show something like a Map :
But when i try to add another entry to the JSCrollPane it's being added horizontally intead of vertically, i have tried everything i don't what i mithgt be doing wrong but i can't manage do fix it.
Here i create the Frame :
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
final JPanel content = new JPanel();
new DataEntry("", 0).create(content);
final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
panel.add(scrollPane);
scrollPane.setMinimumSize(new Dimension(400, 60));
final JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new DataEntry("", 0).create(content);
}
});
panel.add(add);
frame.setContentPane(panel);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
And this is how i create the Entry :
public JPanel create(final JPanel content) {
final JPanel panel = new JPanel();
final JPanel fields = new JPanel();
fields.add(new JLabel("Variable"));
fields.add(variable);
fields.add(new JLabel("Row"));
fields.add(row);
panel.add(fields);
JButton remove = new JButton("Remove");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
content.remove(panel);
content.revalidate();
}
});
panel.add(remove);
content.add(panel, BorderLayout.CENTER);
content.revalidate();
return panel;
}
At the start i was wondering why it wasn't displaying any new Entry, then i tried changing HORIZONTAL_SCROLLBAR_NEVER > HORIZONTAL_SCROLLBAR_AS_NEEDED and then i realized that the variables were being add horizontally.
Here is a gif to see what's going on (Couldn't manage to take a proper photo) : GIF

How to open a new window by clicking a button

As a part of my program, I need to have a button that when the user click on it, it opens a new window.
Well I guess I should have a class that make the frame and call it by the button. but I don't have any idea to start. I just got my button in the program, but it does not work. So can some tell me how to do it? or code it.
Here is a simplified version of what you want to do:
JButton button = new JButton("New Frame");
button.addActionListener( new ActionActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Create a method named "createFrame()", and set up an new frame there
// Call createFrame()
}
});
You would probably want to call some method in the ActionListener rather than make the frame on actionPerformed. Maybe something like this:
public static void createFrame()
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
What that frame should look like:
new CLASS_NAME().setVisible(true);
eg. new NewJFrame().setVisible(true);

Categories

Resources