JTable Not Displaying from Window Builder Eclipse - 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 :)

Related

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();
}
}

JTextArea, JMenuBar, JMenu, JMenuItem not showing up

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.

JList update freezes display but not JFrame setTitle

If I update a JList with a long number of html formatted items then the controls stop responding and indicators won't update. This makes sense, the event thread is busy. The title can still be set though. Why is this?
Here's some (long) code demonstrating this:
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class JListTest extends JFrame {
class TestListModel extends AbstractListModel<String> {
private static final long serialVersionUID = JListTest.serialVersionUID;
private boolean useHtml;
private String[] formattedList = new String[] {};
public int getSize() {
return formattedList.length;
}
public String getElementAt(int index) {
return formattedList[index];
}
public void setUseHtml(boolean useHtml) {
this.useHtml = useHtml;
}
public String getNewListItem() {
if (useHtml) {
return "<html><div style='padding:2px"
+ ";background-color:#EDF5F4;color:black'><div style='padding:2px;font-weight:500;'>"
+ "Item " + (100 * Math.random())
+ "</div>"
+ "This will change!"
+ "</div></html>";
} else {
return "Item " + (100 * Math.random());
}
}
public void updateItems() {
formattedList = new String[] {"<html><h1>Loading!</h1></html>"};
fireContentsChanged(this, 0, 1);
Thread buildItems = new Thread() {
#Override
public void run() {
final String[] tempList = new String[3000];
for (int i=0; i<tempList.length; i++) {
tempList[i] = getNewListItem();
}
// Just show the string bashing's done
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
formattedList = new String[] {"<html><h1>Updating!</h1></html>"};
fireContentsChanged(TestListModel.this, 0, 1);
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update
SwingUtilities.invokeLater(new Runnable() {
public void run() {
formattedList = tempList;
fireContentsChanged(TestListModel.this, 0, formattedList.length);
}
});
}
};
buildItems.start();
}
}
protected static final long serialVersionUID = 1L;
public JListTest() {
JPanel controlPanel = new JPanel();
JButton updaterControl = new JButton("Add 3000");
final JCheckBox useHtmlControl = new JCheckBox("Use HTML");
final TestListModel model = new TestListModel();
JList<String> list = new JList<String>(model);
JScrollPane scrollPane = new JScrollPane(list);
final JLabel durationIndicator = new JLabel("0");
controlPanel.add(useHtmlControl, BorderLayout.WEST);
controlPanel.add(updaterControl, BorderLayout.EAST);
getContentPane().add(controlPanel, BorderLayout.PAGE_START);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(durationIndicator, BorderLayout.PAGE_END);
useHtmlControl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setUseHtml(useHtmlControl.isSelected());
}
});
useHtmlControl.setSelected(false);
updaterControl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.updateItems();
}
});
Timer counter = new Timer();
counter.schedule(new TimerTask() {
#Override
public void run() {
String previousCounter = durationIndicator.getText();
String newCounter = Integer.toString(
Integer.parseInt(previousCounter) + 1);
durationIndicator.setText(newCounter);
setTitle(newCounter);
}
}, 0, 100);
}
public static void main(String args[]) {
JListTest jlt = new JListTest();
jlt.pack();
jlt.setSize(300, 300);
jlt.setVisible( true );
}
}
The answer is pretty obvious - because Window title is not a Swing component, it's OS native entity.
So changes don't have to go through Swing Event Queue, but go to XDecoratedPeer.updateWMName directly in case of Unix and to some other class in other OSes.
The more interesting question would be how to avoid that UI blocking, but I don't think that's possible with just Swing, you'll have to implement some lazy loading, or rendering in batches.

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 can I get the column number in a SWT table in Eclipse RCP?

My question is How can we find the currently selected column number in the selected row of a SWT Table in Eclipse RCP?
Inside a Listener - e.g. for SWT.Selection - you can use viewer.getCell(...) as illustrated in the following example:
myTableViewer.getTable().addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
Point p = new Point(event.x, event.y);
ViewerCell cell = myTableViewer.getCell(p);
int columnIndex = cell.getColumnIndex();
//...
}
});
If you want to be sure that the columnindex is updated prior to calling the selectionlistener, then the mousedown event will also work fine:
table.addMouseListener( new MouseAdapter() {
private static final long serialVersionUID = 1L;
#Override
public void mouseDown(MouseEvent event) {
try {
Point p = new Point(event.x, event.y);
ViewerCell cell = viewer.getCell(p);
selectedColumn = cell.getColumnIndex();
} catch (Exception e) {
e.printStackTrace();
}
}
});