I have a class that extends JPanel. On the panel I have added an JLabel for a image and a few JTextArea (setEditable, setCursor, setOpaque, setFocusable to false) for text. This class is intended to be put in a list-fashion, top to bottom. When the user clicks on one of the JPanels some actions should happen depending on the selected item (JPanel). I have added an addMouseListener which works, but it wont register clicks if the user clicks somewhere where there is text. It work if I change the JTextArea to JLabel but the reason I used JTexArea is because I want/need wordwrap.
Any suggestions? Thanks.
Consider adding same mouselistener to JTextArea Also.
Related
I'm working on a GUI assignment and I encountered a problem that I cannot figure out. I have a JFrame with multiple JPanels, one of those JPanels contains a CardLayout with multiple JPanels. Since we are more people working on the project we decided to make a separate class (that extends JPanel) for each panel that's gonna be inside the CardLayout.
The problem is accessing components of the JFrame from the JPanel classes.
To give you an example, I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel (a class SaleMain that extends JPanel, contained in the CardLayout).
Another example, inside another panel EditCustomer (also a JPanel class, included in the CardLayout), I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.
Hope I made it as clear as possible, thank you guys in advance for helping me :)
The model / view / controller pattern (MVC) is useful for creating a GUI. By separating your model from your view, you can concentrate on one part of your GUI at a time.
You create a model for your GUI that contains the information you want to present on your GUI.
I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel
Put the text in your model, and in the action listener for the button, you have the text put in the status bar.
I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.
Then do so. The action listener is a controller that can change the view.
Take a look at my article, Dice Game, to see how a Java Swing application implements the MVC pattern and JPanel switching.
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.
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
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.
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.