を超えている、これは私が見つけることができる最も近いもの>How to set a Maximum and Minimum value that can be entered in a Jformattedtextfield , using DocumentFilter?た値が最大
1号だった場合JTextFieldが最大値にDocumentFilterの自動設定を使用して:問題は、最大値を自動入力する代わりに入力を制限することです。私はそれをどうやってこれをするのか分からない。たとえば、maxValueは255に設定されているため、「256」または「345」と入力すると、自動入力「255」ではなく「25」または「34」でスタックされます。
さらに、テキストボックスに「255」より大きい数値をコピーして貼り付けると、テキストボックスには値がありませんが、値は入力されません(「255 ")。
問題2:私はまた、testObject.setObjectValue(value)
行を実行したいと思いますが、この行をどこに入力するかわかりません。問題1の場合
:私は、このファイルのコードを見てみます(RestrictedNumberDocumentFilter.java)問題2の場合
:私は、このファイルのコードからコードスニペットを見てしまう(TestGui.java):
ここでprivate void setEnterTxtInTextFieldAction(){
AbstractDocument doc = (AbstractDocument)enterTxtInTextField.getDocument();
doc.setDocumentFilter(new RestrictedNumberDocumentFilter (testObject.getMinValue(), testObject.getMaxValue()));
}
は... 3つのファイルです
TestObject.Java:
public class TestObject {
private int objectValue;
private final int maxValue = 255;
private final int minValue = 0;
protected int getMaxValue(){
return this.maxValue;
}
protected int getMinValue(){
return this.minValue;
}
protected int getObjectValue(){
return this.objectValue;
}
protected void setObjectValue(int someValue){
if (someValue > this.maxValue){
this.objectValue = this.maxValue;
} else if (someValue < this.minValue){
this.objectValue = this.minValue;
} else {
this.objectValue = someValue;
}
}
}
TestGui.java:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class TestGui extends JFrame {
//**************************************************************************************
// Variables
private TestObject testObject = new TestObject();
private int enterTxtInTextFieldFontSize = 16;
private int enterTxtInTextFieldWidth = 100;
private int enterTxtInTextFieldHeight = 40;
private JTextField enterTxtInTextField = createWhiteBoldFgDarkGreyBgFixedSizeAlignTextField("", enterTxtInTextFieldFontSize, enterTxtInTextFieldWidth, enterTxtInTextFieldHeight, SwingConstants.LEFT);;
private JPanel topFrame = createTopFrame();
private JScrollPane topFrameScroll = createTopScrollPane();
private JPanel centerFrame = createCenterFrame();
//**************************************************************************************
// Constructor
TestGui(){
add(topFrameScroll, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);
setSize(1280,720);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//**************************************************************************************
// Support Methods
protected static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// String can be changed into an integer
return true;
}
private static GridBagConstraints setGbc(int gridx, int gridy, int gridWidth, int gridHeight, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
GridBagConstraints gbc = new GridBagConstraints();
if (anchorLocation.toUpperCase().equals("NORTHWEST")){
gbc.anchor = GridBagConstraints.NORTHWEST;
} else if (anchorLocation.toUpperCase().equals("NORTH")){
gbc.anchor = GridBagConstraints.NORTH;
} else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
gbc.anchor = GridBagConstraints.NORTHEAST;
} else if (anchorLocation.toUpperCase().equals("WEST")){
gbc.anchor = GridBagConstraints.WEST;
} else if (anchorLocation.toUpperCase().equals("EAST")){
gbc.anchor = GridBagConstraints.EAST;
} else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
gbc.anchor = GridBagConstraints.SOUTHWEST;
} else if (anchorLocation.toUpperCase().equals("SOUTH")){
gbc.anchor = GridBagConstraints.SOUTH;
} else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
gbc.anchor = GridBagConstraints.SOUTHEAST;
} else {
gbc.anchor = GridBagConstraints.CENTER;
}
gbc.gridx = gridx; // column
gbc.gridy = gridy; // row
gbc.gridwidth = gridWidth; // number of columns
gbc.gridheight = gridHeight; // number of rows
gbc.ipadx = ipadx; // width of object
gbc.ipady = ipady; // height of object
gbc.weightx = weightx; // shifts rows to side of set anchor
gbc.weighty = weighty; // shifts columns to side of set anchor
gbc.insets = insets; // placement inside cell
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
return gbc;
}
private Insets setInsets(int top, int left, int bottom, int right){
Insets insets = new Insets(top,left,bottom,right);
return insets;
}
//**************************************************************************************
// Interactive Object Methods
private JTextField createWhiteBoldFgDarkGreyBgFixedSizeAlignTextField(String text, int textSize, int width, int height, int hAlign){
JTextField txtField = new JTextField(text);
txtField.setForeground(Color.WHITE);
txtField.setBackground(new Color(50,50,50));
txtField.setCaretColor(Color.CYAN);
txtField.setFont(new Font(text, Font.BOLD, textSize));
txtField.setPreferredSize(new Dimension(width, height));
txtField.setHorizontalAlignment(hAlign);
return txtField;
}
//**************************************************************************************
// Object Action Methods
private void setEnterTxtInTextFieldAction(){
AbstractDocument doc = (AbstractDocument)enterTxtInTextField.getDocument();
doc.setDocumentFilter(new RestrictedNumberDocumentFilter (testObject.getMinValue(), testObject.getMaxValue()));
}
//**************************************************************************************
// Panel Methods
private JPanel createTopFrame(){
// pnl.add(object, setGbc(column,row, columnFill,rowFill, columnExtraWidth,columnExtraWidth, cellAlignment, weightColumn, weightRow, setInsets(top, left, bottom, right)));
JPanel pnl = new JPanel();
pnl.setLayout(new GridBagLayout());
Border gridBorder = BorderFactory.createMatteBorder(4,4,4,4,Color.BLUE);
JLabel enterText = new JLabel("Enter Text");
enterText.setBorder(gridBorder);
enterTxtInTextField.setBorder(gridBorder);
setEnterTxtInTextFieldAction();
pnl.add(enterText, setGbc(0,0, 1,1, 0,0, "CENTER", 0, 0, setInsets(10, 10, 10, 10)));
pnl.add(enterTxtInTextField, setGbc(0,1, 1,1, 0,0, "CENTER", 0, 0, setInsets(10, 10, 10, 10)));
return pnl;
}
private JScrollPane createTopScrollPane(){
JScrollPane scrollPane = new JScrollPane();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Border lineBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(224,224,224));
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
scrollPane.setBorder(compoundFinal);
scrollPane.getViewport().setView(topFrame);
return scrollPane;
}
private JPanel createCenterFrame() {
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224, 224, 224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);
pnl.setBorder(topFrameTitle);
return pnl;
}
//**************************************************************************************
public static void main(String[] args) {
new TestGui();
}
}
RestrictedNumberDocumentFilter.java:@Andrewトンプソンは、上記の提案として
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class RestrictedNumberDocumentFilter extends DocumentFilter{
private int min;
private int max;
public RestrictedNumberDocumentFilter(int min, int max){
if (max < min){
int temp = max;
max = min;
min = temp;
}
this.min = min;
this.max = max;
}
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr) throws BadLocationException {
StringBuilder sb = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength()));
if (test(sb.toString())){
fb.insertString(off, str, attr);
} else {
//warn
}
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int off, int len, String str, AttributeSet attr)throws BadLocationException {
StringBuilder sb = new StringBuilder(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.replace(off, off+len, str);
if (test(sb.toString())){
fb.replace(off, len, str, attr);
} else {
//warn
}
}
/**
* Sanitized the input
* @param val
* @return
*/
private boolean test(String val){
try{
double d = Double.parseDouble(val);
if (d >= min && d <= max){
return true;
}
return false;
}catch(NumberFormatException e){
return false;
}
}
}
'SpinnerNumberModel'と' JSpinner'を使用することを検討してください。 –