User-Interface question - java

i'm kinda new to manipulating UI in java, so pls forgive me for this question i cant find the answer anywhere.
Description
| i'm trying to do a card game and i have an engine class that manipulates all the cards and plays made, and i want the engine to tell the UI to update the score, card position or card image.
this is an example of how i start the UI, problem here is i don't have any instance to manipulate the JLabels using the instance methods i made inside Board class, and i can't create a instance outside of the EventQueue because i would be violating "never manipulate/create UI outside UI thread"
public class Engine {
public StartUp(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (UnsupportedLookAndFeelException e) {
}
new Board().setVisible(true);
}
});
}
}
the Board class extends JPanel and adds some JLabels to the ui in the constructor , and has several methods to change text and imgs.
My question is how to properly call these methods(the ones i created to alter text and img), i'm also open o any other suggestions on how to approach this problem.
*edited:
here's simple example of my Board Class:
public class Board extends JFrame{
public JLabel img1;
public Board(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 265);
JPanel body = new JPanel(new GridBagLayout());
getContentPane().add(body);
img1 = new JLabel();
body.add(img1);
}
public void setImg1(String s){
img1.setIcon(new ImageIcon(s));
}
}
i want to be able from Engine to access setImg1(String s) method that is inside the Board to be able to change the current image during runtime
sorry if i expressed my question wrong
final edit:
solved it merging the Engine into the Board.
ty to everyone that helped and for your time

public class MainFrame extends JFrame {
public MainFrame() {
super("Demo frame");
// set layout
// add any components
add(new Board()); // adding your board component class
frameOptions();
}
private void frameOptions() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // or setSize()
setVisible(true);
}
public static void main(String[] a) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
new MainFrame();
} catch (Exception exp) {
exp.printStackTrace();
}
}
});
}
}

The basic idiom for getting a GUI up is:
SwingUtilities.invokeLater(new Runnable() {
JFrame frame = new JFrame("My Window Title");
frame.setSize(...);
frame.add(new Board()); // BorderLayout.CENTER by default
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // center on main screen
frame.setVisible(true);
});

Related

Tabbedpane change tabs position Java Swing

I have i situation when my Tabbedpane have 3 tabs and then I choose someone of them, it sets in first position and move whole tabs.
I couldn't find in Internet anything like that, everyone has fixed positions of their tabs. How to fix tabs?
//EDITED
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
JFrame frame = new JFrame("Somest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
frame.add(UpperPanel);
}
}
Find out that try {...} "enable" this problem. Used try {...} to change JChooser from standard to more-or-less Windows chooser view. What I have to do?
The reason is that this is implemented differently in various Look and Feel implementations (See here, section 6.2.1):
[...] selecting a tab that is not in the frontmost row or column moves that row or column to the front. This does not occur in a JTabbedPane using the default Metal L&F as can be seen in the TabbedPaneDemo example above. However, this does occur when using the Windows, Motif, and Basic L&Fs. This feature was purposefully disabled in the Metal L&F [...]
The responsible method is shouldRotateTabRuns(...) and returns true or false in various TabbedUI implementations (see Metal L&F vs. Basic L&F).
To prevent tab rotation, you could overwrite like this:
public class TabExample extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
TabExample app = new TabExample();
app.setVisible(true);
}
});
}
private TabExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
setLocationRelativeTo(null);
setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
// Check if TabbedPaneUI is instance of WindowsTabbedPaneUI
// You may add checks for other L&F
if (tabbedPane.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI) {
tabbedPane.setUI(new MyWindowsTabbedPaneUI());
}
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
add(tabbedPane, BorderLayout.NORTH);
}
// Class that overwrites WindowsTabbedPaneUI and returns false
class MyWindowsTabbedPaneUI extends com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI{
#Override
protected boolean shouldRotateTabRuns(int tabPlacement) {
return false;
}
}
}

opening a frame by clicking on the button

