Panel not appearing on clicking button - java

I am using group radio buttons.What i want is that when i have selected the particular radio button then i press the button(call it show button) to display the results(on a chart placed in a panel) according to the selected radio button.The problem is when i press the show button it does not display results until i click the panel though i have not written any code when i press the panel.Here is my code
private void Show1MouseClicked(java.awt.event.MouseEvent evt) {
if (jRadioButton1.isSelected()) {
Panel.removeAll();
//some code
}
Panel.setVisible(true);
Panel.add(frame1);
Panel.setSize(700, 260);
}

Use a CardLayout, as shown here.
As to the chart itself, change the model if the data changes.

You probably need to call repaint() on your panel after the change. When you interact with the panel with your mouse it is calling repaint() behind the scenes.

Related

Java Detect MouseClick anywhere in Window doesn't work in certain areas

So I have a Java Swing program and I want to be able detect mouse clicks.
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
update(evt); //another method in the program
}
});
The code works if I click on the window's side or places where there isn't an object, but does not work when I click on objects in the JFrame, such as my JTable or my text field.
Please help me how to have the MouseListener work on objects inside the JFrame as well.
When you click on a textfield, the textfield gains focus. That means your frame loses ownership of focus, and since your listener is most likely added to your frame, your listener stops working right when frame isnt in focus. Add your listener to all compoments, or use Key Binding

JFrame window with sub components

So I have created this JFrame window which has a lot of items like JLabel, JComboBox JTextField etc… At the bottom it has a "next" JButton.
I want that when a user clicks the next button, everything on the screen should be removed and replaced with stuffs from other class that I have created.
I only manage to open a new JFrame window whenever I click the next button. Can somebody please tell me how to remove all items from the screen and replace them with items from another class.
Thanks. I am a newbie so please give me the easiest way possible.
This sounds like a job for CardLayout
You could create a base panel in the BorderLayout.SOUTH position of your JFrame that would have your navigation buttons and have a number of panels added to your main panel being managed by CardLayout.
See Creating Wizard Dialogs with Java Swing
While the systematic thing for it is using CardLayout, you can imitate it if you don't want to learn how to use it!!
Create a Panel, add all items except the next button to this panel. Use BorderLayout to put the panel on top of the next button in the frame.
Now when the user press the next button you remove the panel (jframe.remove(panel)). create a new JPanel and add it using the BorderLayout again on top of the next button.

Get focus on a JTextField from another panel when a CardLayout changes

hi need help on a Swing application that I am doing. I have a dialog with two panels, the first panel has a CardLayout and the second has a FlowLayout. The first layout has buttons that change the card layout and the buttonclick is entered to a specific textfield on the second panel. Every time the card layout changes, the textfield on the second panel loses its focus. How to get the focus of a specific textfield of the second panel?
when you are clicking on a button to go anohter panel
write the code inside the "actionPerformed" of that button
actionPerformed(ActionListenet al)
{
//code..
textField.requestFocus();//what the textfield u wnat
}
refer this link Setting the focus to a text field

How can I generate a new panel based on where my JTable is double-clicked?

I need help implementing the following behavior: when a user double-clicks on a row in the JTable on JPanel A — code snippet for this shown below — the program should redirect the user to JPanel B. B should contain some data about whatever was on the JTable's row.
private void TableMouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() == 2){
System.out.println("Double click");
}
}
What do you mean, "redirect the user to?"
If the other panel is already visible, you can switch the focus there (though that's a bit unusual behavior which may negatively surprise your user) using setFocus() on the second panel.
If the other panel is not visible but has its own space in the GUI, then I guess you'd simply make it visible. If it has to overlay the panel the user just clicked on, then you want to use a CardLayout to display two panels alternatively in the same space.
Check out this thread: http://forums.sun.com/thread.jspa?threadID=366670
This is essentially the same thing you are trying to do, you just want to respond to mouse double-clicks instead of mouse move events.

Java Swing Maximize button of JFrame disappearss

I have created a java frame. It contains a JSplitPane. In the right partition of the splitpane , I have put JTextPane. Now I have kept JButton somewhere on the frame. When I click the button, a new JTextArea is inserted in the JTextPane as follows
JTextArea newTextBox = new JTextArea(1, 1);
StyleConstants.setComponent(style, newTextBox);
My problem is as soon as I insert JTextArea in JTextPane, the maximize button of my JFrame disappears. Can anyone tell me what could be the reason of happning this.
Maxmize button is unavailable on two conditions:
For a Dialog, in which case only close(X) button is availabe.
If frame.setResizable(false) has been called(i.e diabled resize of window)
So applying this to your case, my best guess is that somewhere in your button's action you may have made it unresizable.

Categories