I really doesn't understand what it's going on with my JSpinner.
I instanciated it and then I tried to set it on my JPanel with a setBounds but the only thing I get is this :
The little square on the middle is what should be my JSpinner..
My code is like this :
private JSpinner spinnerDayBirth;
spinnerDayBirth = new JSpinner();
spinnerDayBirth.setBounds(280,351, 25, 25);
add(spinnerDayBirth);
They're few lines betweens each of these instructions but they never touch to this JSpinner.
So I wonder why I can't get a normal JSpinner..
Thank you in advance !
I had same problem. I worked with simply spinnerModel then I tried your program and it works too.
There is one thing to do - re-set your model. If you want the spinner to appear you have to set new spinner model again.
for example i typed this code:
public void method() {
jPanel2.setLocation(0, 96);
jPanel2.setSize(getWidth(), getHeight() - 96);
jPanel2.getHeight() - jButton1.getHeight() - 50);
jSpinner1.setModel(new SpinnerNumberModel(2,1,10,1)); //<- this is line you need to type
jSpinner1.setSize(60, 25);
jSpinner1.setLocation(0,0);
}
Related
I have knowledge about C programming but I'm a newbie Java Programmer, and I want to ask a question to you. I try to set both spinners values null if they are less then "1" but I couldn't do it. I'm waiting for your helps, thank you.
Spinner spinnermin = new JSpinner();
spinnermin.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
valuemin =(Integer) spinnermin.getValue();
if(valuemin<0){
spinnermin.setValue(null);
}
}
});
spinnermin.setBounds(478, 215, 76, 20);
frame.getContentPane().add(spinnermin);
JSpinner spinnerhour = new JSpinner();
spinnerhour.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
valuehour = (Integer) spinnerhour.getValue();
if(valuehour<1){
spinnerhour.setValue(null);
}
}
});
spinnerhour.setBounds(362, 215, 86, 20);
frame.getContentPane().add(spinnerhour);
You state:
I try to set both spinners values null if they are less then "1" but I couldn't do it.
Much better to use a SpinnerNumberModel that doesn't allow for negative values. For example,
JSpinner hourSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 24, 1));
This creates a JSpinner that allows int values from 1 to 24, and that starts at 1.
It really doesn't make sense to assign null to a component that expects to hold a numeric value, and rather than try to do this, better to limit the user's ability to input unacceptable values. Also please have a look at the JSpinner Tutorial.
As a side recommendation, while null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Here I am using JMonthChooser and JYearChooser. So how to Change BackGround of JMonthChooser and JYearChooser is there any Idea. how to do it.
I am using Netbeans.
I assume that you use toedter's JCalendar, that you can add to NetBeans'palette.
In this case you have to make it in 3 times for a WHITE background, 2 for other background's colors(3rd point of the belowed list is not useful in this case).
get the JCombobox (Java Component). You have to cast it into a JComboBox because the method getComboBox() returns a java.awt.Component.
javax.swing.JComboBox box = (javax.swing.JComboBox) monthChooser.getComboBox();
Modify the JComboBox's Renderer to change list's background (more examples here).
box.setRenderer(new javax.swing.DefaultListCellRenderer() {
#Override
public void paint(java.awt.Graphics g) {
setBackground(new java.awt.Color(255, 255, 255));
setForeground(java.awt.Color.BLACK);
super.paint(g);
}
});
Set the "collapsed list" (selected) background (WHITE only)
box.setOpaque(false);
Hope that help.
Actually JCalender is made of multiple components. So, if you want to change background or foreground of it, then first you have to traverse from all different subcomponents of it and then change each's background color.
In my case:
JDateChooser jdatechooser = new JDateChooser();
//to change background color : <br>
for( Component c : jDateChooser1.getComponents()){<br>
((JComponent)c).setBackground(Color.YELLOW); // whatever color you want to choose<br>
}
I have a JPanel in which I need to add bunch of JLabels at a required coordinates. These JLabels will have key Listeners assigned to them that will determine new position using arrow keys.
To be more specific I know how to do it when there is only one JLabel but whenever I put a more of them the things mess up. while I use arrow key the first JLabel moves but all other JLabel disappears.
Can Anyone give me some hints to write a method to put a JLabel in a specific coordinate and also move them using arrow key later without making other JLabels dissapear?
Huge Thanks in Advance
You can try using JDesktopPane or JLayeredPane, it works the same as the JPanels, but you won't use layouts, with these you will use Bounds, you always have to set the bound of a jlabel like this.
JLabel label = new JLabel("Hello");
label.setBounds(0, 0, 100, 20);
//label.setBounds(x, y, width, height);
pane.add(label)
if you need to move that label, then you can use something like
int xx = label.getBounds().getX();
int yy = label.getBounds().getY();
int ww = label.getBounds().getWidth();
int hh = label.getBounds().getHeight();
//to the right 10 units
xx+=10;
label.setBounds( xx, yy, ww, hh );
I assume you are using repaint() to update the UI. Btw, upon which component you are calling repaint()?
I want to set the text on a JButton that is size 32x32 but it only shows "...". yeah I know you could see the text if you make the button bigger, but how do you make the text be shown on a 32x32 jbutton? The text is only 1 or 2 digits(characters), it is actually a counter. Thanks
The insets are probably crowding out the text...
try
button.setMargin(new Insets(1, 1, 1, 1));
edit: Also, use a smaller font.
edit2: you can also control the insets for all buttons:
UIManager.put("Button.margin", new Insets(1, 1, 1, 1));
I don't think you can, this is managed directly by the look'n'feel' that is used by Java. You could try by changing it to another one to see if there is one with different insets. You could try changing them by setting smaller insects programatically.
A more complex way would be to subclass the JButton class and provide a custom drawing implementation but I think you will lose all the other cool effect.
As per my idea , its quite simple to making GUI application easier.I am writing some code below it may help you .
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frm=new JFrame("Manoj Button Test");
frm.setVisible(true);
frm.setSize(500,500);
Container cnt=frm.getContentPane();
//You can add any text to the JButton
JButton btn=new JButton("Hello Button");
cnt.add(btn);
//2nd type of example
JButton btn2=new JButton();
int number_btntext=4;
btn2.setText(String.valueOf(number_btntext));
cnt.add(btn2);
}
In the above code I have set text to GUI JButton.
I am making an autocomplete project(just like Google). I have a jtextfield in my frame and whenever I type something to that field, a JWindow will appear below the textfield and that window is coming from another class.
Now the problem is how could I make the window always appear below the textfield whenever I drag the frame?
Any help would be much appreciated.. Thanks...
for manually set Location on the screen, you have to define something as private Point location; and getLocation from desired JComponent, don't forget dealyed show for Show Top-Level Container into invokeLater();
for example
public void showWindow() {
window.setVisible(false);
location = myTextField.getLocationOnScreen();
int x = location.x;
int y = location.y;
window.setLocation(x - 20, y - 20);
Runnable doRun = new Runnable() {
#Override
public void run() {
window.setVisible(true);
}
};
SwingUtilities.invokeLater(doRun);
}
Use SwingUtilities.convertPointToScreen()/convertPointFromScreen() pass JTextField's position and get coordinates on screen for the JWindow.
If I understood well, you're trying to do a JTextfield which propose a list of suggestions when the user enters some text.
I used this by the past: http://www.java2s.com/Code/Java/Swing-Components/AutocompleteComboBox.htm
It needs a bit of refactoring but your problem is what I said, it will help you solve it much more easily (and elegantly)!
To use the example:
Java2sAutoTextField textField = new Java2sAutoTextField(
Arrays.asList(new String[] {"Value 1","Value 2"}));
You should not use a JWindow for that but a JComboBox instead of the TextField.
JWindow's are designed to be toplevel windows ...