Buttons Generating other Buttons - java

So, i'm asking cause i searched and didn't found nothing about this, i don't know if i'm only searching it wrong.
I'm building a POS (Point of Sale) for my final School work but instead off adding the buttons manually i wanted to make an interface for the admn where he could add the buttons to the main project (ex. I want to add the button for Meat, Fish, etc.)
It's much likely to be easy to do it, my other doubt becomes with, if the button is generated how it will be called so i can use it later on?

With the NetBeans form designer you can see what code must be created.
Then instead of jButton1, jButton2 use List<JButton> buttons = new ArrayList<>();
In the initComponents (or after its call) create the buttons dynamically, using some list with button data: caption Meat / Fish / ... and so on. These data could come from a file you generated, so they are persist even if quitting the application.
A file can be read as:
Path path = Paths.get("buttons.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (String line : lines) {
String[] words = line.split(";\\s*");
if (words.length > 2 && words[0].equals("button")) {
JButton button = new JButton(word[1]);
button.addActionListener(this); ...
... add(button);
buttons.add(button);
}
}

I think you shouldn't generate new buttons. The best way is to hide the buttons you've created by calling button.setVisibility(View.Gone). So just create buttons and call setVisibility(View.Gone) in onCreate. And where needed make them visible by calling button.setVisibility(View.visible).

Related

Autocomplete does not load data correctly

