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.
Related
I am writing the back end code for my java project which is implementing a fitness logger. For some reason when I put the actionListener function to a button in my Border Layout then the button disappears.
I tried setting the function in different places in the constructor.
public class Buttons extends JFrame implements ActionListener {
JFrame frame = new JFrame("Menu");
JPanel MyPanel= new JPanel();
JButton b1= new JButton("Daily Logger");
JButton b2= new JButton("View Weekly Logs");
JButton b3= new JButton("Weight Calculator");
JButton b4= new JButton("BMI Calculator");
JButton b5= new JButton("Log Out");
public Buttons(){
MyPanel.setLayout(new BorderLayout());
MyPanel.add(b1, "North");
MyPanel.add(b2, "Center");
MyPanel.add(b3, "East");
MyPanel.add(b4, "West");
MyPanel.add(b5, "South");
b1.addActionListener(this);
add(b1);
frame.getContentPane().add(MyPanel, "North");
frame.setSize(500,115);
frame.setVisible(true);
}
public static void main(String[] args) {
new Buttons();
}
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("Daily Logger"))
myMethod();
}
public void myMethod() {
JOptionPane.showMessageDialog(this,"Onto the next step");
}
}
I expected the button to show up in the border layout when I add the actionListener function but it disappears. If the button did work as it should it should implement the myMethod() function. My main goal is to show my JTable that I created in another class to show up when the button is pressed.
add(b1);
Should be removed. The code has already added it via:
MyPanel.add(b1, "North");
A BorderLayout accommodates up to five components, each in a separate layout constraint. If a component is added twice, in different areas, it faces two problems:
A component can only appear in one place.
The component 'overwrites' the original component added to that area of the layout.
More general tips:
Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.
For better help sooner, add a minimal reproducible example or Short, Self Contained, Correct Example. Note: The posted code only needed appropriate import statements to be an MRE / SSCCE.
MyPanel.add(b3, "East"); re East
There are constants for this. E.G. BorderLayout.EAST. always use the constants for compile time checking.
But BorderLayout.LINE_END is sensitive to the locale. It will appear on the RHS for left to right languages, and the left for right to left languages.
All Swing & AWT GUIs should be created & updated on the EDT (Event Dispatch Thread).
The code both extends and keeps a reference to, JFrame. Keep the latter, ditch the former.
Be sure to add the java (language) and swing (GUI toolkit) tags to questions! The only reason I saw this was because I (extraordinarily) checked the question listing for the jframe tag! To the best of my recollection, it's the first time I've ever checked that tag's question listing. Even more ironic, it did not make the list of 5 tags I saw as being most relevant to this question.
frame.getContentPane().add(.. can be shortened to frame.add(.. since Java 1.5.
frame.setSize(500,115); should better be frame.pack();, since 500 x 115 is no better than a guess, and will be 'wrong' for different OS' (the size of the content pane will change due to different frame decorations per system).
i just started to use Java to build a GUI. Now i ran in an error that causes very strange behaviour with the JTextArea. I used this to create the TextArea:
`
public class gui{
JTextArea ausgabe;
public gui(){
//Some other Stuff in here
//
//
ausgabe = new JTextArea("Test \n Text",15,50);
ausgabe.setSize(110, 170);
ausgabe.setVisible(true);
mainFrame.add(ausgabe);
ausgabe.setLocation(170,20);
ausgabe.setEnabled(false);
}
}
Now until this point everything just works fine. But I want another method to change the text of the area (with ausgabe.setText("String");) the area relocates itself to x,y = 0 of the JFrame and layers itself above all other JFrame elements. Thanks for help!
I highly recommend not trying to fight the Layout Managers
For a simple fix, use the default layout manager for a JFrame like so
public class gui
{
JTextArea ausgabe;
public gui()
{
ausgabe = new JTextArea("Test \n Text");
mainFrame.add(ausgabe, BorderLayout.CENTER);
}
}
You also don't need to set Swing Components other than the JFrame itself visible.
You can then call setText() in an action listener or such and the TextArea should stay in the same position.
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 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.
I am trying to make a refresh button that will essentially restart the program when ever I click the button. I don't know how I should go about doing this.
I've place the Graphical User Interface i decided to use do complete this action. Any and all help would be greatly appreciated.
package pdfView;
import javax.swing.*;
import java.awt.*;
public class View extends JFrame {
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
JButton ReFresh = new JButton("ReFresh");
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.squing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc){
}
}
public static void main(String[] args) {
View pdf = new View();
}
}
What do you mean by refresh or restart?
Do you mean:
Let the application be as it is, just update what it's showing?
Really restart the application?
Updating what the application is showing
You first need to decide what actually should cause your application to refresh. You already talked about a Button. The mechanism for activating something like a button is called Action. You can do that stuff manually, using an ActionListener, or you could extend AbstractAction, which is what I recommend. Extending AbstractAction allows you to use the same logical action something in more than one location on the UI. Look at typical applications, they offer Cut/Copy/Paste through menu, toolbar, popupmenu and keyboard shortcuts. The simplest way to achieve this in Java is using Action by extending AbstractAction.
The methods you need to call to update your application are invalidate(), validate() or repaint().
Restarting an application
So you want to run through main() again? That should actually not be required, unless you have an application that supports updating itself. Even then it can sometimes be avoided by smart usage of a ClassLoader.
Some more notes on your code
Usage by extension anti-pattern
I wouldn't extend JFrame just to display a window on the screen. Usage by extension is an anti-pattern. You don't need to extend JFrame to get a JFrame displayed on the screen and do what you want.
Referring static members
I would refer to constants via their original declaration. I.e. I'd refer to EXIT_ON_CLOSE via WindowConstants.EXIT_ON_CLOSE, not JFrame.EXIT_ON_CLOSE.
Typo
You have a typo in your UIManager.setLookAndFeel() code. Search for swing and you will see the typo.
Exception information
You might actually want to print the exception to stderr using exc.printStackTrace() instead of ignoring it completely, because when you have a typo in the LaF class name, as you do, and you don't print the exception, you might actually not come to know what's going wrong.
Sequence of widget construction and UIManager.setLookAndFeel()
The sequence of UIManager.setLookAndFeel() and the effective new JFrame() via super(...) does not guarantee you that the whole UI will be in Nimbus, parts of it might still be in Metal. I recommend to set the LaF before even constructing the first widget, to be on the safe side. As far as I remember, it's not guaranteed that changing the LaF after component construction has an effect, unless you tell the UIManager to update the LaF. See also this quote from the documentation of UIManager:
Once the look and feel has been changed it is imperative to invoke updateUI on all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component) makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.
http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html
setSize() vs. pack() with a little help of Insets and Border
Instead of setting the size manually, you might want to play with Insets or Border and JFrame.pack() in order to get a decent layout of your window. Setting the size manually assumes that you know a lot about the screen resolution and the font size of the user.
The pack() method performs automatic size calculation based on the contents. Insets and Border allow you to create some space and borders, even with some designs or labels, around components so they wouldn't be cramped tightly in a window but be nicely spaced.
First you have to assign an actionListener to the ReFresh Jbutton.
You can either implement the interface ActionListener to the class, and override the actionPerformed() method like this
public class View extends JFrame implements ActionListener{
private JButton ReFresh;
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
ReFresh = new JButton("ReFresh");
ReFresh.addActionListener(this);
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() { //right way for nimbus: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.equals(ReFresh))
{
super.repaint();
}
}}
public static void main(String[] args) {
View pdf = new View();
}
Or you can do inline assignment to addActionListener, like this
ReFresh.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
super.repaint();
}
});
You can try these methods to refresh/reload the JFrame,
invalidate();
validate();
repaint();
you can also use dispose(); and then new View(); to create the new JFrame, but in this sequence it will close the window and create new one.
or you can even try setVisible(false); then setVisible(true);
I recommend the first 3.