Violet_82 89 Posting Whiz in Training

The constructor for Insets is

Ah, I see, not sure where I got the wrong order of the parameters from. That explains everything.

Re your 5x5 grid... yes you can put your components into non-contiguous cells

But is there an explicit way to specify that a cell should be empty or do I simply not include its coordinates in the constructor?
Also, has anybody ever come up with some kind of GridBagLayout "box model" - a bit like the CSS box model - to clarify where internal padding and external padding are located? I mean a graphical representation of it, that would be awsome!

Violet_82 89 Posting Whiz in Training

Hello, since I have realised how little I have understood of the GridBagLayout, I thought I'd open a thread where I could clarify one by one all my doubts.
I have produced a very small and simple GUI which I will use to test the GridBagLayout and ask questions as they arise. The GUI has only a bunch of buttons at the moment but I will update it
as I go along.
So this is the code:

/*TestingGridBag.java*/
import java.awt.GridBagLayout;//chosen JFrame layout
import static java.awt.GridBagConstraints.*;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;//for radio buttons

public class TestingGridBag extends JFrame{
private GridBagLayout layout;
//private JPanel panel;
private JButton okButton;
private JButton cancelButton;
private JButton submitButton;
private JButton test1Button;
private JButton test2Button;

public TestingGridBag(){
super("GridBagLayout test");
layout = new GridBagLayout();
setLayout( layout );//set layout of JFrame

//panel = new JPanel();
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
submitButton = new JButton("Submit");
test1Button = new JButton("test1");
test2Button = new JButton("test2");

add( okButton, new GridBagConstraints( 0, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, 0 ) );
add( cancelButton, new GridBagConstraints( 1, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, 0 ) );
add( submitButton, new GridBagConstraints( 2, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, 0 ) );
add( test1Button, new GridBagConstraints( 3, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, …
Violet_82 89 Posting Whiz in Training

hi thanks, yes I know that will work (that's the way I would do it anyway) but I thought I'd experiment with the object literals :-). So you think that it doesn't work because it doesn't sit inside a document ready and therefore it tries to add the click handler before the DOM is ready? In which case le tme ask you, do you think that if I had the script in the HTML file at the end of the body tag, it would work?

Violet_82 89 Posting Whiz in Training

HI I am looking into object literals, a topi that I am afraid I really don't know much about. Anyway, I thought I'd try something but I didn't get it to work and I am not sure what I am doing wrong.
I have an HTML page:

<html>
    <head>
        <title>This is a test</title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
        <div class="container">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse bibendum turpis neque, et pretium arcu euismod eu. Etiam vulputate vitae nunc sit amet laoreet. Sed euismod justo sit amet interdum tincidunt. Donec pulvinar nec justo quis aliquet. Suspendisse at orci sed ante varius vehicula non vel enim. Donec posuere sollicitudin tristique. Aliquam imperdiet erat ultrices odio lobortis viverra. Integer id erat vitae quam elementum elementum non vitae leo. Nam velit enim, cursus id urna vitae, adipiscing congue odio. Nullam eleifend tristique rhoncus. In vel neque a sem mollis convallis. Mauris molestie est sit amet dui imperdiet, a pharetra nibh scelerisque. Nam massa purus, vulputate a purus sed, bibendum porttitor neque.</p>
            <p>Proin adipiscing, ipsum eget malesuada sodales, mi purus sollicitudin ligula, sed ultrices massa ipsum id sem. Nulla eu laoreet dui. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras sit amet imperdiet turpis, sit amet rutrum justo. Nunc ultricies, enim eget consectetur laoreet, lorem ipsum posuere nulla, in lacinia lorem risus et ipsum. Nulla eros turpis, faucibus sit amet pharetra eu, pretium eu libero. Nam …
Violet_82 89 Posting Whiz in Training

sorry I don't mean to be thick, so they both need to be there then. Clearly I didn't understand that, sorry. OK it compiles now, but the layout reverted back to what I had a while ago, so it's ignoring the positioning.
I will go back and dig here and there trying to find the reason why.
59c2dc03d3c9210bcf311e644b25d3f2

Violet_82 89 Posting Whiz in Training

OK, thanks scudzilla, this thing is making me sweat!
Right, so the ipadx and ipady parameters were missing. I have added them in. ABout the import, just so I get it completely right: I now have

import static java.awt.GridBagConstraints.*;
import java.awt.Insets;

correct? (I have also tried this combination just in case

import static java.awt.GridBagConstraints;
import java.awt.Insets;

)
If I compile, I now only get 20 errors, still to do with new GridBagConstraints (that's where the caret is) saying:

Form.java:134: error: cannot find symbol
                selectionPanel.add( selectionRadioButton, new GridBagConstraints( 3, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, 0 ) );//add component to jpanel and constraints

Code's here:

/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import static java.awt.GridBagConstraints.*;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
//label
private JLabel printerLabel;
private JLabel printQualityLabel;

//txt areas
private JTextArea textArea1;
private JTextArea textArea2;
private JTextArea textArea3;

//checkboxes
private JCheckBox imageCheckBox;
private JCheckBox textCheckBox;
private JCheckBox codeCheckBox;
private JCheckBox printCheckBox;

//radio buttons
private JRadioButton selectionRadioButton;
private JRadioButton allRadioButton;
private JRadioButton appletRadioButton;
private ButtonGroup radioGroup;//to create logical connection between radio buttons

//jcombobox
private JComboBox<String> qualityCombo;
private String[] values;

//buttons
private JButton okButton;
private JButton cancelButton;
private JButton setupButton;
private JButton helpButton;

//panels
private JPanel selectionPanel;//contains …
Violet_82 89 Posting Whiz in Training

Oh, apologies. So an import can be static?! And what does it mean?
I have made the changes and I have less errors now (42) and the compiler seems to have a problem only with new GridBagLayout and with new Insets(). Now the constants are fine.

Violet_82 89 Posting Whiz in Training

OK, that didn't work. I have made the change you suggested and also I have done the same thing for JPanels, but I still get a lot of error. So here is the code - I used pastebin this time, maybe it's more readable http://pastebin.com/PBrYE8aM and I have attached an excerpt of the errors. Still the same thing, it picks on the constants for whatever reason
b6bb17d99ffbd2dbfaa92891d89954cb

Violet_82 89 Posting Whiz in Training

OK, thanks. I am halfway but the compiler is complaining quite a lot about "CENTER", "NONE", and "new Insets". Is there any other import declaration that I need to include?

I have changed the code to what you have suggested, it is much shorter now, but I have done that just where the components attach to the various JPanels. Do I have to do the same thing when I add the JPanels to the JFrame, correct?

import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
//label
private JLabel printerLabel;
private JLabel printQualityLabel;

//txt areas
private JTextArea textArea1;
private JTextArea textArea2;
private JTextArea textArea3;

//checkboxes
private JCheckBox imageCheckBox;
private JCheckBox textCheckBox;
private JCheckBox codeCheckBox;
private JCheckBox printCheckBox;

//radio buttons
private JRadioButton selectionRadioButton;
private JRadioButton allRadioButton;
private JRadioButton appletRadioButton;
private ButtonGroup radioGroup;//to create logical connection between radio buttons

//jcombobox
private JComboBox<String> qualityCombo;
private String[] values;

//buttons
private JButton okButton;
private JButton cancelButton;
private JButton setupButton;
private JButton helpButton;

//panels
private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
private JPanel printQualityPanel;//contains print quality combo box 
private JPanel buttonsPanel;//contains buttons

//layout
private GridBagLayout jPanel1Layout;
private GridBagLayout jPanel2Layout;
private GridBagLayout jPanel3Layout;
private GridBagLayout labelLayout;
private GridBagLayout layout;//layout of JFrame

//constraints
private GridBagConstraints constraints;

//constructor
public Form(){
super("Form GUI");
layout = new GridBagLayout();
setLayout( layout );//set layout of …
Violet_82 89 Posting Whiz in Training

OK no problem. Just one question about your method. I looked up the GridBagConstraints constructor just to check what order the parameters follow and here is what I have found:

public GridBagConstraints(int gridx, 
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int anchor,
int fill,
Insets insets,
int ipadx,
int ipady
)

One thing isn't clear: all the parameters are optional correct? So I can include all the above or a few, correct?
thanks

Violet_82 89 Posting Whiz in Training

I'd nominate JamesCherrill http://www.daniweb.com/members/314062/JamesCherrill because he is incredibly helpful and patient (even when I do very silly mistakes that I shouldn't do anymore). Too many threads to post.

Violet_82 89 Posting Whiz in Training

Oh hang on you mean using a GridBagLayout for the JFrame?

Violet_82 89 Posting Whiz in Training

OK I see I will do that then and try out one panel at time.
Yep, I know that the panel is redundant the only reason why I suggested that is because I am a bit worried that by positioning it with the FlowLayout I might not be able to move it to a position as precise as the one I could get with the GridBagLayout

Violet_82 89 Posting Whiz in Training

thanks, it's that I don't know any better, meaning the API has that approach too, add(component, constraint), I wouldn't know how to separate the two. Is there a better way to add the component and separately set the constraints on a component, in other words to split this add(component, constraint)?

Also about the printer label: OK I now understand why the constraints are ignore, thanks for pointing that out. Isn't it worth adding the label to a JPanel so I can use GridBagLayout and all the constraints as opposed to attempt to position it using a flowLayout?

Violet_82 89 Posting Whiz in Training

Yes sorry, I effectively gave the jframe a GridBagLayout and the JPanels a flowLayout. What a noob! Sorry. OK now I have made some changes and created 1 GridLayoutBag object for each panel (+ 1 for the controller that is directly added to the JFrame) to lay out the content of the JPanel and created 1 flowLayout object to control the layout of the panels. Now, even if it still looks really bad, at least it's picking up something:
afcaba465392a58f56a5a2b265d9a2d5
Now I have to determine why it's al messed up. Here is the code just for reference.
One question. As I said I have in total 4 GridBagLayout objects, but I have only 1 constraint of type GridBagConstraints. Is that correct? (Sadly the examples I looked at - both on my book and on the API - don't use JPanels in combination with GridBagLayout).

/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
    //label
    private JLabel printerLabel;
    private JLabel printQualityLabel;

    //txt areas
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea textArea3;

    //checkboxes
    private JCheckBox imageCheckBox;
    private JCheckBox textCheckBox;
    private JCheckBox codeCheckBox;
    private JCheckBox printCheckBox;

    //radio buttons
    private JRadioButton selectionRadioButton;
    private JRadioButton allRadioButton;
    private JRadioButton appletRadioButton;
    private ButtonGroup radioGroup;//to create logical connection …
Violet_82 89 Posting Whiz in Training

OK I see what you mean now, I have done something really stupid. I will have another look and post the results...

Violet_82 89 Posting Whiz in Training

Oh..well the idea was to have a GridBagLayout to position elements inside the JPanels and Flow layout to position the panels themselves. SO I thought that to achieve that I needed 3 flowLayout objects (because there are 3 JPanels)...oh wait...getting confused now. So, to achieve the above how many GridBagLayout objects do I need?

Violet_82 89 Posting Whiz in Training

Duh! I checked the code twice, sorry not sure how I missed that...
Anyway, that sorted the output isn't that freat I am afraid...
4ca1942b3630bb6e21e2e1d153e62995
Umm...it's a bit like it's ignoring all my constraints...

Violet_82 89 Posting Whiz in Training

Hi there,
It took me ages to do this, sorry I got caught into other things.
So, let’s start with a general description. In the end I decided for a 6x7 grid with the top OK button in the top cell and the Help button in the bottom cell.
The programs compiles fine but at execution time I get the following

C:\Users\antonio.borrillo\Documents\useful\JAVA\GUI\14.11>java FormTest
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.hash(Unknown Source)
at java.util.Hashtable.put(Unknown Source)
at java.awt.GridBagLayout.setConstraints(Unknown Source)
at Form.<init>(Form.java:298)
at FormTest.main(FormTest.java:9)

I had a look around my code but I don't understand where the error is and I dont' understand what the error means. in FormTest.java I correctly created an object of type Form. In Form.java al line 298 layout.setConstraints( helpButton, constraints );there is nothing suspicious.
Here is the code:

/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
    //label
    private JLabel printerLabel;
    private JLabel printQualityLabel;

    //txt areas
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea textArea3;

    //checkboxes
    private JCheckBox imageCheckBox;
    private JCheckBox textCheckBox;
    private JCheckBox codeCheckBox;
    private JCheckBox printCheckBox;

    //radio buttons
    private JRadioButton selectionRadioButton;
    private JRadioButton allRadioButton;
    private JRadioButton appletRadioButton;
    private ButtonGroup radioGroup;//to create logical connection between radio buttons

    //jcombobox
    private JComboBox<String> qualityCombo;
    private String[] values;

    //buttons
    private JButton okButton; …
Violet_82 89 Posting Whiz in Training

hello iamthwee, thanks for that. Everything I do, I do it for learning purposes whether it's a project or a test. If it's worked related I do with the learning hat on too, so I guess my point of view is a bit askew. Yes, you're right, there are frameworks available, but using a framework or even a plugin are at times (and depending on the constraints of a project) not the best course of action. Personally, if I can, I prefer to build things myself, at least I know I am in full control of what I did. This - as I have often found out - is not necessarily the case with frameworks and the like. That said, I do not want to diminsh the importance of pre-built solutions - I have used them myself a few times - but sometimes you want something absolutely basic, with not so many options to choose from. So, these are the kind of situations when this sort of code might be handy.

Violet_82 89 Posting Whiz in Training

Hello guys,
A few days ago I was looking on the net for a good jquery tabbed navigation – I needed it for a project – and I have to say that what I found wasn’t really what I was hoping for. An awful lot of people seem to be using plug-in (nothing wrong with that but I try to avoid them unless it is absolutely necessary), others had hundreds lines of script instead (I try to avoid that too). So not terribly satisfied with the results of my search, I thought I’d try to build one myself, using as little code as I could. Well, it turned out it wasn’t as difficult as I thought it would be. My JS is only 18 lines long which makes it very good for beginners (there are lots of comments)! So I thought it might be a good idea to share this with the community in case you are like me and you look for a very very simple, non-plug-in, tabbed navigation solution. Feel free to expand it and do whatever you want with it.
HTML:

<!DOCTYPE html>
<html>
<head>
<title>Tab test</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="wrapper">
<div class="smallBoxWrapper">
<div class="smallBox"></div>
<div class="smallBox"></div>
<div class="smallBox"></div>
<div class="smallBox"></div>
<div class="clear"></div>
</div>
<div class="tabbedMenu">
<ul>
<li class="active"><a href="#">Tab 1</a></li>
<li><a href="#">Tab 2</a></li>
<li><a href="#">Tab 3</a></li>
<li><a href="#">Tab 4</a></li>
<li><a href="#">Tab 5</a></li>
<li><a href="#">Tab 6</a></li>
<li><a href="#">Tab 7</a></li>
<li><a href="#">Tab 8</a></li>
</ul> …
Violet_82 89 Posting Whiz in Training

OK, thanks guys :-)!

Violet_82 89 Posting Whiz in Training

thanks guys, yes it makes sense, but then again, why do browsers interpret this in a different way? I mean clearly chrome and any browser returning 0 when position() is called ignore the margins, but firefox and the other ones that do not return 0, take the margins into account and add them to the calculation of the value returned by position().

Violet_82 89 Posting Whiz in Training

thanks guys, yes I thought about a responsive framework myself, thanks

Violet_82 89 Posting Whiz in Training

Cool. Just something else occurred to me sorry. Do you know when I create the JTextArea objects textArea1 = new JTextArea(); how do I know how many columns and rows should it span? I mean take the first text area: it clearly spans 3 rows but if I include this in the constructor would that really make it span 3 rows the way it looks on the screenshot?
thanks

Violet_82 89 Posting Whiz in Training

that's OK. I am assuming you have properly structured HTML (the one you posted is probably just an excerpt and doesn't have the opening <html> tag, no <head> etc), also make sure you have a proper doctype.

Violet_82 89 Posting Whiz in Training

Why don't you start by removing that awful <br/> in the HTML file between the two lines?

 <div class="body_panel">
                    <h4>main body 3</h4>
                </div>
                <br />
                <div class="body_panel">
                    <h4>main body 4</h4>
                </div>

That should sort you out. Try and post back.

Violet_82 89 Posting Whiz in Training

Hi thanks, yes I did JorgeM and it's kind of clear how to use it, but I thought that in the context of a plugin it was somehow different, that's why I asked :-)

Violet_82 89 Posting Whiz in Training

Sorry, this thing got really interesting. I built a much simpler page with just on div in the middle:
HTML

    <!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="carouselStyle.css">
            <title>Test</title>
            <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
            <script type="text/javascript" src="script.js"></script>
        </head>
        <body>
            <div class="bigBox"></div>
        </body>
    </html>

CSS

*{
    margin:0;
    padding:0;
}


.bigBox{
    width:600px;
    height:300px;
    background-color:yellow;
    margin:0 auto;
}

Script

$(document).ready(function(){
    //alert("ohi");
    var imageContainerPosLeftOffset = $(".bigBox").offset().left;
    var imageContainerPosLeftPosition = $(".bigBox").position().left;
    $(".bigBox").html("Offset is " + imageContainerPosLeftOffset + " position is " + imageContainerPosLeftPosition);
});

Same problem here. Position().left returns 0 in IE9, 10, and Chrome, whereas it returns a value other than 0 (the same as offset().left)in any other browser. So is this a bug with the browsers, in that they can't understand position().left?

Violet_82 89 Posting Whiz in Training

Hello guys, I hope you can clarify something for me please.
I was looking into carousels, found an excellent one and replicated in my code (for reference this is the one I looked at, it’s really good and easy to understand https://tutsplus.com/lesson/the-obligatory-slider/).
Anyway, I have implemented that and I thought I’d do my own controllers, so I grabbed a few images and positioned them absolutely inside the container with the pictures (screenshot attached for reference).
fe2b66806a64f4f99ab98b863add69cc

Let’s have a look at some code.
This is the HTML structure. the carouselWrapper div contains the images and the controllers

<body>
<!-- carousel -->
<div class="carouselWrapper">
<ul>
<li><img src="image1.jpg"></li>
<li><img src="image2.jpg"></li>
<li><img src="image3.jpg"></li>
<li><img src="image4.jpg"></li>
</ul>
<div class="sliderNav">
<button data-dir="prev" class="left transparent"></button>
<button data-dir="next" class="right transparent"></button>
</div>
</div>

<!-- end of carousel -->

</body>

Here is the relevant CSS:
Nothing too involved, the carouselWrapper has default position:static.

.carouselWrapper{
    width:500px; /*size of the pix*/
    /* height:375px; */
    overflow:hidden;
    margin:0 auto;
}

.carouselWrapper ul{
    list-style-type: none;
    width:10000px;  

}
 .sliderNav button.right{
    background:url("controllersRight.jpg") no-repeat 0 0;
    width:48px;
    height:39px;
}
.sliderNav button.left{
    background:url(controllersLeft.jpg) no-repeat 0 0;
    width:48px;
    height:39px;
}
 .sliderNav button.left, .sliderNav button.right{
    position:absolute;
    border:0;
}

And here is the relevant part of the script that positions the two controllers. Again, nothing fancy: I get all the widths and heights I need and then update the top and left value of both the controllers.

/*positioning the controllers*/
    var containerWidth = $(".carouselWrapper").width();//get width of …
Violet_82 89 Posting Whiz in Training

Thanks, sure sorry, you're right, the first it's not needed. I started to build it but then I have realized something: the GridBagLayout will be the layout manager of the JFrame to position the various panels. What type of layout should I instead use for each panel? I think that JPanel3 could have a flowLayout because elements flow from left to right whereas in panel 2 and panel 4 the elements are vertically stack, so perhaps - as mKorbel suggested a gridLayout?
thanks

Violet_82 89 Posting Whiz in Training

OK thanks guys. I have used photoshop and coloured and labelled the different areas that I think should be enclosed in different panels. It's actually harder than it seems, and I am not sure whether the following is correct. There will be 6 columns and 4 rows. I ended up with 4 panels:
1)optional: contains the Printer: my printer label and spans across 5 columns and only 1 row;
2)contains the text areas, the check boxes and the radio buttons and spans 5 cols and only 1 row;
3)Contains the print quality label, the combobox and the print to file check box. It spans 5 cols but only 1 row
4)contains the buttons. It spans 1 column and 4 rows altough it is not aligned with any of the previous ones
What do you reckon?
Here is the visual:
701e0b83e24f6e7fae8aadc8ea123fab
thanks

