I have this class for my UI
public class MyFrame extends JFrame{
JTextArea textArea;
public MyFrame(){
setSize(100,100);
textArea = new JTextArea(50,50);
Container content = getContentPane();
content.add(textArea);
}
public static void main(String[] args){
JFrame frame = new MyFrame();
frame.show();
UpdateText u = new UpdateText();
u.settext("Helloworld");
}
}
And I have this another class that will set the text of textArea, in which I extended MyFrame to access textArea in another class.
public class UpdateText extends MyFrame{
public void settext(String msg){
textArea.setText(msg);
}
}
Then I instantiate UpdateText and call the function settext. but the text doesn't seem to appear in the GUI.
First of all, don't override the setText() method unless you want different behavior. Second of all, you don't have to extend anything. All you have to do is follow these simple steps and you'll be set!
In the UpdateText class, put these lines somewhere in it:
MyFrame gui;
public UpdateText(MyFrame in) {
gui = in;
}
In the 'MyFrame` class, put this line at the beginning:
UpdateText ut = new UpdateText(this);
Now, you can refer to everything in the MyFrame class from the UpdateText class by preceeding what you want to change with gui. For example, say you wanted to change the text of your textarea. The code would be the following:
gui.textArea.setText("Works!");
Happy coding! :)
Related
I am trying to implement mouse listener however I can not seem to get it to work. My code doesnt have any errors, but when I click on the frame I I can not get a message to print out. I have tried extending the class HandleClassOne to viewOne, but that also wouldn't work. Any thoughts?
The main class creates a frame and then creates an instance of viewOne on the frame.
public class main{
protected static JFrame window;
public static void main(String args[]){
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400,400);
window.setVisible(true);
new viewOne(window);
}
}
The viewOne class adds a panel and a label to a frame. It also adds a mouse listener to the panel.
public class viewOne {
private static JPanel panel1;
private static JLabel label1;
public viewOne(JFrame frame) {
panel1 = new JPanel();
label1 = new JLabel("View One");
panel1.add(label1);
frame.add(panel1);
panel1.setBackground(Color.red);
frame.validate();
}
public static void mouseAdd() {
HandleClassOne handle = new HandleClassOne();
panel1.addMouseListener(handle);
panel1.addMouseMotionListener(handle);
}
public static void main(String[] args) {
mouseAdd();
}
}
The HandleClassOne class should print out a message when the panel created in viewOne is clicked.
public class HandleClassOne extends main implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent e) {
System.out.println("mouse clicked");
}
}
While you have defined the function mouseAdd(...) I don't see you calling it.
Try (within the constructor)
public viewOne(JFrame frame) {
...
mouseAdd();
...
}
naturally, you'll need to do this after the panel1 is set.
Note that there are other issues, too
You don't invoke presenting the JFrame properly within your main function in your main class. Look up a basic tutorial on Java Swing, where it talks about the event dispatch thread and the requirements to not present within your program's main thread of execution.
You have an additional main function in your viewOne class, which is not how these things are wired up.
You added the mouseAdd() method (which is responsible for registering the mouse listener) inside the main method of viewOne class.
Please keep in mind that main method only gets called whenever you are running it as entry point class for your application. Here you have main class to act as an entry point.
You kept main method in viewOne class as well and it will get called only when you are running it as an individual piece (not along with main class).
To fix the issue here, keep your mouseAdd() method call inside viewOne() constructor as constructor gets called every time whenever object is getting created.
public viewOne(JFrame frame) {
panel1 = new JPanel();
label1 = new JLabel("View One");
panel1.add(label1);
frame.add(panel1);
panel1.setBackground(Color.red);
mouseAdd();
frame.validate();
}
so I got this in my Main class:
public class Main extends JFrame {
public static void main(String[] args) {
JFrame Launch = new JFrame();
Launch.setSize(800, 400);
Launch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Launch.setContentPane(new StartView());
Launch.setTitle("De Hartige Hap");
Launch.setVisible(true);
}
}
Now let's say I'm in that Panel ("StartView()")
and onClick on a button, I want to change the frames' contentpane..
How would I do this?
public class StartView extends javax.swing.JPanel {
public StartView() {
initComponents();
}
private void OrderButtonActionPerformed(java.awt.event.ActionEvent evt) {
/*instead of Launch.setContentPane(new StartView());
*it has to be (new otherView())
*/
}
Pass your Launch object to your panel object (i.e. new StartView(launch)). This way, you can create a method changeView() in Launch and you can call this method from your panel (launch.changeView()), you can change your view inside that method.
Also, if I may adivce you, take a look at the ModelViewController pattern. This makes sure you keep the View (your panels) and the Controller (your Frame) seperated so you don't get issues like this.
I've been searching and nothing has been simple enough for me. But for the thing I'm working on I want to make one JFrame in my main method and use different class files for each JPanel. (This is to keep the information separated and clean).
Also what's the best way to switch JPanels if I do it in this method?
public class Main extends JFrame {
public Main(){
JFrame intro = new JFrame("FormProgram");
intro.setSize(800,600);
intro.setVisible(true);
intro.setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
intro.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Main();
}
Then my basic second class looks like this.
public class Page1 extends JFrame implements ActionListener{
public JLabel test;
public void Page1(){
Container cp = intro.getContentPane();
cp.setLayout(null);
this.test = new JLabel("welcome");
this.test.setBounds(5,5,300,300);
cp.add(test);
}
what's the best way to switch JPanels
Use a CardLayout. Read the section from the Swing tutorial on How to Use a CardLayout for more information and a working example.
I have this piece of code:
public class GUI extends JFrame {
private PlaneUI planeui;
public GUI(PlaneUI planeui) {
this.planeui = planeui;
}
//We have put the code that creates the GUI inside a method
public GUI() {
start();
planeui.display();
} ...
This is just a test and I need the method "planeui.display" to work when the program starts, together with the method "start();" which already works.
public final class PlaneUI extends JFrame {
public void display() {
//Creates a new JPanel object
JPanel panelStart = new JPanel();
getContentPane().add(panelStart);
//Changing the default layout from Flowlayout to absolute
panelStart.setLayout(null);
setTitle("Reservationer"); //Sets the window title
setSize(236, 256); //Sets the default size of the window
setLocationRelativeTo(null); //Start location of the window (centered)
setDefaultCloseOperation(EXIT_ON_CLOSE); //Exits the window
}
}
I have imported the needed libraries and I feel like the problem lies in an object that isn't created correctly since I get a nullpointerexception. I tried running this planeUI class in the main method and it worked correctly. I just can't get it to work this way..
In function PlaneUI.display() add one last line setVisible(true) because your adding everything but not displaying anything
you have to add this into your display() method:
setVisible(true);
Otherwise, all you are doing is setting all the aspects of the JFrame and adding the JPanel to it. You have to make it visible afterwards.
In my Java course I have to create a GUI class that interacts with the user and a Logic class that handles the interaction. Since I find this very complicated and hard to understand, I'm looking for some help that can inspire me to continue.
Until now I have just used a text based Menu class with a Swich statement to handle simple input with Scanner and then handle all get and set methods. But I guess I don't need that anymore, and instead could create some Logic class to handle all get and set methods in objects depending on the input from the user. But to begin, how do I create a simple menu in a window and get input value from a GUI class to this Logic class and it's methods?
I add a simple GUI test class that I have done to start this task, but I'm afraid something is missing?
import javax.swing.*;
import java.awt.*;
class Guitest extends JFrame {
JTextField inputLine;
JLabel text;
Container contentPane;
// constructor
public Guitest() {
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
setTitle("Test GUI");
setSize(400,200);
setLocation(400,400);
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
text = new JLabel("Enter name of new customer");
contentPane.add(text);
inputLine = new JTextField();
inputLine.setColumns(10);
contentPane.add(inputLine);
setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}
And I also add a simple class that make an instance of the window and make it visible. Perhaps this class could be the Logic class?
class Showgui {
// main
public static void main(String[] args) {
Guitest mywindow;
mywindow = new Guitest();
mywindow.setVisible(true);
}
}
All help is preciated! Thanks!
You should study event listeners for a start and then learn about about the MVC pattern, shown here.
You can add an ActionListener to the JTextField so whenever the users presses enter an event is triggered and a piece of code is executed.
For example:
inputLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = inputLine.getText();
}
});