In my project I am trying to open LoginFrame from WelcomeFrame by clicking a button and I want myWelcomeFrameto be closed as well.
I have successfully opened theLoginFrameby usingsetVisible(true).
To close theWelcomeFrameI have writtenframe.SetVisible(false)where frame is the object ofWelcomeFrame` but this line shows an error: frame cannot be resolved ....
Here's my code.. Please help
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WelcomeFrame frame = new WelcomeFrame(); //object of WelcomeFrame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton btnNewButton = new JButton("Librarian Portal\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrame l=new LoginFrame();
l.setVisible(true);
frame.setVisible(false); //error:frame cannot be resolved
}
});
Because object of WelcomeFrame is not accessible.
You need to create another object before trying to call setVisible method.
WelcomeFrame closing_frame = new WelcomeFrame();
closing_frame.setVisible(false);
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
private WelcomeFrame frame;
after
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new WelcomeFrame();
I think it'll help you
a CardLayout is more adapted to your needs; the general idea is to create a jPanel instead of a JFrame, the CardLayout enables you to switch between them by showing only one at a time. if this is what you need have alook here

Displaying an image, Java

I'm new to Java GUI, and am having issues displaying an image. My intention is to display a large image and allow the user to click on regions of the image to indicate where certain features are located. Anyway, I'm getting a rough start because I can't even get the image to appear, despite reading Oracle's explanation and other solutions.
I've created a JFrame and used its setContentPane() method to add a JPanel and JLabel. I use the setIcon() method of the JLabel to add an image to it, or at least that's my intention...
Any advice is appreciated, especially if there's a better way of doing this. I'll be using OpenCV to process images, and plan to convert them to Java image (or BufferedImage) before displaying them.
Here is the code. I left out the libraries to save space.
public class Pathology {
public static void main(String[] args) {
PrimaryFrame primaryFrame = new PrimaryFrame();
primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
primaryFrame.setSize(1500, 900);
primaryFrame.setVisible( true );
primaryFrame.setContentPane(primaryFrame.getGui());
try {
primaryFrame.setImage(ImageIO.read(new File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GUI Class:
public class PrimaryFrame extends JFrame{
//private JTextField textField1;
JPanel gui;
JLabel imageCanvas;
public PrimaryFrame() {
super( "Pathology-1" );
//setLayout(new FlowLayout());
//textField1 = new JTextField("Chup!", 50);
//add(textField1);
}
public void setImage(Image image) {
imageCanvas.setIcon(new ImageIcon(image));
}
public void initComponents() {
if (gui==null) {
gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(5,5,5,5));
imageCanvas = new JLabel();
JPanel imageCenter = new JPanel(new GridBagLayout());
imageCenter.add(imageCanvas);
JScrollPane imageScroll = new JScrollPane(imageCenter);
imageScroll.setPreferredSize(new Dimension(300,100));
gui.add(imageScroll, BorderLayout.CENTER);
}
}
public Container getGui() {
initComponents();
return gui;
}
}
Would you laugh at me if I'd tell you that you just have to put the primaryFrame.setVisible( true ); to the end of the main method? :)
For furture understanding, you don't have to call frame.setVisible(true) every time you want to add/update something in the frame (in an ActionListener, for example). Instead you can call frame.revalidate() and frame.repaint(). (Where frame can be replaced with the particular panel)
You need to setVisible(true) after the call to setImage():
primaryFrame.setImage(ImageIO.read(new
File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
because any update to the GUI after setVisible() will not be shown.
That's it and the code should be like this:
public class Pathology {
public static void main(String[] args) {
PrimaryFrame primaryFrame = new PrimaryFrame();
primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
primaryFrame.setSize(1500, 900);
primaryFrame.setContentPane(primaryFrame.getGui());
try {
primaryFrame.setImage(ImageIO.read(new File(
"C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
primaryFrame.setVisible( true );
}
}

Change JLabel text from another class

I have the following class that draws a Label. (I have only given part of the code here). Everyhting works fine, the label gets displayed.
Now, i have another class called Caller Class. I have a method in that where i will use to change the value of this label. how can i do that
public class MyClass{
private JLabel label;
MyClass(){
run();
}
public void editTheLabelsValue (String text) {
label.setText(text);
frame.repaint();
}
run(){
.... // there were more code here, i removed it as it's not relevant to the problem
label = new JLabel("Whooo");
label.setBounds(0, 0, 50, 100);
frame.getContentPane().add(label);
.....
}
later on, i will be using the following class to change the text of the above label. How can i do this.
public class Caller {
void methodA(){
MyClass mc = new MyClass();
mc.editTheLabelsValue("Hello");
}
}
1.) When the methodA() is executed, the text Hello is not getting displayed on the Label field. it still remains as Whooo. How can i correct this. I want the label text to be Hello once that method has been executed.
The immeditate problem I can see is to appears that you are either using a null layout or your don't understand how layout managers work.
The following code updates the label from the main class in a sub class via a setText method call. This method is called every second
public class PaintMyLabel {
private int counter = 0;
public static void main(String[] args) {
new PaintMyLabel();
}
public PaintMyLabel() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final MasterPane master = new MasterPane();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(master);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
counter++;
master.setText("Now updated " + counter + " times");
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
});
}
public class MasterPane extends JPanel {
private JLabel label;
public MasterPane() {
label = new JLabel("Original text");
setLayout(new GridBagLayout());
add(label);
}
public void setText(String text) {
label.setText(text);
}
}
}
If you're using a null layout, then stop it. Just don't. There are only a very small number of times you would ever use a null layout and I suspect this isn't one of them.

How to have a JFrame Maximise icon

How do i create a JFrame window in eclipse on a Mac that has an icon that makes the window full screen like the double arrow icon on most windows at the top right??
Take a look at
Fullscreen feature for Java Apps on OSX Lion
And Java Runtime System Properties, which may be of interested
or How can I do full screen in Java on OSX if those were the wrong feature you wanted
UPDATE
Lucky for you JFrame extends Window via Frame...
public class TestMacFullScreen {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
JLabel label = new JLabel("Look ma, no hands");
frame.add(label);
enableOSXFullscreen(frame);
frame.setVisible(true);
}
});
}
public static void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
} catch (Exception exp) {
exp.printStackTrace();
}
}
}

Categories