Violet_82 89 Posting Whiz in Training

Hello, I need to build the following GUI
b1ec49424775312369c320b6b57981d8

and as usual I was hoping to have some advice before I start building it. In particular, I wonder if you guys think I should have any JPanel at all or if it is better to attach everything to the JFrame directly using the GridBagLayout (incidentally, is this the best layout manager to use in this case? I suspect somebody will say yes... :-)! )
Again no functionality is required (this is probably the last one I build without event handlers), so just the layout.
thanks

Violet_82 89 Posting Whiz in Training

OK perfect, thanks, it's clear now!

Violet_82 89 Posting Whiz in Training

Thanks for the demonstration pixelsoul.
I think I know why it didn't work when I did it. Basically I have used this. as opposed to $(this) so I was cycling through the object as opposed to the elements.
Anyway, you guys say I don't need the .each() in this case, which brings me to the next question: when is it that I need it, why would/would I not need it?

Violet_82 89 Posting Whiz in Training

Hello,
today I decided to look into jquery plugins, and with the help of various tutorials I have produce a very simple one that changes the colour of some elements in a page. I would appreciate some help in understanding whether there is something else that I need to do to make it bullet proof (I am sure this plug in in itself is completely useless but it was a good exercise for me to understand how the whole plugin world works).
So here is the HTML first:

