Here's and example code where the code i'm executing runs multiple times.
First class:
public class Test2 extends JFrame {
public static int asd = 0;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnShowtest = new JButton("ShowTest2");
btnShowtest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Test1 a1 = new Test1();
a1.setVisible(true);
dispose();
}
});
contentPane.add(btnShowtest, BorderLayout.CENTER);
}
}
When button is pressed this one opens:
public class Test1 extends JFrame {
public static int ab = 1;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test1() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
contentPane.add(btnrun, BorderLayout.CENTER);
}
}
When button is pressed it executes this code:
public class Test3 {
public static void error(){
JOptionPane.showMessageDialog(null, "error");
Test2.asd += 1;
System.err.print(Test2.asd);
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
}
}
Now every time I press the buttons again it executes Test3.error(); multiple times.
For example if I press the buttons 10 times then Test3.error(); runs 10 times.
I'm guessing it's a simple fix but I can't figure it out.
Edit:
When i press the btnrun button it runs the "Test3.error()" Code, then closes all frames and creates another main frame.
When i get back to btnrun and press it again, it runs "Test3.error()" 2 times instead of once. 2 JOptionePanes and creates new mainframe twice.
If i do it again it runs "Test3.error()" 3 times and so on it keeps doing that.
What i want is for it to always run "Test3.error()" just once but for some reason it doesn't.
Another example:
public class Frame1 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 frame = new Frame1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnRun = new JButton("Run");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Run.run();
}
});
contentPane.add(btnRun, BorderLayout.CENTER);
}
}
And the code that runs:
public class Run {
public static void run(){
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Frame1 newtet = new Frame1();
newtet.setVisible(true);
}
}
}
Same problem.
Everytime i press the run button, it executes the code as many times as i've pressed it in the past.
Press button once it diposes Frame1 and recreates Frame1.
Press it again and it executes the code 2 times in a row.
(disposes Frame1, creates Frame1, diposes Frame1 and creates Frame1)
Yes, you've added an action listener here:
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
That means "When the button is clicked, call Test3.error()." Were you expecting the action listener to be removed automatically after executing once?
EDIT: Okay, I think I've worked out what's going on. This is the problem:
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
For every existing frame, you're disposing that frame and creating a new Test2(). So if you had 3 frames on the screen (a Test1, a Test2 and a Test3) when you click the button, you'll end up with three new Test2 frames. I suspect you didn't mean to do that. Did you mean this instead?
for (Frame f : Frame.getFrames()){
f.dispose();
}
Test2 newtet = new Test2();
newtet.setVisible(true);
Related
I'm new to Swing and I'm having trouble replacing an existing JFrame. I initialize the first JFrame without a problem.
class GererAdgerent :
public class GererAdherent extends JFrame {
private JPanel contentPane;
static GererAdherent frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new GererAdherent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GererAdherent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAjouterAdherent = new JButton("Ajouter Adherent");
btnAjouterAdherent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
}
});
btnAjouterAdherent.setBounds(104, 34, 130, 23);
contentPane.add(btnAjouterAdherent);
}
}
But once I try to initialize a different JFRame, I get a blank JFrame without all of it's components (It is created properly when I use AjouterAdherent's main to initialize)
class AjouterAdherent :
public class AjouterAdherent extends JFrame {
JFrame frame;
JTextField txtNom;
static Properties p=new Properties();
static BibliothequeDAORemote proxy;
public static void main(String[] args) throws NamingException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AjouterAdherent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.menu);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane txtpnAjouterClient = new JTextPane();
txtpnAjouterClient.setFont(new Font("Tahoma", Font.BOLD, 11));
txtpnAjouterClient.setBackground(SystemColor.menu);
txtpnAjouterClient.setEnabled(false);
txtpnAjouterClient.setEditable(false);
txtpnAjouterClient.setForeground(Color.black);
txtpnAjouterClient.setBounds(175, 11, 89, 20);
txtpnAjouterClient.setText("Ajouter Client");
frame.getContentPane().add(txtpnAjouterClient);
JTextPane txtpnNom = new JTextPane();
txtpnNom.setBackground(SystemColor.menu);
txtpnNom.setEnabled(false);
txtpnNom.setEditable(false);
txtpnNom.setForeground(Color.black);
txtpnNom.setBounds(36, 49, 72, 20);
txtpnNom.setText("Nom");
frame.getContentPane().add(txtpnNom);
}
}
Any help would be greatly appreciated!
(It is created properly when I use AjouterAdherent's main to initialize)
So look at the code that works properly:
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
I try to initialize a different JFRame, I get a blank JFrame
And look at the code that doesn't work:
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
What is the difference?
The problem is your class extends JFrame and also creates a new JFrame, so you have two frame instances, one with components and one without.
Don't extend JFrame! Then you won't cause confusion.
You should only extend a class when you add new functionality to the class. Adding components to the frame is not adding new functionality.
i want to write any text or key from swing onscreen keyboard to any caret location like browser notepad office word. i get some example layout, but dont know how to work to system caret. please teach me how it work.
this example code
public class keyboard extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
keyboard frame = new keyboard();
frame.setVisible(true);
frame.setAlwaysOnTop(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public keyboard() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(3, 3));
for(int i = 1; i <= 9; i++) {
createButton(Integer.toString(i));
}
pack();
}
private void createButton(String label)
{
JButton btn = new JButton(label);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
contentPane.add(btn);
}
public void actionPerformed(ActionEvent e)
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
}
});
}
}
I have created 2 JFrames and tying to link two JFrames Windows together. But i couldnt get the content in the 2nd Jframe. Can anyone help me with this?
My first Frame:
public class Main extends JFrame {
private JPanel contentPane;
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);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnOpenTheJung = new JButton("Open the JUNG window");
btnOpenTheJung.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
**JungLearning1 frame1 = new JungLearning1();
frame1.setVisible(true);
frame1.setSize(600, 400);**
}
});
btnOpenTheJung.setBounds(172, 99, 145, 34);
contentPane.add(btnOpenTheJung);
}}
My second Frame:
public class JungLearning1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DirectedSparseGraph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("Vertex1");
g.addVertex("Vertex2");
g.addVertex("Vertex3");
g.addEdge("Edge1", "Vertex1", "Vertex2");
g.addEdge("Edge2", "Vertex1", "Vertex3");
g.addEdge("Edge3", "Vertex3", "Vertex1");
VisualizationImageServer<String, String> vs = new VisualizationImageServer<String, String>(
new CircleLayout<String, String>(g), new Dimension(
200, 200));
**JFrame frame1 = new JFrame();
frame1.getContentPane().add(vs);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);**
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JungLearning1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}}
I want to display the 2nd frame along with the content when clicking the butten from the first window
I have a simple gui, that is suppose to shows table(JTable). How to make it switch to table1(JTable) after x amount of seconds, and then back to table after x amount of seconds and have that run in a loop. I know setting the other table is nothing more than putting this line in:
scrollPane.setViewportView(table1);
but how to switch from one to the other and back periodically. Guess I have to use the Timer but need some help with this one.thanks
Here is the simple example code:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
}
}
THE FINAL RESULT:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
private Timer timer;
private Timer timer1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
ActionListener action = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer.stop();
scrollPane.setViewportView(table1);
timer1.start();
}
};
ActionListener action1 = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer1.stop();
scrollPane.setViewportView(table);
timer.start();
}
};
timer = new Timer(3000, action);
timer.start();
timer1 = new Timer(3000, action1);
}
}
here is the working example of what i wanted.thanks everyone for help
I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.
public class Home
{
private JFrame frame;
JInternalFrame internalFrame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Home window = new Home();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Home()
{
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);
JLabel title1 = new JLabel("City of Murphy");
JLabel title2 = new JLabel("Traffic Ticket Input System");
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
JButton payTicketButton = new JButton("Make a Payment");
JButton reportButtton = new JButton("Ticket Report");
JButton exitButton = new JButton("Exit");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}
second class (the screen i want to open upon newticket button being pressed
public class NewTicketWindow extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NewTicketWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
}
just add these lines into your action performed code -
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
The ActionListener of newTicketButton should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main of NewTicketWindow):
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
NewTicketWindow newTicketWindow = new NewTicketWindow();
newTicketWindow.setVisible(true);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also you need to add the newTicketButton to the home window:
frame.add(newTicketButton);