layout issue while changing java code - java

I'm currently trying to modify a java project TRMSim_WSN (Simulator) using Eclipse.
I changed some parts of the source code but the layout seems like it has changed even though I didn't touch it!
The original one:
After modifying:
Can you help me please?

There are two parts for this component:
The 1st one:
this.legendPanelContainer.add(this.legendPanel, null);
this.legendPanel.setBackground(Color.white);
this.legendPanelContainer.setPreferredSize(new Dimension(100, 98));
GroupLayout legendPanelContainerLayout = new GroupLayout(this.legendPanelContainer);
this.legendPanelContainer.setLayout(legendPanelContainerLayout);
legendPanelContainerLayout.setHorizontalGroup(legendPanelContainerLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 100, 32767));
legendPanelContainerLayout.setVerticalGroup(legendPanelContainerLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 81, 32767));
this.legendLabel.setText("Legend");
this.legendLabel.setPreferredSize(new Dimension(100, 15));
The 2nd one:
this.legendPanelContainer.add(this.legendPanel, null);
this.legendPanel.setBackground(Color.white);
this.legendPanel.setSize(this.legendPanelContainer.getSize());
this.legendPanel.plotLegend();
The problem is that I don't see any difference between the source code and the one I modified for this part!

Related

Where would my image file have to be located in order for the following code to work?

So I've been playing around with JButtons and I've been trying to add an ImageIcon to a JButton. I have the following code:
window = new JFrame("Test");
window.setSize(1000, 600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
Icon icon = new ImageIcon("/Apple.jpg");
JButton apple = new JButton(icon);
apple.setBounds(50, 50, 200, 200);
window.add(apple);
I was wondering where would the Apple.jpg file have to be located for the code to work? Currently, Apple.jpg is located in the same package as this class.
Looking at the source code for constructor ImageIcon(String), leads me to the conclusion that the string you pass is treated as is.
Hence, according to the code you posted in your question, i.e.
Icon icon = new ImageIcon("/Apple.jpg");
Java will search for file Apple.jpg in the root directory.
If you are running on a Windows machine, like I am, Java considers my root directory to be C:\ (on my machine) so it will search for this file: C:\Apple.jpg
The answer linked to in this comment to your question (from Gilbert le Blanc) details all the different ways to load an image. I just tried to answer your question. So using the code in your question, the answer to it is that you would have to place your file (Apple.jpg) in the root directory. I'm assuming that you know where that is on your computer. In any case, I couldn't find enough information in your question to help you with that. for example, I couldn't tell what platform you are on.

Adding maps at iText Java

I am using java and iText to create a pdf. Is it possible to add a map with a pointer on it so the user will know where the starting point is?
Clarification (from a meanwhile deleted answer by the OP which should have been an edit of the question)
my problem is can i add at iText google map from web and not as an image
Since you didn't answer my counter-question added in comment, I'm providing you two examples. If these are not what you're looking for, you really should clarify your question.
Example 1: add a custom shape as extra content on top of a map
This is demonstrated in the AddPointer example:
PdfContentByte canvas = writer.getDirectContent();
canvas.setColorStroke(BaseColor.RED);
canvas.setLineWidth(3);
canvas.moveTo(220, 330);
canvas.lineTo(240, 370);
canvas.arc(200, 350, 240, 390, 0, (float) 180);
canvas.lineTo(220, 330);
canvas.closePathStroke();
canvas.setColorFill(BaseColor.RED);
canvas.circle(220, 370, 10);
canvas.fill();
If we know the coordinates of the pointer, we can draw lines and curves that result in a the red pointer shown here (see the red pin near the Cambridge Innovation Center):
Example 2: add a line annotation on top of a map
This is demonstrated in the AddPointerAnnotation example:
Rectangle rect = new Rectangle(220, 350, 475, 595);
PdfAnnotation annotation = PdfAnnotation.createLine(writer, rect, "Cambridge Innovation Center", 225, 355, 470, 590);
PdfArray le = new PdfArray();
le.add(new PdfName("OpenArrow"));
le.add(new PdfName("None"));
annotation.setTitle("You are here:");
annotation.setColor(BaseColor.RED);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setBorderStyle(
new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
annotation.put(new PdfName("LE"), le);
annotation.put(new PdfName("IT"), new PdfName("LineArrow"));
writer.addAnnotation(annotation);
The result is an annotation (which isn't part of the real content, but part of an interactive layer on top of the real content):
It is interactive in the sense that extra info is shown when the user clicks the annotation:
Many other options are possible, but once again: your question wasn't entirely clear.

How do I add and display a JLabel at runtime?

I created frame with button and when it is pressed all content is removed and replaced by new one. But I can not display label, here is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
getContentPane().removeAll();
jLabel2 = new javax.swing.JLabel();
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Hello World!");
jLabel2.setLocation(80, 80);
jLabel2.setVisible(true);
getContentPane().add(jLabel2);
getContentPane().repaint();
pack();
}
What am I doing wrong? :(
instead of repaint() try validate().
Instead of trying to remove all and add new components, use a CardLayout, which will "layer" panels and let you navigate between them. See How to use CardLayout and you can see a simple example here
You can also see how to use CardLayout with Netbeans GUI Builder here
You should use validate() instead of repaint. The rest of your source looks fine.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

Why does my UnicodeFont (Slick2D) not draw?

So I am trying to draw a font using Slick2D's UnicodeFont. The code that loads the font:
Font font = new Font("Arial", Font.BOLD, 20);
UnicodeFont uFont = new UnicodeFont(font);
uFont.addAsciiGlyphs();
uFont.getEffects().add(new ColorEffect(java.awt.Color.BLACK));
uFont.loadGlyphs();
It's more or less copied and pasted from the wiki. Next, during a frame I use gluOrtho2d() and reset the projection and modelview matrices. Then I call
this.font.drawString(100, 50, "some text goes here");
to actually draw the text. But all I get is an empty screen, an no errors of any kind.
Considering that UnicodeFonts are depreciated in the current version of slick 2d, I recomend you folow my steps for using AngelCodeFonts on this related question: Slick2D Fonts don't work

Stretching out JScrollPane - Java

I have this structure:
<JFrame>
<JPanel backgroundcolor = "pink">
<JScrollPane>
<JTable>!!!Data here !!!</JTable>
</JScrollPane>
</JPanel>
</JFrame>
How do i stretch the ScrollPane it to cover the full window without using setSize?
This is how it looks like now:
alt text http://img22.imageshack.us/img22/8491/17747996.png
Thanks!
Mmmph! Nobody offered a simple solution such as using BorderLayout as layout manager for my JScrollpane container!
I am not familiar with the XML file format.
If it is coded, you may need to code something like this:
JScrollPane1 = new JScrollPane();
JPanel1.add(JscrollPane1);
JScrollPane1.setBounds(5,29,636,122);
JTable1 = new JTable();
JPanel1.add(JTable1);
JScrollPane1.setBounds(5,434,553,3097);
JScrollPane1.setViewportView(JTable1);
Use setPreferredScrollableViewportSize() and a suitable layout.
Edit: You'll also need setFillsViewportHeight(), as discussed in Adding a Table to a Container.

Categories