<div class="divContainer">
<p>Changing colour plugin</p>
<div class="square">Square 1</div>
<div class="square">Square 2</div>
<div class="square">Square 3</div>
<div class="square">Square 4</div>
<div class="square">Square 5</div>
<div class="square">Square 6</div>
<div class="square">Square 7</div>
<div class="square">Square 8</div>
<button class="changeColour">change colour</button>
</div>

Then the CSS:

.divContainer{
    width:600px;
    height:300px;
    background-color:grey;
    position:absolute;
    top:450px;
}
.square{
    background-color:red;
    width:50px;
    height:50px;
    margin-right:10px;
    float:left;
}

And the script:

$(".changeColour").click(function(){
$(".square").colourThem({
background: 'yellow',
color: 'red'
});
});


/*plugin with options*/
(function($){
$.fn.colourThem = function( options ){
//declare defaults
var defaults = {
background:'green',
color:'white'
}
var settings = $.extend({}, defaults, options );//new object merging defaults and options

this.css({ "backgroundColor": settings.background, 
"color": settings.color
});
return this;
}
}(jQuery));

So, in one of the tutorials (this one is brilliant by the way http://net.tutsplus.com/articles/news/you-still-cant-create-a-jquery-plugin/) I have read that when we build plug-ins since we return an object that contains many elements we need to cycle through them with this.each(function() { }.
In my case I have added that to have …

Violet_82 89 Posting Whiz in Training

To all these valuable comments I will add that I have used ubuntu for a few years now, although I don't really do any scripting so I use ubuntu as I would use any other OS. I have a dual boot machine, WIndows7 and Ubuntu 12.04, and I can say that I rarely use windows now, except for cases in which I couldn't find a way to do something with Ubuntu (like I still can't connect my tv to my laptop the same way I do with windows, or when I use photoshop - I am bit lazy and don't really want to learn how to use Gimp but it's a minor thing). Generally speaking I am very happy I decided to use ubuntu as my main OS, it is much faster than wndows and the only thing is that you have to be willing to thinker with it a little: if something doesn't work it can be fixed but - unless you are a unix wizard - you have to find out how to do it, then do it and probably do it again.

Violet_82 89 Posting Whiz in Training

thanks that's clear now :-)! I guess that the reason why he did it this way is because it would save him doing things like

constraints.gridx = column; // set gridx
constraints.gridy = row; // set gridy
constraints.gridwidth = width; // set gridwidth
constraints.gridheight = height;

for each component, meaning you will still need to add the actual constraints to each of them.
yes exactly like you said, in the API everything is passed explicitly and it takes more lines of code to do that (I am not trying to defend the way my book has done it, just an observation). Also, int he API I have the same problem I had here, I don't understand how the compiler know which method the constraints are applied to

Violet_82 89 Posting Whiz in Training

Cool, maybe I will try to rewrite this one then. I don't know why he had used his own method, there is probably a reason for that. Anyway, pretty much all clear. Just one thing, so I understand things correctly. Going back to my point 2 for the above program, how does the compiler know which component is using the constraints.fill=... (I appreciate that global variables are used a lot, but it's just for me to understand really)
thanks

Violet_82 89 Posting Whiz in Training

Hello, I have looked at an example of GridBagLayout (JamesCherrill you must be thrilled : - )!!) and there are a few things that are not entirely clear to me. First here is the code taken from deitel and deitel “Java how to program”, chapter 25:

