Reputation: 11
I want to show a console for log or probably anyone here can suggest me the references of creating log via GUI? I use Java on Netbeans
If I choose a window GUI application from running a built jar file, I can't see the log or console running, too
Upvotes: 1
Views: 2332
Reputation: 84
The best way to have application with console as well as GUI is to create a command line application and then running an instance of JFrame ( or which ever other GUI library you want to use ) from the console application.
Following is code example with console and Jframe UI
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ConsoleJFrameCombo extends JFrame {
// JPanel
JPanel pnlButton = new JPanel();
// Buttons
JButton btnAddFlight = new JButton("Click me");
public ConsoleJFrameCombo() {
initUI();
}
private void initUI() {
btnAddFlight.setBounds(60, 400, 220, 30);
// JPanel bounds
pnlButton.setBounds(800, 800, 200, 100);
// Adding to JFrame
pnlButton.add(btnAddFlight);
add(pnlButton);
btnAddFlight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("OUtput to console");
}
});
setTitle("Simple example");
setSize(300, 200);
setLocationRelativeTo(null);
//chnge to exit on close to exit program.
setDefaultCloseOperation(HIDE_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
ConsoleJFrameCombo ex = new ConsoleJFrameCombo();
ex.setVisible(true);
System.out.println("Press Enter to exit");
System.in.read();
System.out.println("Exiting....");
System.exit(0);
}
}
Upvotes: 0
Reputation: 48307
Yes you can, ypu jar is a runnable, but ypu can still run it from the Console...
open a terminal, navigate to the jar path and run it by calling "java -jar yourJar.jar"
... and all the system.out and system.err will be displayed there...
Upvotes: 1