I have a JTextField inside a JPanel A which is a part of CardLayout. When this A gets shown, I want to set the focus automatically to the JTextField (i.e. the cursor is flashing in the text field so the user doesn't need to click on it to enable the input). I tried calling requestFocusInWindow() on the JTextField object at initialization, but that doesn't seem to work. Do I need to call this method every time when A gets displayed? Thanks.
Maybe you can try requestFocusInWindow() when the panel is shown ?
something like this?
jPanel.addComponentListener(new ComponentAdapter() {
#Override
public void componentShown(java.awt.event.ComponentEvent e)
{
jTextField.requestFocusInWindow();
}
});
Related
In my btcFrame class I have the method
private void closeButtonMouseClicked(MouseEvent evt){
this.dispose();
}
In my BtcTitleBarPanel I have in button for that I tend to close the frame. In the constructor I am adding my listener to closeButton like below.
closeButton.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
closeButtonMouseClicked(evt);
}
});
Problem is I cannot reach closeButtonMouseClicked method within the panel. How should I be doing to solve this?
Images in case.
Methods from jframe
Methods in jpanel
I would add a getter within the Panel for the closeButton. After initializing the Panel in the frame I would add the listener to button within the Frame. Like, btcPanel.getCloseButton().addActionListener(new ...); and within this action listener you could close the frame, like btcFrame.this.dispose()
Though I am not sure if that is a good practise, I almost always create new classes for listening events, but in this case of yours maybe helpful.
PS(Out of topic): You should see how to name classes and methods, your
naming style is wrong.(i.e classes starts with Capital etc.)
I am not able to get how to put up a text besides my button or text fields, when I made a separate code for just the label and a text field(copied as it from the internet) it is working, but it isn't in this case, please tell me how do I make it work? I got an idea like I might have to use some frame or some layout, but is it necessary? Just the basics please. Thanks. :
public class UIClass extends JFrame {
public UIClass()
{ super("GUIPart1");
super.setBounds(50,50,1000,700);
super.setResizable(true);
super.setVisible(true);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Button
JButton btn1=new JButton();
btn1.setBounds(50,50, 100, 50);
super.add(btn1);
super.setLayout(null);
btn1.setText("Test");
//TextField
JTextField tf1=new JTextField(15);
super.add(new JLabel("Label :"));
super.add(tf1);
tf1.setBounds(100,100,200,200);
//OptionPane
JOptionPane text=new JOptionPane();
String Message=JOptionPane.showInputDialog("My Message");
JOptionPane.showMessageDialog(null,"Thank you");
}
public static void main(String args[])
{
UIClass u=new UIClass();
}
}
You should call
this.repaint();
at the end. I wouldn't recommend using setBounds in JavaDoc it reads:
This method changes layout-related information, and therefore, invalidates the component hierarchy.
It was shown, when you resize window. This is valid for code with
JLabel jl=new JLabel("Label : "); jl.setBounds(300,300,400,400); super.add(jl);
as you mentioned in comment...
In your original code you explicitly set container layout to null.
In this case you must specify the position and the size of every other component you add and you did it for the button and textfield components only.
The code you posted in your comment you use BorderLayout as LayoutManager which automatically resizes and positions every component according to his strategy.
I strongly reccomend you to learn how to use LayoutManagers, because they are such a critical component in the Java UI environment, at least BorderLayout and GridLayout, because if your application is not too complex they should do the trick.
I quite new in java and I encounter a problem with repaint a TextFile in my JPanel, this is code:
JPanel paneldol = new JPanel();
paneldol.add(new JButton(new AbstractAction("Oblicz pole i obwód")
{
#Override
public void actionPerformed(ActionEvent e) {
paneldol.repaint();
paneldol.revalidate();
}
}
));
paneldol.add(new TextField(model.getPole(), 10));
paneldol.add(new TextField(model.getObwod(), 10));
this.add(paneldol, BorderLayout.PAGE_END);
As you see TextField have metod that generate string, so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?
so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?"`
If all you want to do is to change the text in text field on button push, then you should give your class a JTextField variable (or multiple JTextField variables), assign a JTextField object to the variable, and this to the GUI. Then when within your button's listener, simply set the text of the JTextField via its setText(...) method. There's no need to call repaint() or revalidate() as they will do nothing useful in this situation.
Also don't mix AWT with Swing components. Use JTextFields not TextFields.
I am not a java programmer. But i have to solve this problem in java. I have a textbox and a button. after entering some text in the text box , by clicking the button i have to generate a new button whose text is same as that in the textbox. I have to create the buttons and add then to a Jpanel. Can anyone help me with this.
This is the actionPerformed() i wrote, which was not working
public void actionPerformed(ActionEvent e) {
String str=textFeild1.getText();
panel1.add(new JButton(str));
}
Try adding panel1.validate();
public void actionPerformed(ActionEvent e) {
String str=textFeild1.getText();
panel1.add(new JButton(str));
panel1.validate();
}
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
validate()
In your case you add a component and you have to validate it.
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.).