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.
Related
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 am trying to use Java Swing to create a simple GUI in which I have a drawing pad and some buttons it all works fine until I add this code for the JTextField:
String text = "hello";
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
textArea.setEditable(false);
Before adding this code the drawpad displays on the left of the screen followed by the buttons, when I add this only the drawpad is displayed unless I resize the frame in which case the buttons and text field reappear although the text field is hidden behind the drawpad slightly. Here is the full code:
public class testGUI extends Frame{
public static void main(String[] args) {
JFrame frame = new JFrame("Neural Networks");
frame.setSize(700, 300); //set the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //make it visible
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
final PadDraw drawPad = new PadDraw();
drawPad.setSize(100, 100);
content.add(drawPad);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
JButton loadButton = new JButton("Load");
loadButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Load something here
}
});
JButton testButton = new JButton("Test Draw Pad Image");
testButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//
}
});
JButton loadImage = new JButton("Test image from file");
loadImage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String filename = textField.getText();
}
});
String text = "hello";
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
textArea.setEditable(false);
mainPanel.add(clearButton);
mainPanel.add(loadButton);
mainPanel.add(testButton);
mainPanel.add(loadImage);
mainPanel.add(textArea);
content.add(mainPanel);
}
}
You're adding the drawPad and the mainPanel to the content panel, which uses BorderLayout, without specifying any location. They thus end up both in the center position of the border layout, which is supposed to contain only one component.
See How to use BorderLayout in the Swing tutorial.
Also note that setting the preferred size is not something you should do. Instead, the preferred size is supposed to be automatically computed based on other sttings (the contained components, the number of rows and columns of a text area, etc.)
And a JTextArea should be enclosed into a JScrollPane to be good-looking and allow you to scroll.
JPanel mainPanel = new JPanel();
The default layout for a JPanel is a FlowLayout, so all the components flow on a single row. If there is not enough room on the row then the components wrap to the next row.
So when you add the JTextArea the flow is disturbed. The solution is to use a combination of layout managers to get your desired layout effect. Read the section from the Swing tutorial on Using Layout Managers for more information and examples.
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
Also, you should NOT set the preferred size of the text area (or any Swing component for that matter). Instead you should do something like:
JTextArea textArea = new JTextArea(rows, columns);
and let the component determine its own preferred size. Also a text area is typically used with a JScrollPane and then you add the scroll pane to your panel:
JScrollPane scrollPane = new JScrollPane( textArea );
Edit:
Taking a second look at your code you have many more problems.
The point of using a layout manager is to have the layout manager set the size and location of the components. So your code should not have any logic related to the size/location of a component.
When you use the add(...) statement on a BorderLayout without a constraint, the component gets added to the CENTER. However only the last component added is managed by the BorderLayout. So only the "mainPanel" is given a size/location by the layout manager. That is why you need the setSize(...) statement on the drawPad to make the component visible. Although you now have the problem that two components are painted in the same space.
So to see the drawPad on the left you might want to use:
content.add(drawPad.BorderLayout.LINE_START);
However this still probably won't work because I'm guessing you are doing custom painting on the draw pad which means you will also need to override the getPreferredSize() method of the class so the layout manager can use the information to determine the size of the component. Read the section from the Swing tutorial on Custom Painting for more information and working examples.
Finally some other issues:.
The setVisible(...) statement should be invoked AFTER all the components have been added to the frame.
To follow Java standards, class names should start with an upper case character.
You should NOT be extending "Frame". There is no need to extend any class in your example.
Read the tutorial and download the demos for examples of better structured code.
My Goal is :
having a text pane where user can paste text only(with OS not a button!!!) and can't type in to the pane
because: can't have submit button (this is a must requirement - I know that it's not logical)
after pasting: the text-pane becomes not editable for user, and the program will change background at specific chars.
after button "clear filed" pressed - the program clears the pane and returns to initial state
My Problem: How to make text pane accept pastes but block typing
so far I made only JPanel with the text pane itself and all the controls will be in different JPanel
public class textPanel extends JPanel{
private JTextPane text;
public textPanel ()
{
setLayout(new BorderLayout());
text = new JTextPane(); //12,81
text.setBorder(BorderFactory.createLoweredBevelBorder());
add(text,BorderLayout.CENTER);
StyledDocument doc = text.getStyledDocument();
setBackground(Color.LIGHT_GRAY);
setPreferredSize(new Dimension(1000, 210));
}
thanks for help
Override public void paste() method of your. By default your JTextPane is not editable (setEditable(false)).
The paste() method's source in JTextComponent is
public void paste() {
if (isEditable() && isEnabled()) {
invokeAction("paste", TransferHandler.getPasteAction());
}
}
So you just make it editable, call super.paste(), and set the editable=false back after the super call.
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();
}
});
how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});