I have a problem with the following code, if filtered data appears to me, but it only filters it when the jframe starts, but not when I add a new one, I have to close the jframe and reopen it so that it just recognizes that data. I put the data in .txt
String barrapro = File.separator;
String ubicacionpro = System.getProperty("user.dir")+barra+"Procesador"+barra;
File contenedorpro = new File(ubicacionpro);
File [] procesadorlistado = contenedorpro.listFiles();
public TextAutoCompleter AutocompletarProcesador;
public Registrar() {
initComponents();
setLocationRelativeTo(this);
AutocompleterReg();
public void AutocompleterReg() {
AutocompletarProcesador = new TextAutoCompleter(TProcesador1);
for (int i = 0; i < procesadorlistado.length; i++) {
AutocompletarProcesador.addItem(procesadorlistado[i].getName().replace(".procesador", ""));
}
}
I saw in some forums that use repaint and but I only want that when one is modified in real time the filtering is updated, it does but it continues showing the data that was already deleted until I close the jframe and reopen it, I also tried to do it with timer but if I do that, it won't let me select as if it were google search, pressing the down arrow key to select the result I want.
video_recording.mp4
The problem you're experiencing is likely due to the fact that the list of files in the "Procesador" directory is only being read once, when the Registrar JFrame is first created. When a new file is added to the directory, the program doesn't know to refresh the list of files.
One way to fix this would be to update the list of files in the directory and repopulate the TextAutoCompleter every time the JFrame is made visible. You can do this by overriding the setVisible() method of the JFrame and updating the list of files and the TextAutoCompleter inside of it.

Java Swing want to keep Radio button selection after Restart

Friends
I am a beginner and trying to develop a system level java swing software.
I have 2 JRadio buttons viz A & B in a RadioButton group bg.
I want to keep the radio button selection after restart or until further selection.
Searched for this long in net but getting code for PHP,HTML etc.
Somebody please help me.
rdbtA = new JRadioButton("A");
contentPane.add(rdbtA);
rdbtB = new JRadioButton("B");
contentPane.add(rdbtB);
ButtonGroup bg = new ButtonGroup();
bg.add(A);
bg.add(B);
When you start your application all the graphic components ar re-created, so you have to save the selection in some way when you close your program or when the selection changes (the easiest is to save your choice in a file) and restore it when the software is started (after the buttons creation).
Let me explain this in code:
rdbtA = new JRadioButton("A");
contentPane.add(rdbtA);
rdbtB = new JRadioButton("B");
contentPane.add(rdbtB);
/*
1. If exists a save-file, open it (else, ignore 2. and 3.)
2. read the value of previous A and previous B
3. Set these values to rdbtA and rdbtB
*/
//Rest of the code

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.
What I have:
I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.
The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.
I was going to post screen shots but you need 10 rep for that. It would have explained everything better.
Here is the code for the label on the Error_dialog window:
Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");
Here is the condition I would like to fire off when it is met:
if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
showError.open();
}
I have included this at the top of the class as well:
Error_dialog showError = new Error_dialog();
Just save the label as a field in your dialog class and add a 'setter' method. Something like:
public class ErrorDialog extends Dialog
{
private Label errorLabel;
... other code
public void setText(String text)
{
if (errorLabel != null && !errorLabel.isDisposed()) {
errorLabel.setText(text);
}
}
You will need to use your dialog like this:
ErrorDialog dialog = new ErrorDialog(shell);
dialog.create(); // Creates the controls
dialog.setText("Error message");
dialog.open();
Note: you should stick to the rules for Java variable names - they always start with lower case.
Further learn to use Layouts. Using setBounds will cause problems if the user is using different fonts.

Simple Java GUI, cards not appearing

import javax.swing.*;
public class SlideShow {
JFrame slide = new JFrame("Slide Show");
public SlideShow(){
slide.setSize(300,400);
slide.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
slide.setVisible(true);
slide.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel(new ImageIcon("Images/picture1"));
panel.add(label);
slide.add(panel);
}
public static void main(String[] args){
SlideShow slide = new SlideShow();
}
}
I have to create a simple Java GUI that displays some cards. First, I just wanted to test it by displaying one card. For some reason I can't seem to figure out why nothing is being displayed.
You haven't actually used a proper file name "Images/picture1". Should be something like "Images/picture1.png" with the file format
Also image files, generally should be read from the class path, if you plan on having them embedded to the program. To do so, you will first need to put the file in the class path. With most IDE build configurations it's as simple as placing the image in the src. So
ProjectRoot
src
images
picture1.png
Then you would read it like
new ImageIcon(getClass().getResource("/images/picture1.png"));
A better approach would be to use ImageIO.read(). If the file path is incorrect, it will throw an exception, so you know where you're going wrong
Image image = ImageIO.read(getClass().getResource("/images/picture1.png"));
ImageIcon icon = new ImageIcon(image);
You will need to put it in the try/catch block
Also do what codeNinja said about the setVisible() after adding component. Also preferably pack() the frame, instead of setSize()
You need to set the Frame visible after you add all necessary components to it. Move slide.setVisible(true); Down to the bottom of the constructor like this:
...
slide.add(panel);
slide.setVisible(true);
Alternatively you can add slide.revalidate(); at the bottom of your constructor.

Tripleplay Button: Dynamic text with proper alignment on an Image Button (say the text justified , centre)

I am creating a window with two image buttons (using TriplePlay in my playN game).
Now I need dynamic text on these buttons. But when I add buttons with images (setIcon), I am not able to add Text on it same time. Please check the following code block I use now.
Interface iface = new Interface(null);
pointer().setListener(iface.plistener);
Styles buttonStyles = Styles.none().add(Style.BACKGROUND.is(new NullBackground())).
addSelected(Style.BACKGROUND.is(Background.solid(0xFFCCCCCC)));
Stylesheet rootSheet = Stylesheet.builder().add(Button.class, buttonStyles).create();
Root buttonroot = iface.createRoot(AxisLayout.horizontal().gap(150), rootSheet);
buttonroot.setSize(width_needed, height_needed);
buttonroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(buttonroot.layer);
Button you = new Button().setIcon(buttonImage);
Button friend = new Button().setIcon(buttonImage);
buttonroot.add(you).add(friend);
buttonroot.layer.setTranslation(x_needed, y_needed);
Root nameroot = iface.createRoot(AxisLayout.horizontal().gap(300), rootSheet);
nameroot.setSize(width_needed, height_needed);
nameroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(nameroot.layer);
name = new Label("YOU");// we need the dynamic string variable instead
friendName = new Label("FRIEND"); // we need the dynamic string variable instead
nameroot.add(name).add(friendName);
nameroot.layer.setTranslation(x_needed, y_needed);
here I have tried making a root then add button with images to it then making another root and add labels on it so that it will be show like text on the image buttons. But I know this is a bad way of doing it, and the alignment will not be according to what needed as it a dynamic text. Is there anyway to add a button, with image and a label on it?
Thanks in anticipation
Creating a button with text and an icon is trivial:
Button button = new Button("Text").setIcon(iconImage);
You can then change the text on the button any time you like, like so:
button.text.update("New Text");
If you want a button with a background image with text rendered over the background, then do the following:
Button button = new Button("Text").
addStyles(Background.is(Background.image(bgImage)));
Note that you will need the latest TriplePlay code (from Github) to use the ImageBackground. The latest code also supports Flash-style "scale9" backgrounds:
Button button = new Button("Text").
addStyles(Background.is(Background.scale9(bgImage)));

Categories