Importing button and text field from another class to main class - java

I have two different classes (mainClass) and (visual). In the visual class I have a method and inside the method I put the required code for a simple JButton. In the main class I create an object to call the method from visual class, to show the button in the main class . But it does not work. I would appreciate any advice.
package init;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
public class mainClass {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainClass window = new mainClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mainClass() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
In the code below I made an object and called the method from the visual class
visual bt = new visual();
bt.btn();
}
}
////////////////VISUAL CLASS//////////////////////
package init;
import javax.swing.JButton;
import javax.swing.JFrame;
public class visual {
public JFrame frame;
public void btn() {
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(141, 155, 151, 45);
frame.getContentPane().add(btnNewButton);
}
}

For what you are trying to achieve here you don't need 2 classes. You can do it like this:
import javax.swing.*;
import java.awt.*;
public class MainClass {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("New button");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(btnNewButton);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Related

Java Swing Class Objects

Question is about the object cont in mainWindow, I cant use it!
I got a Package (View) containing mainWindow and main. In my main I got the code:
package View;
import java.awt.EventQueue;
import Controller.mainController;
public class main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainWindow view = new mainWindow();
mainController cont = new mainController(view);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
in my mainWindow, I got:
public class mainWindow {
public mainWindow() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.control);
frame.setBounds(100, 100, 728, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton ButtonStartGame = new JButton("Start Game");
ButtonStartGame.setToolTipText("Start the game");
ButtonStartGame.setEnabled(false);
ButtonStartGame.setBounds(12, 189, 117, 25);
frame.getContentPane().add(ButtonStartGame);
ButtonStartGame.addActionListener(e->cont.StartGame());
frame.setVisible(true);
}
}

Get(), set() methods make JButton and JTable can not be displayed on JFrame?

I organized my example Project to practice MVC pattern. Model package I stored class "Student", model view I stored a JFrame called "ExampleView", controller I stored a class "ExampleController".
In JFrame "ExampleView", I have a button(btnGetAllInfo) and a table(tblGetAll).
This is my source code for "ExampleView"
package View;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTable;
public class ExampleView extends JFrame {
private JPanel contentPane;
private JTable tblGetAllStudentInfo;
private JButton btnGetAllInformation;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ExampleView frame = new ExampleView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ExampleView() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(this.contentPane);
this.contentPane.setLayout(null);
this.btnGetAllInformation = new JButton("Get all information");
this.btnGetAllInformation.setBounds(159, 11, 128, 23);
this.contentPane.add(btnGetAllInformation);
this.tblGetAllStudentInfo = new JTable();
this.tblGetAllStudentInfo.setBounds(24, 52, 384, 180);
this.contentPane.add(this.tblGetAllStudentInfo);
}
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
public JTable getTblGetAllStudentInfo() {
return tblGetAllStudentInfo;
}
public void setTblGetAllStudentInfo(JTable tblGetAllStudentInfo) {
this.tblGetAllStudentInfo = tblGetAllStudentInfo;
}
public JButton getBtnGetAllInformation() {
return btnGetAllInformation;
}
public void setBtnGetAllInformation(JButton btnGetAllInformation) {
this.btnGetAllInformation = btnGetAllInformation;
}
}
When I run JFrame "ExampleView" with no getBtnGetAllInfo() and getTblGetAllStudentInfo() methods, I got this below result
And corresponding result with getBtnGetAllInfo() and getTblGetAllStudentInfo() methods, I got this below result
Can anyone explain to me why when I have get(), set() methods for button and table like I do above, my button and table do not be shown?

java open a frame from another class

