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
Related
I am working on a barcode application which has in its main GUI a tabbed pane that contains many panels where one of this panels is for the cashier, the problem is that when the cashier panel is displayed i need to always set focus on the textfield where the barcode must be inserted.
requestFocusInWindow() or requestFocus() does not work since I am in the same jframe and in the same jTabbedPane but i am choosing panels inside this tabbedpane
what i need is when i move to a specific panel which is "cashier panel" is always to set focus on a certain textfield
thanks.
private void MainTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
if(CashierPanel.isShowing()){
Barcode_txt.requestFocus();
}
}
This may also work,
when you specifying barcodeText's properties i.e. Height,Width etc set also focusable true as
barcodeText.setFocusable(true);
I'm writing a code that, when I push a jButton, create and show in a jPannel a jRadioButton. In NetBeans I have writted, in the action method of the jButton, this part of code:
javax.swing.JRadioButton birdButton = new javax.swing.JRadioButton("ciao");
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand("ciao");
birdButton.setSelected(true);
jPanel1.add(birdButton);
jPanel1.revalidate();
jPanel1.repaint();
but, when I push the button the jRadioButton doesn't appear. The jPanel1 there is. What's the trouble? Thanks.
when you drag and drop jpanel to jframe in netbeans from visual component section,they set your panel layout to somelayout for example group layout
so when you add component you will not see it probably because there is different way to add components to different layout .not just .add() .you should add appropriate layout according to your expected design.
for example if you set layout to flowlayout you will see the jradiobuton as you expect.
this is how you can set layout to a component in netbean ide.
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.
I am facing problem during event handling.
The problem is like this:
My GUI has mainPanel (JPanel) which in turn consists of a panel with three buttons (namely btn1, btn2, btn3) at its WEST position.
I have created 3 more panels namely pnl1, pnl2, pnl3 (each panel has one label and one text area) using three different functions of same class.
My requirement is that if I click btn1 / btn2 / btn3 then pnl1 / pnl2 / pnl3 respectively must appear at mainPanel's CENTER position.
You will have to add action listener to the btn1, btn2, btn3. Then when the respective button is clicked you write a function that will display the required respective panels to the center of GUI.
To do so you can use cardLayout.
If you add detail to your question then we can help you with better answer or suggestions.
What's the purpose of the label and text area?
Add a screenshot of your GUI and some code that you have written.
Sounds like you want to put a CardLayout in the center, here's a tutorial.
You could consider creating a JPanel with a CardLayout for the CENTER panel. The CardLayout could contain 4 UI's ( the pnl1,pnl2,pnl3 and an empty panel ), and clicking on those buttons could activate the correct panel on the CardLayout
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.