I want to personalize the JXDatePicker. More specifically I want to create an own own action button on a an own position. I tried to extract the included JButton but when I try to add it to any JPanel it only adds empty space.
JXDatePicker fDate = new JXDatePicker(new Date());
fDate.getEditor().setBorder(null);
fDate.getEditor().setEditable(false);
JButton eDate = (JButton) fDate.getComponent(1);
fDate.remove(eDate);
I don't think this approach would work anyway, but I don't know any better at the moment.
Any suggestions?
found a working solution, migt be not the cleanest way:
JXDatePicker fDate = new JXDatePicker(new Date());
//edit the Textfield
fDate.getEditor().setBorder(null);
fDate.getEditor().setEditable(false);
//edit the Button
JButton dateBtn= (JButton) fDate.getComponent(1);
Image editImage =Toolkit.getDefaultToolkit().getImage(getClass().getResource("/toolbarButtonGraphics/general/Edit16.gif"));
dateBtn.remove(eDate);
dateBtn.setIcon(new ImageIcon(editImage));
dateBtn.setFocusPainted(false);
dateBtn.setMargin(new Insets(0, 0, 0, 0));
dateBtn.setContentAreaFilled(false);
dateBtn.setBorderPainted(false);
dateBtn.setOpaque(false);
I added the dateBtn somewhere else in my view.
((JButton) startDatePicker.getComponent(1)).setIcon(new ImageIcon(((new ImageIcon(getClass().getResource("/images/calendar.png"))).getImage()).getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
Related
I'm relatively new to programming. Hoping someone can help. I have a button that creates a series of labels and text boxes within a frame once the user enters a number. I'm having trouble access the text boxes once they are created. Can someone point me in the right direction. Code snips below. Thanks.
method to create a label and text box.
public JTextField createPrizePanels(){
JLabel prizePanel = new JLabel("Enter Prize Here", SwingConstants.CENTER);
prizePanel.setVerticalAlignment(SwingConstants.TOP);
prizePanel.setFont(new Font("Arial", Font.BOLD, 14));
prizePanel.setForeground(Color.BLUE);
Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
prizePanel.setBorder(border);
prizePanel.setOpaque(true);
prizePanel.setBackground(Color.LIGHT_GRAY);
prizePanel.setBounds(setBoundsX, setBoundsY, 120, 60);
prizeTextBox = new JTextField(50);
prizeTextBox.setBounds(setBoundsX + 5, setBoundsY + 20, 110, 30);
prizeTextBox.setFont(new Font("Arial", Font.BOLD, 12));
prizeTextBox.setOpaque(true);
prizeTextBox.setBackground(Color.WHITE);
prizeTextBox.setForeground(Color.BLACK);
prizeTextBox.setText("No Prize");
prizeTextBox.setHorizontalAlignment(JTextField.CENTER);
lp.add(prizePanel);
lp.add(prizeTextBox);
return prizeTextBox;
}
code that creates multiple text boxes bases on input from user.
JButton numberOfBallonsButton = new JButton("Set");
numberOfBallonsButton.setBounds(360,160,95, 0x1e);
numberOfBallonsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numberOfBallons = Integer.parseInt(numberOfBallonsTextBox.getText());
System.out.println(numberOfBallons);
lp.remove(numberOfBallonsButton);
for(int i = 0; i < numberOfBallons; i++ ){
createPrizePanels();
setBoundsX = setBoundsX +125;
if(setBoundsX > 450){
setBoundsX = 120;
setBoundsY = setBoundsY + 65;
}
}
lp.add(startGameButton);
}
});
Up to this point it works fine. However, the user needs to enter text into each text box and press another button. I'm not sure how to access each text field. Thanks in advance.
You are using local fields.you can create global field and the return value of createPrizePanels() is never used and for your case you don't know number of created textfield so you can use List as global field and when you create textfield add it to the list so you access it later from anywhere
why if i have an image background i cant see all my buttons? only if my arrow go on button i see it..
this is my jlabel with background:
JLabel sfondo = new JLabel("");
sfondo.setBounds(0, 0, 832, 527);
contentPane.add(sfondo);
sfondo.setIcon(new ImageIcon(
"C:\\Users\\bocci\\workspace\\it's time of the series\\src\\immagini\\icone\\sfondo.jpg"));
sfondo.setIcon(icona3.newicona(patchsfondo, sfondo));
and i have 5 jbuttons
JButton btnOpzioni = new JButton("Opzioni");
btnOpzioni.setBounds(497, 5, 67, 23);
contentPane.add(btnOpzioni);
i see only first button.
I know that if I want underline a text in a JButton I have to do something like this:
JButton myButton=new JButton("Test");
Font font = myButton.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
myButton.setFont(font.deriveFont(attributes));
And, If I would overline a text? What should I do? Could anyone help me?
No need for the Font class here. You can use HTML directly and add the <u> and <strike> tags:
myButton = new JButton("<html><strike><u>Test</u></strike></html>");
Just adding to #maroun's answer(I'm not allowed to comment). try using HTML directly.
myButton = new JButton ("<html><font style=\"text-decoration: overline;\"> Overline text</font></html>");
You can create a CompoundBorder:
JButton button = new JButton("Using Compound Border");
Border lineBorder = BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK);
CompoundBorder border = new CompoundBorder(button.getBorder(), lineBorder);
button.setBorder( border );
I want to use this JXDatePicker component in a application that will work on a touch display. Because the default component is small, all the dates and the buttons are hard to click using a bad touch screen I wanted to make them bigger. So far I successfully made the result text field bigger (the one showing the selected date, by changing the font), make the pop-up bigger (the JXMonthView, also by changing its font), change the picture of the JXDatePicker with a bigger image, set the default date to be the current date, set the date format, etc. This is my code:
private void touch_screen_datepicker(JXDatePicker date_picker) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
date_picker.setDate(new Date());
date_picker.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
JButton btn_pick = (JButton) date_picker.getComponent(1);
btn_pick.setBackground(new Color(66, 147, 223));
Image image = toolkit.getImage("/home/adrrian/Image/calendar/" + "calendar image 4.png"); //Земање на сликата за мк знаме
ImageIcon icon = new ImageIcon(image); //Правење на икона
btn_pick.setIcon(icon); //Поставување на иконата
SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat shortFormat = new SimpleDateFormat("yy-MM-dd");
Date startDate = new Date(0);//01.01.1970
shortFormat.set2DigitYearStart(startDate);
DatePickerFormatter formatter = new DatePickerFormatter(
// invers sequence for parsing to satisfy the year parsing rules
new DateFormat[]{shortFormat, longFormat}) {
#Override
public String valueToString(Object value) throws ParseException {
if (value == null) {
return null;
}
return getFormats()[1].format(value);
}
};
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
date_picker.getEditor().setFormatterFactory(factory);
monthView.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
monthView.setFirstDayOfWeek(Calendar.MONDAY);
}
and this is an image of my final work:
My main problem is how to make the arrow that are changing the months (for example if I go back from this image to show September). I tried listing all of the components, like I did for the button, but still I didn't found anything. Also for better GUI I like to find that dark blue color (where the month is displayed), to make my button the same.
Hope someone can help me. Thanks in advance.
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
/*EDITED*/
//1
date_picker.putClientProperty("JComponent.sizeVariant", "large");
monthView.putClientProperty("JComponent.sizeVariant", "large");
//2
date_picker.putClientProperty("JXComponent.sizeVariant", "large");
monthView.putClientProperty("JXComponent.sizeVariant", "large");
//3
date_picker.putClientProperty("JXDatePicker.sizeVariant", "large");
monthView.putClientProperty("JXMonthView.sizeVariant", "large");
//
date_picker.putClientProperty("JDatePicker.sizeVariant", "large");
monthView.putClientProperty("JMonthView.sizeVariant", "large");
SwingUtilities.updateComponentTreeUI(this);
SwingUtilities.updateComponentTreeUI(date_picker);
SwingUtilities.updateComponentTreeUI(monthView);
date_picker.updateUI();
monthView.updateUI();
/*EDITED*/
As #Vighanesh Gursale suggested I insterdet this lines and also did the frame.pack() before setVisible(true), but nothing changes.
I have made some code using nimbus look and feel, i know it is not exact that you want but it is pretty much helpful. Check this code. To perform same you need to find the key of your next and previous buttons key in my case it is Button.margin. Try to use the same key in your code if you are lucky it would work.
import javax.swing.*;
import java.awt.*;
public class Demo {
JFrame frame = new JFrame("");
JButton btn = new JButton("Example");
public Demo() {
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setVisible(true);
}
public static void main(String[] args) {
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
Insets insets = new Insets(50, 20, 50, 20); //change the size of button
UIManager.put("Button.margin", insets);
}
catch(Exception e)
{
e.printStackTrace();
}
Demo d = new Demo();
}
}
Also keep in mind to use default look and feel or any other look and feel.
This question is quite old but for everyone who is looking how it can be done
UIManager.put("JXMonthView.monthDownFileName", <PATH_TO_IMAGE>);
UIManager.put("JXMonthView.monthUpFileName", <PATH_TO_IMAGE>);
monthDownFileName refers to previous month
monthUpFileName refers to next month
this will change the image with other of your choice (and size)
How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.
Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working.
Here's a code snippet to insert a formatted "Hello World!" string in a JEditorPane:
Document doc = yourEditorPane.getDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);
Font font = new Font("Arial", Font.BOLD, 18);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Hello World!", style);
Take a look at the following code in this pastebin:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3
The example is from here:
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm
It looks like you can change the style using the following in an action listener:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
It sets the style of the text between the given offset and length to a specific style.
See the full pastebin for more details. That should fix your problem though.
The easiest way to manipulate text panels is using editor kits and their associated actions. You can find a demo of this in the JDK samples (under jdk\demo\jfc\Stylepad).
Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text:
public static void main(String[] args) {
// create a rich text pane
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// install the editor kit
StyledEditorKit editorKit = new StyledEditorKit();
textPane.setEditorKit(editorKit);
// build the menu
JMenu fontMenu = new JMenu("Font Size");
for (int i = 48; i >= 8; i -= 10) {
JMenuItem menuItem = new JMenuItem("" + i);
// add an action
menuItem
.addActionListener(new StyledEditorKit.FontSizeAction(
"myaction-" + i, i));
fontMenu.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(fontMenu);
// show in a frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setJMenuBar(menuBar);
frame.setContentPane(scrollPane);
frame.setVisible(true);
}
(Tip: if you want to use a FontFamilyAction, have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names.)
I'd recommend taking a look at Sun's Java Tutorial about editor panes.
Ok, wow. Hard question. So I have not found a way to get the style of a given character. You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set.
Style s; //your style
Element run = styledDocument.getCharacterElement(
textPane.getSelectionStart() );
MutableAttributeSet curAttr =
( MutableAttributeSet )run.getAttributes();
boolean containsIt = curAttr.containsAttributes( s );
One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not).
To update the selected text you can:
Style s; //your style
JTextPane textPane; //your textpane
textPane.setCharacterAttributes( s, false );
Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p.