So I am trying to just make it so when I click a button it will open a frame from another class my filechooser frame just won't open.
This is the class called mainFrame:
package javatut;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class mainFrame {
private JFrame frmMainwindow;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainFrame window = new mainFrame();
window.frmMainwindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMainwindow = new JFrame();
frmMainwindow.setTitle("Mainwindow");
frmMainwindow.setBounds(100, 100, 280, 150);
frmMainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMainwindow.getContentPane().setLayout(null);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
myButtonAction();
}
});
btnBrowse.setBounds(155, 11, 100, 23);
frmMainwindow.getContentPane().add(btnBrowse);
textField = new JTextField();
textField.setBounds(10, 12, 135, 20);
frmMainwindow.getContentPane().add(textField);
textField.setColumns(10);
}
private void myButtonAction(){
Toolkit.getDefaultToolkit().beep();
}
}
And this is the FileChooser class called simply frame:
package javatut;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class frame {
private JFrame frmFilechooser;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame window = new frame();
window.frmFilechooser.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public frame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFilechooser = new JFrame();
frmFilechooser.setTitle("FileChooser");
frmFilechooser.setBounds(100, 100, 470, 350);
frmFilechooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFilechooser.getContentPane().setLayout(new BorderLayout(0, 0));
JFileChooser fileChooser = new JFileChooser();
frmFilechooser.getContentPane().add(fileChooser);
}
}
The easiest and most logical solution is to change the myButtonAction() method to:
private void myButtonAction() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(frmMainwindow);
if (result==JFileChooser.APPROVE_OPTION) {
// do something with the chosen file ..
}
}
If I understand you correctly, you're just missing two lines. The first in mainFrame's myButtonAction:
private void myButtonAction(){
// Toolkit.getDefaultToolkit().beep();
new frame();
}
And the other in frame's initialize:
private void initialize() {
frmFilechooser = new JFrame();
frmFilechooser.setTitle("FileChooser");
frmFilechooser.setBounds(100, 100, 470, 350);
frmFilechooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFilechooser.getContentPane().setLayout(new BorderLayout(0, 0));
JFileChooser fileChooser = new JFileChooser();
frmFilechooser.getContentPane().add(fileChooser);
frmFilechooser.setVisible(true);
}
(I just added the setVisible line at the bottom)

Opening a different frame with a button in Java

I am trying to open the menu frame using a button on the main frame. I added an event to the button and I tried calling the other class but it keeps giving me an error of ":: expected after this token"
This is my main frame
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
public static JPanel mainPane;
public final JButton menuButton = new JButton("New button");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
mainPane = new JPanel();
mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(mainPane);
mainPane.setLayout(null);
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu.main(String[] args);
}
});
menuButton.setBounds(76, 89, 104, 32);
mainPane.add(menuButton);
}
}
And this is my menu frame
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Menu extends JFrame {
public static JPanel menuPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu frame = new Menu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Menu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
menuPane = new JPanel();
menuPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(menuPane);
menuPane.setLayout(null);
JLabel menuTitle = new JLabel("Menu");
menuTitle.setBounds(194, 11, 46, 14);
menuPane.add(menuTitle);
}
}
change your action event to this.no need to call main method .create a new instance of Menu class instead.
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu menu = new Menu();
menu.setVisible(true);
}
});
if you relly want to call main method then use
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu.main(new String[0]);
}
});
the error is here
Menu.main(String[] args);//error
this is not a correct way of passing arguments to a methods.this is declaration of parameter list.
you can correct error by changing it to ,
String args[] = null;
Menu.main(args); //correct

Java Swing processing status

I am trying to implement a swing frame. In this, I want to display a processing status in a textPanel using a different thread while performing the needed task. I tried the following code. Of course there is something wrong with the logic. Please provide me with the proper approach
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SampleSwing {
private JFrame frame;
public static JTextField textField;
public static boolean processing=false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SampleSwing window = new SampleSwing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SampleSwing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(0, 31, 434, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
processing=true;
Processingstatus ps=new Processingstatus();
ps.start();
/*perform the actual task*/
processing=false;
}
});
btnNewButton.setBounds(174, 74, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}
class Processingstatus extends Thread{
public void run() {
try {
while(SampleSwing.processing) {
SampleSwing.textField.setText("Processing");
Thread.sleep(1000);
SampleSwing.textField.setText("Processing..");
Thread.sleep(1000);
SampleSwing.textField.setText("Processing...");
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
First I thought, "you should be using a SwingWorker, as it has methods to handle progress and EDT updates..."
But when I looked closer, you don't actually really care about the process itself, you just want some where to show that a process is running...They are two separate entities, that are only related because one (the UI updates) will run so long as the other is running.
So, instead, I used a javax.swing.Timer. This allows me to schedule an event to occur every n milliseconds and have that triggered in the EDT, nice and clean...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.Timer;
public class SampleSwing {
private JFrame frame;
public static JTextField textField;
public static boolean processing = false;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SampleSwing window = new SampleSwing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SampleSwing() {
initialize();
}
private Timer processTimer;
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
textField = new JTextField(25);
frame.add(textField, gbc);
processTimer = new Timer(500, new ActionListener() {
private StringBuilder dots = new StringBuilder(3);
#Override
public void actionPerformed(ActionEvent e) {
dots.append(".");
if (dots.length() > 3) {
dots.delete(0, dots.length());
}
textField.setText("Processing" + dots.toString());
}
});
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (!processing) {
processing = true;
processTimer.start();
} else {
processTimer.stop();
processing = false;
textField.setText(null);
}
}
});
frame.add(btnNewButton, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
ps For the reason why your original code didn't work, see my comment in the above comments section ;)

Categories

Resources