Skip to content

Instantly share code, notes, and snippets.

@Anticom
Created April 22, 2013 11:50
Show Gist options
  • Save Anticom/5434266 to your computer and use it in GitHub Desktop.
Save Anticom/5434266 to your computer and use it in GitHub Desktop.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class TestFrame extends JFrame implements ActionListener {
private JPanel contentPane;
private JButton btnOpenFile;
private JScrollPane scrollPane;
private JTextArea textArea;
private JFileChooser fc;
private JMenuBar menuBar;
private JMenu mnFile;
private JMenu mnSettings;
private JMenu mnPort;
private JMenu mnHelp;
private JMenuItem mntmAbout;
private JButton btnAddPort;
private JButton btnRemovePort;
private JButton btnClearPorts;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestFrame() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mnSettings = new JMenu("Settings");
menuBar.add(mnSettings);
mnPort = new JMenu("Port");
mnSettings.add(mnPort);
mnHelp = new JMenu("?");
menuBar.add(mnHelp);
mntmAbout = new JMenuItem("About");
mnHelp.add(mntmAbout);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane);
JPanel pOpen = new JPanel();
tabbedPane.addTab("Open", null, pOpen, null);
pOpen.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
fc = new JFileChooser();
btnOpenFile = new JButton("Open File...");
btnOpenFile.addActionListener(this);
pOpen.add(this.btnOpenFile);
btnAddPort = new JButton("Add Port");
btnAddPort.addActionListener(this);
pOpen.add(btnAddPort);
btnRemovePort = new JButton("RemovePort");
btnRemovePort.addActionListener(this);
pOpen.add(btnRemovePort);
btnClearPorts = new JButton("Clear Ports");
btnClearPorts.addActionListener(this);
pOpen.add(btnClearPorts);
this.textArea = new JTextArea();
this.textArea.setEditable(false);
this.scrollPane = new JScrollPane(this.textArea);
tabbedPane.addTab("View", null, scrollPane, null);
}
private static String readFile(File file) throws IOException {
FileInputStream stream = new FileInputStream(file);
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
} finally {
stream.close();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// open file:
if (e.getSource() == btnOpenFile) {
int returnVal = fc.showOpenDialog(this);
String content = "";
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
content = TestFrame.readFile(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
// nothing to do here...
}
textArea.setText(content);
textArea.setCaretPosition(0);
// add port
} else if (e.getSource() == btnAddPort) {
System.out.println("adding port");
mnPort.add(new JCheckBoxMenuItem("New Port " + mnPort.getComponentCount()));
// remove port
} else if (e.getSource() == btnRemovePort) {
System.out.println("removing port");
mnPort.remove((mnPort.getComponentCount() - 1));
// clear ports
} else if (e.getSource() == btnClearPorts) {
System.out.println("clearing ports");
mnPort.removeAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment