JTextArea, JMenuBar, JMenu, JMenuItem not showing up - jframe

I am quite new to Java so I need some help. I am trying to make a Notepad application.
The problem is that none of my menus or textfield is showing up. I cannot figure out what the problem is. Please help.
public class NotePad extends JFrame implements ActionListener {
private JTextArea txtArea;
private JMenuBar mnuBar;
private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp;
private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, clear, abtNotepad;
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Tekstboks
txtArea = new JTextArea();
//MenyBar
mnuBar = new JMenuBar();
//Meny
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
//UnderMeny
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
add(txtArea);
add(mnuBar);
add(mnyFile);
add(mnyFormat);
add(mnyEdit);
add(mnyHelp);
add(openFile);
add(saveFile);
add(exit);
add(textWrap);
add(noTextWrap);
add(clear);
add(abtNotepad);
setJMenuBar(mnuBar);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
public void actionPerformed(ActionEvent e) {
}
}

Your constructor should look something like:
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
txtArea = new JTextArea();
mnuBar = new JMenuBar();
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
mnuBar.add(mnyFile);
mnuBar.add(mnyFormat);
mnuBar.add(mnyEdit);
mnuBar.add(mnyHelp);
mnyFile.add(openFile);
mnyFile.add(saveFile);
mnyFile.add(exit);
mnyFormat.add(textWrap);
mnyFormat.add(noTextWrap);
mnyEdit.add(clear);
mnyHelp.add(abtNotepad);
setJMenuBar(mnuBar);
add(txtArea);
setVisible(true);
}
Otherwise you are overriding each component you add to BorderLayout.

Related

I set params but they still null

i wan't to create a chat so i have server and client; server code:
serverSocket = new ServerSocket(8080);
mainSocket = serverSocket.accept();
out = new PrintWriter(mainSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(mainSocket.getInputStream()));
gui = new JavaFXGUI();
gui.setIn(in);
gui.setOut(out);
gui.run()
client code:
clientSocket = new Socket("127.0.0.1", 8080);
out = new PrintWriter(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
gui = new JavaFXGUI();
gui.setIn(in);
gui.setOut(out);
gui.run()
JavaFXGUI :
public class JavaFXGUI extends Application{
private BufferedReader in;
private PrintWriter out;
private ChatController chatController;
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
URL xmlUrl = getClass().getResource("/GUI/scenes/mainScene.fxml");
loader.setLocation(xmlUrl);
System.out.println(in);
chatController = new ChatController(out,in);
loader.setController(chatController);
Parent root = loader.load();
primaryStage.setTitle("Chat");
primaryStage.setWidth(800);
primaryStage.setHeight(450);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public void run() {
launch();
}
public void setIn(BufferedReader in) {
this.in = in;
}
public void setOut(PrintWriter out) {
this.out = out;
}
}
so the problem is that in start method in and out parameters are null, why is this hapenning and how can i fix it. (i tried to pass in and out in constructor but it throws an error)

Opening MS Word file in eclipse

I am currently using eclipse Kepler as an ID for my development work.I would like to know how can i open MS word file and view same at eclipse ID. Thanks in advance.
You should use OleClientSite(...,File).
public class WordSample {
private Shell shell;
private OleFrame frame;
private OleClientSite site;
public WordSample() {
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(800, 600);
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem fileMenu = new MenuItem(bar, SWT.CASCADE);
fileMenu.setText("&File");
Menu menuFile = new Menu(fileMenu);
fileMenu.setMenu(menuFile);
MenuItem menuOpen = new MenuItem(menuFile, SWT.CASCADE);
menuOpen.setText("&Open");
menuOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
fileDialog.setFilterExtensions(new String[] {"*.doc"});
String doc = fileDialog.open();
if (doc != null && !doc.equals("")) {
openDocument(doc);
}
}
});
frame = new OleFrame(shell, SWT.NONE);
frame.setFileMenus(new MenuItem[] {fileMenu});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private void openDocument(String doc) {
if (site != null && !site.isDisposed()) site.dispose();
site = new OleClientSite(frame, SWT.NONE, "Word.Document", new
File(doc));
site.doVerb(OLE.OLEIVERB_SHOW);
}
public static void main(String[] args) {
WordSample sample = new WordSample();
}
}

JTable Not Displaying from Window Builder Eclipse

I've just started to use the Eclipse Window Builder plugin to create a JFrame for my program but when adding a JTable it's just not showing up. The program acts like it's there but won't show it.
public class Stock extends JFrame {
private static final long serialVersionUID = -4904110593143929972L;
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Stock frame = new Stock();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Stock() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Add");
menuBar.add(mnNewMenu);
JMenu mnNewMenu_1 = new JMenu("Delete");
menuBar.add(mnNewMenu_1);
JMenu mnNewMenu_2 = new JMenu("Edit");
menuBar.add(mnNewMenu_2);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
table.setBounds(10, 230, 414, -221);
contentPane.add(table);
loadTable();
}
public void loadTable() {
String[] columnNames = {"Key",
"Name",
"Quantity",
"Price per unit"};
Object[][] data = {
{"1","Name","10","�2.40"}};
table = new JTable(data, columnNames);
contentPane.add(table);
}
}
Like I said, I've only just started with Window Builder so it's probably just simple but I can't figure it out.
Sorry the issue was this line, "
contentPane.setLayout(null);" no idea how it got there. Ah well least its working :)

Trying to set the size/location of a button in a tab for SWT

This should be pretty simple but this is the first time I've worked with SWT. This is what I have so far.
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display){
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open ();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder){
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Button okButton = new Button(folder, SWT.PUSH);
okButton.setText("OK");
okButton.setSize(10,10);
NFL.setControl(okButton);
CTabItem NBA = new CTabItem(folder,SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder,SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800,500);
}
public static void main (String [] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}
What this currently gives me is this....
How would I make this a small button in the bottom right corner? Or just in general make it smaller and move it somewhere.
Since you are using a FillLayout the control takes up the entire space available. What you need is a different kind of a layout. I will suggest you to read this article, it will be a good start.
I generally prefer GridLayout as it is quite easy to use and it fulfills most needs.
Edited: Modifying your code to use GridLayout
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display) {
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new GridLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder) {
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Composite nflParent = new Composite(folder, SWT.NONE);
nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
nflParent.setLayout(new GridLayout());
Button okButton = new Button(nflParent, SWT.PUSH);
okButton.setText("OK");
GridData gd = new GridData();
gd.verticalAlignment = GridData.END;
gd.horizontalAlignment = GridData.END;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
okButton.setLayoutData(gd);
NFL.setControl(nflParent);
CTabItem NBA = new CTabItem(folder, SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder, SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800, 500);
}
public static void main(String[] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}

how to execute the .cs file generated using codeDom

Here is my source code.....
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
namespace CodeDOM
{
class Program
{
private string m_strFileName;
private string m_Suffix = ".cs";
public Program(string FileName)
{
m_strFileName = FileName;
}
public void CreateCodeDom()
{
Stream codeFile = File.Open("c:\\" + m_strFileName + m_Suffix, FileMode.Create);
StreamWriter sw = new StreamWriter(codeFile);
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeGenerator codeGenerator = cscp.CreateGenerator(sw);
CodeGeneratorOptions cgo = new CodeGeneratorOptions();
CodeSnippetCompileUnit cscu = new CodeSnippetCompileUnit("imports System");
codeGenerator.GenerateCodeFromCompileUnit(cscu, sw, cgo);
CodeNamespace cnsCodeDom = new CodeNamespace("SampleNamespace");
CodeTypeDeclaration clsDecl = new CodeTypeDeclaration();
clsDecl.Name = "Sample";
clsDecl.IsClass = true;
clsDecl.TypeAttributes = TypeAttributes.Public;
cnsCodeDom.Types.Add(clsDecl);
CodeConstructor clsConstructor = new CodeConstructor();
clsConstructor.Attributes = MemberAttributes.Public;
clsDecl.Members.Add(clsConstructor);
CodeMemberField clsMember = new CodeMemberField();
clsMember.Name = "myname";
clsMember.Attributes = MemberAttributes.Private;
clsMember.Type = new CodeTypeReference("System.String");
clsDecl.Members.Add(clsMember);
CodeMemberProperty property = new CodeMemberProperty();
property.Name = "MyName";
property.Type = new CodeTypeReference("System.String");
property.Attributes = MemberAttributes.Public;
property.HasGet = true;
property.GetStatements.Add(new CodeSnippetExpression("return this.myname"));
property.HasSet = true;
property.SetStatements.Add(new CodeSnippetExpression("this.myname=value"));
//property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "this.myname")));
//property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "this.myname"), new CodePropertySetValueReferenceExpression()));
clsDecl.Members.Add(property);
CodeMemberMethod mtd = new CodeMemberMethod();
mtd.Name = "Print";
mtd.ReturnType = null;
mtd.Attributes = MemberAttributes.Public;
mtd.Statements.Add(new CodeSnippetStatement("Console.WriteLine(myname)"));
clsDecl.Members.Add(mtd);
codeGenerator.GenerateCodeFromNamespace(cnsCodeDom, sw, cgo);
sw.Close();
codeFile.Close();
}
static void Main(string[] args)
{
Program m_obj = new Program("Sample");
m_obj.CreateCodeDom();
Console.ReadLine();
}
}
}
from this i got sample.cs file.now i want to execute that sample.cs file.wil u plz suggest me how to do it?
If it is an application, in a single source file, you can simply do:
csc sample.cs && sample.exe