// Fig. 25.21: GridBagFrame.java
// Demonstrating GridBagLayout.
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

public class GridBagFrame extends JFrame 
{ 
   private GridBagLayout layout; // layout of this frame
   private GridBagConstraints constraints; // constraints of this layout

   // set up GUI
   public GridBagFrame()
   {
      super( "GridBagLayout" );
      layout = new GridBagLayout();
      setLayout( layout ); // set frame layout
      constraints = new GridBagConstraints(); // instantiate constraints

      // create GUI components
      JTextArea textArea1 = new JTextArea( "TextArea1", 5, 10 );
      JTextArea textArea2 = new JTextArea( "TextArea2", 2, 2 );

      String[] names = { "Iron", "Steel", "Brass" };
      JComboBox comboBox = new JComboBox( names );

      JTextField textField = new JTextField( "TextField" );
      JButton button1 = new JButton( "Button 1" );
      JButton button2 = new JButton( "Button 2" );
      JButton button3 = new JButton( "Button 3" );

      // weightx and weighty for textArea1 are both 0: the default
      // anchor for all components is CENTER: the default
      constraints.fill = GridBagConstraints.BOTH;
      addComponent( textArea1, 0, 0, 1, 3 );    

      // weightx and weighty for button1 are both 0: the default
      constraints.fill = GridBagConstraints.HORIZONTAL;
      addComponent( button1, 0, 1, 2, 1 );

      // weightx and weighty for comboBox are both 0: the default …
Violet_82 89 Posting Whiz in Training

please open another thread faizlo.

Violet_82 89 Posting Whiz in Training

thanks, that's not actually the switch, mapli doesn't seem to have it but I have found it elsewhere. I will check how that's been soldered and take it from there

Violet_82 89 Posting Whiz in Training

right thanks guys. @JamesCherrill: offending panel removed, much better now thanks :-)
@mKorbel ok thanks, didn't occur to me I could use only one jpanel :-)!

Violet_82 89 Posting Whiz in Training

Hello guys, I have a t-rb22 logitech trackball and at the moment it's not working properly. I have thinkered with it a bit, I think I know what the problem is and I need to replace a small switch inside (pictures attached).
7deee63990f867790dc8d2589191b6cccd517819478df412d7a20c4d88486dc7
This switch - well I call it switch - has a little pin at the back not visible in the picture that gets pushed in everytime a button is clicked on the trackball (it's the main left mouse click). I am not really techincal when it comes to these things and I have absolutely no idea where to buy it. It's a 426c IC (as you can see) and there is also a white cover that says 002069-0000 cav4. WOuld anybody know where and if I could buy this part anywhere or where I could get more info? Oh logitech don't sell this trackball anymore so no point asking them.
Thanks

Violet_82 89 Posting Whiz in Training

oh I see, sorry I will get rid of it then. Well, in the previous exercise you said that one of the panels wasn't necessary because it had only one element, but you never mentioned that it could have been a disaster to add it in :-)! Lesson learned, next time I will use it to only group 2 or more elements. So essentially, when there is only one element we can add it directly to the jframe, is that what we are saying?

Violet_82 89 Posting Whiz in Training

Guys, I was wondering if any of you use Umbraco CMS and if so how user-friendly/good/easy-to-use it is.
thanks

Violet_82 89 Posting Whiz in Training

Thanks JamesCherrill. I think the book has been written long after JAVA 1.5, it's deitel and deitel 9th edition.
Yes, I noticed that the width of the combo box was determined by the longest element when I clicked on it :-)

And yes, by now I know very well how you would finish that sentence :-)! There is also another reason why I am not using GridBagLayout: it's because the book I am taking the exercises from doesn't mention GridBagLayout and therefore it assumes that the exercise can be completed with other layouts :-), that's why I was wondering whether my choice of layout manager was wrong.
I came across this render thing on the net, so I thought that perhaps it will help me to set the width of the combo box (although as you said and I agree with that, it makes perfect sense to have it as long as the longest string, but still I can see that in the exercise it is as long as the whole box, so I thought I'd attempt to do that too)

Violet_82 89 Posting Whiz in Training

Uhm, very odd the book I took the exercise from doesn't mention anything at all on the subject.
But surely the compiler should give you the possibility to ignore such a warning and go on regardless!?
Anyway, I have amended it to this:

private JComboBox<String> dropdown; //combobox
dropdown = new JComboBox<>( values );

and the compiler doesn't complain anymore.
So, the layout, here it is
b2e4eb30857d113b5227222c0c585bd3
It looks a bit awful I must say. A couple of things I would like to ask:
My combo box doesn't have any width applied to it. To get the combo box to extend all the way across the window, do I have to create a renderer?
Then somehow the buttons are really far at the bottom. Was my choice of layout managers really wrong? I have used a borderLayout to stack the panels containing each groups of controllers ont he top of one another and I used a flowLayout for each controller pairs, including the combobox.

Violet_82 89 Posting Whiz in Training

Sorry I didn't realize it was telling me to do it again with the option. Here it is:

C:\Users\bah\JAVA\GUI\14.10>javac *.java -Xlint:unchecked
Dropdown.java:38: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox
                dropdown = new JComboBox( values );
                           ^
  where E is a type-variable:
    E extends Object declared in class JComboBox
1 warning
Violet_82 89 Posting Whiz in Training

Hello guys, I have built another small GUI:
c91be0d75e981f9b7ed81b89898a1722
Even before knowing whether the layout of the application I have built is right, I am facing another big problem. At compiling time I get the following error:

Note: Dropdown.java uses unchecked or unsafe operation
Note: Recompile with -Xlint:unchecked for details.

I had a look online and it seems that it has something to do with arrays but for the life of me I don't seem to be able to find the error. ANy idea?
Once that solved we can have a look at any possible error in the layout :-)

Here is the code:

/*Dropdown.java
ex 14.10 p 662.
-create 2 jPanels, one for tick boxes and one for buttons. The dropdown needs no panel. 
Border layout will position the elements in the middle
*/

import java.awt.FlowLayout;//arrange the components inside the panels
import java.awt.BorderLayout;//arrange thepanels containing the components
import javax.swing.JFrame;//provides basic window feature
import javax.swing.JPanel;//for the panels
import javax.swing.JButton;//for the buttons
import javax.swing.JCheckBox;//for the chekboxes
import javax.swing.JComboBox;//for dropdown

public class Dropdown extends JFrame{
    private JCheckBox background;
    private JCheckBox foreground;
    private JPanel comboBoxPanel; //panel holding the checkboxes 
    private JPanel checkboxesPanel; //panel holding the checkboxes 
    private JPanel buttonsPanel;//panel holding the buttons
    private JComboBox dropdown; //combobox
    private BorderLayout JframeLayout;//layout manager for checkboxes panel 
    private FlowLayout comboBoxLayout;//layout manager for combo box
    private FlowLayout checkboxesLayout;//layout manager for checkboxes
    private FlowLayout buttonsLayout;//layout manager for buttons
    private String[] values;//holds the string values of the combo box
    private JButton …